Skip to content

Instantly share code, notes, and snippets.

@bhaidar
Forked from stevebauman/FilesystemMockProvider.php
Created February 19, 2023 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhaidar/d4edabca2397049515c0f51db1671e7a to your computer and use it in GitHub Desktop.
Save bhaidar/d4edabca2397049515c0f51db1671e7a to your computer and use it in GitHub Desktop.
Filesystem Cloud Mocking
<?php
namespace App\Providers;
class FilesystemMockProvider extends ServiceProvider
{
public function boot()
{
if (App::isProduction()) {
return;
}
if (! config('filesystems.mock', false)) {
return;
}
$public = config('filesystems.disks.public');
foreach (config('filesystems.disks') as $disk => $config) {
if ('local' !== $config['driver']) {
$root = $config['root'] ?? $config['bucket'] ?? '/';
Storage::set($disk, Storage::createLocalDriver(array_merge($config, $public, [
'root' => implode(DIRECTORY_SEPARATOR, [$public['root'], $root]),
'url' => implode(DIRECTORY_SEPARATOR, [$public['url'], $root]),
])));
}
}
}
}
<?php
// config/filesystems.php
return [
// ...
/*
|--------------------------------------------------------------------------
| Filesystem Mocking
|--------------------------------------------------------------------------
|
| The application may store files in several different cloud providers.
| Enabling filesystem mocking will swap these cloud provider drivers
| with local ones, storing these files locally for easier testing.
|
*/
'mock' => env('FILESYSTEM_MOCK', env('APP_ENV') === 'local'),
// ...
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment