Storage.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Illuminate\Support\Facades;
  3. use Illuminate\Filesystem\Filesystem;
  4. use function Illuminate\Support\enum_value;
  5. /**
  6. * @method static \Illuminate\Contracts\Filesystem\Filesystem drive(string|null $name = null)
  7. * @method static \Illuminate\Contracts\Filesystem\Filesystem disk(\UnitEnum|string|null $name = null)
  8. * @method static \Illuminate\Contracts\Filesystem\Cloud cloud()
  9. * @method static \Illuminate\Contracts\Filesystem\Filesystem build(string|array $config)
  10. * @method static \Illuminate\Contracts\Filesystem\Filesystem createLocalDriver(array $config, string $name = 'local')
  11. * @method static \Illuminate\Contracts\Filesystem\Filesystem createFtpDriver(array $config)
  12. * @method static \Illuminate\Contracts\Filesystem\Filesystem createSftpDriver(array $config)
  13. * @method static \Illuminate\Contracts\Filesystem\Cloud createS3Driver(array $config)
  14. * @method static \Illuminate\Contracts\Filesystem\Filesystem createScopedDriver(array $config)
  15. * @method static \Illuminate\Filesystem\FilesystemManager set(string $name, mixed $disk)
  16. * @method static string getDefaultDriver()
  17. * @method static string getDefaultCloudDriver()
  18. * @method static \Illuminate\Filesystem\FilesystemManager forgetDisk(array|string $disk)
  19. * @method static void purge(string|null $name = null)
  20. * @method static \Illuminate\Filesystem\FilesystemManager extend(string $driver, \Closure $callback)
  21. * @method static \Illuminate\Filesystem\FilesystemManager setApplication(\Illuminate\Contracts\Foundation\Application $app)
  22. * @method static string path(string $path)
  23. * @method static bool exists(string $path)
  24. * @method static string|null get(string $path)
  25. * @method static resource|null readStream(string $path)
  26. * @method static bool put(string $path, \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $contents, mixed $options = [])
  27. * @method static string|false putFile(\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path, \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file = null, mixed $options = [])
  28. * @method static string|false putFileAs(\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $path, \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|array|null $file, string|array|null $name = null, mixed $options = [])
  29. * @method static bool writeStream(string $path, resource $resource, array $options = [])
  30. * @method static string getVisibility(string $path)
  31. * @method static bool setVisibility(string $path, string $visibility)
  32. * @method static bool prepend(string $path, string $data)
  33. * @method static bool append(string $path, string $data)
  34. * @method static bool delete(string|array $paths)
  35. * @method static bool copy(string $from, string $to)
  36. * @method static bool move(string $from, string $to)
  37. * @method static int size(string $path)
  38. * @method static int lastModified(string $path)
  39. * @method static array files(string|null $directory = null, bool $recursive = false)
  40. * @method static array allFiles(string|null $directory = null)
  41. * @method static array directories(string|null $directory = null, bool $recursive = false)
  42. * @method static array allDirectories(string|null $directory = null)
  43. * @method static bool makeDirectory(string $path)
  44. * @method static bool deleteDirectory(string $directory)
  45. * @method static \Illuminate\Filesystem\FilesystemAdapter assertExists(string|array $path, string|null $content = null)
  46. * @method static \Illuminate\Filesystem\FilesystemAdapter assertCount(string $path, int $count, bool $recursive = false)
  47. * @method static \Illuminate\Filesystem\FilesystemAdapter assertMissing(string|array $path)
  48. * @method static \Illuminate\Filesystem\FilesystemAdapter assertDirectoryEmpty(string $path)
  49. * @method static bool missing(string $path)
  50. * @method static bool fileExists(string $path)
  51. * @method static bool fileMissing(string $path)
  52. * @method static bool directoryExists(string $path)
  53. * @method static bool directoryMissing(string $path)
  54. * @method static array|null json(string $path, int $flags = 0)
  55. * @method static \Symfony\Component\HttpFoundation\StreamedResponse response(string $path, string|null $name = null, array $headers = [], string|null $disposition = 'inline')
  56. * @method static \Symfony\Component\HttpFoundation\StreamedResponse serve(\Illuminate\Http\Request $request, string $path, string|null $name = null, array $headers = [])
  57. * @method static \Symfony\Component\HttpFoundation\StreamedResponse download(string $path, string|null $name = null, array $headers = [])
  58. * @method static string|false checksum(string $path, array $options = [])
  59. * @method static string|false mimeType(string $path)
  60. * @method static string url(string $path)
  61. * @method static bool providesTemporaryUrls()
  62. * @method static bool providesTemporaryUploadUrls()
  63. * @method static string temporaryUrl(string $path, \DateTimeInterface $expiration, array $options = [])
  64. * @method static array temporaryUploadUrl(string $path, \DateTimeInterface $expiration, array $options = [])
  65. * @method static \League\Flysystem\FilesystemOperator getDriver()
  66. * @method static \League\Flysystem\FilesystemAdapter getAdapter()
  67. * @method static array getConfig()
  68. * @method static void serveUsing(\Closure $callback)
  69. * @method static void buildTemporaryUrlsUsing(\Closure $callback)
  70. * @method static void buildTemporaryUploadUrlsUsing(\Closure $callback)
  71. * @method static \Illuminate\Filesystem\FilesystemAdapter|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
  72. * @method static \Illuminate\Filesystem\FilesystemAdapter|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
  73. * @method static void macro(string $name, object|callable $macro)
  74. * @method static void mixin(object $mixin, bool $replace = true)
  75. * @method static bool hasMacro(string $name)
  76. * @method static void flushMacros()
  77. * @method static mixed macroCall(string $method, array $parameters)
  78. * @method static bool has(string $location)
  79. * @method static string read(string $location)
  80. * @method static \League\Flysystem\DirectoryListing listContents(string $location, bool $deep = false)
  81. * @method static int fileSize(string $path)
  82. * @method static string visibility(string $path)
  83. * @method static void write(string $location, string $contents, array $config = [])
  84. * @method static void createDirectory(string $location, array $config = [])
  85. *
  86. * @see \Illuminate\Filesystem\FilesystemManager
  87. */
  88. class Storage extends Facade
  89. {
  90. /**
  91. * Replace the given disk with a local testing disk.
  92. *
  93. * @param \UnitEnum|string|null $disk
  94. * @param array $config
  95. * @return \Illuminate\Filesystem\LocalFilesystemAdapter
  96. */
  97. public static function fake($disk = null, array $config = [])
  98. {
  99. $root = self::getRootPath($disk = enum_value($disk) ?: static::$app['config']->get('filesystems.default'));
  100. if ($token = ParallelTesting::token()) {
  101. $root = "{$root}_test_{$token}";
  102. }
  103. (new Filesystem)->cleanDirectory($root);
  104. static::set($disk, $fake = static::createLocalDriver(
  105. self::buildDiskConfiguration($disk, $config, root: $root)
  106. ));
  107. return tap($fake, function ($fake) {
  108. $fake->buildTemporaryUrlsUsing(function ($path, $expiration) {
  109. return URL::to($path.'?expiration='.$expiration->getTimestamp());
  110. });
  111. $fake->buildTemporaryUploadUrlsUsing(function ($path, $expiration) {
  112. return ['url' => URL::to($path.'?expiration='.$expiration->getTimestamp()), 'headers' => []];
  113. });
  114. });
  115. }
  116. /**
  117. * Replace the given disk with a persistent local testing disk.
  118. *
  119. * @param \UnitEnum|string|null $disk
  120. * @param array $config
  121. * @return \Illuminate\Filesystem\LocalFilesystemAdapter
  122. */
  123. public static function persistentFake($disk = null, array $config = [])
  124. {
  125. $disk = enum_value($disk) ?: static::$app['config']->get('filesystems.default');
  126. static::set($disk, $fake = static::createLocalDriver(
  127. self::buildDiskConfiguration($disk, $config, root: self::getRootPath($disk))
  128. ));
  129. return $fake;
  130. }
  131. /**
  132. * Get the root path of the given disk.
  133. *
  134. * @param string $disk
  135. * @return string
  136. */
  137. protected static function getRootPath(string $disk): string
  138. {
  139. return storage_path('framework/testing/disks/'.$disk);
  140. }
  141. /**
  142. * Assemble the configuration of the given disk.
  143. *
  144. * @param string $disk
  145. * @param array $config
  146. * @param string $root
  147. * @return array
  148. */
  149. protected static function buildDiskConfiguration(string $disk, array $config, string $root): array
  150. {
  151. $originalConfig = static::$app['config']["filesystems.disks.{$disk}"] ?? [];
  152. return array_merge([
  153. 'throw' => $originalConfig['throw'] ?? false],
  154. $config,
  155. ['root' => $root]
  156. );
  157. }
  158. /**
  159. * Get the registered name of the component.
  160. *
  161. * @return string
  162. */
  163. protected static function getFacadeAccessor()
  164. {
  165. return 'filesystem';
  166. }
  167. }