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