Storage.php 8.6 KB

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