FilesystemServiceProvider.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Illuminate\Filesystem;
  3. use Illuminate\Contracts\Foundation\CachesRoutes;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Route;
  6. use Illuminate\Support\ServiceProvider;
  7. class FilesystemServiceProvider extends ServiceProvider
  8. {
  9. /**
  10. * Bootstrap the filesystem.
  11. *
  12. * @return void
  13. */
  14. public function boot()
  15. {
  16. $this->serveFiles();
  17. }
  18. /**
  19. * Register the service provider.
  20. *
  21. * @return void
  22. */
  23. public function register()
  24. {
  25. $this->registerNativeFilesystem();
  26. $this->registerFlysystem();
  27. }
  28. /**
  29. * Register the native filesystem implementation.
  30. *
  31. * @return void
  32. */
  33. protected function registerNativeFilesystem()
  34. {
  35. $this->app->singleton('files', function () {
  36. return new Filesystem;
  37. });
  38. }
  39. /**
  40. * Register the driver based filesystem.
  41. *
  42. * @return void
  43. */
  44. protected function registerFlysystem()
  45. {
  46. $this->registerManager();
  47. $this->app->singleton('filesystem.disk', function ($app) {
  48. return $app['filesystem']->disk($this->getDefaultDriver());
  49. });
  50. $this->app->singleton('filesystem.cloud', function ($app) {
  51. return $app['filesystem']->disk($this->getCloudDriver());
  52. });
  53. }
  54. /**
  55. * Register the filesystem manager.
  56. *
  57. * @return void
  58. */
  59. protected function registerManager()
  60. {
  61. $this->app->singleton('filesystem', function ($app) {
  62. return new FilesystemManager($app);
  63. });
  64. }
  65. /**
  66. * Register protected file serving.
  67. *
  68. * @return void
  69. */
  70. protected function serveFiles()
  71. {
  72. if ($this->app instanceof CachesRoutes && $this->app->routesAreCached()) {
  73. return;
  74. }
  75. foreach ($this->app['config']['filesystems.disks'] ?? [] as $disk => $config) {
  76. if (! $this->shouldServeFiles($config)) {
  77. continue;
  78. }
  79. $this->app->booted(function ($app) use ($disk, $config) {
  80. $uri = isset($config['url'])
  81. ? rtrim(parse_url($config['url'])['path'], '/')
  82. : '/storage';
  83. $isProduction = $app->isProduction();
  84. Route::get($uri.'/{path}', function (Request $request, string $path) use ($disk, $config, $isProduction) {
  85. return (new ServeFile(
  86. $disk,
  87. $config,
  88. $isProduction
  89. ))($request, $path);
  90. })->where('path', '.*')->name('storage.'.$disk);
  91. Route::put($uri.'/{path}', function (Request $request, string $path) use ($disk, $config, $isProduction) {
  92. return (new ReceiveFile(
  93. $disk,
  94. $config,
  95. $isProduction
  96. ))($request, $path);
  97. })->where('path', '.*')->name('storage.'.$disk.'.upload');
  98. });
  99. }
  100. }
  101. /**
  102. * Determine if the disk is serveable.
  103. *
  104. * @param array $config
  105. * @return bool
  106. */
  107. protected function shouldServeFiles(array $config)
  108. {
  109. return $config['driver'] === 'local' && ($config['serve'] ?? false);
  110. }
  111. /**
  112. * Get the default file driver.
  113. *
  114. * @return string
  115. */
  116. protected function getDefaultDriver()
  117. {
  118. return $this->app['config']['filesystems.default'];
  119. }
  120. /**
  121. * Get the default cloud based file driver.
  122. *
  123. * @return string
  124. */
  125. protected function getCloudDriver()
  126. {
  127. return $this->app['config']['filesystems.cloud'];
  128. }
  129. }