Skip to content

Commit

Permalink
add method to build an on-demand filesystem
Browse files Browse the repository at this point in the history
the `build` method allows the user to create an on-demand filesystem disk.

a string passed to the method will be treated as the "root" of a local driver (99% use case).

passing an array allows full customization of the disk config.
  • Loading branch information
browner12 committed Jun 17, 2021
1 parent 40104ba commit 3ec57b2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Illuminate/Filesystem/FilesystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ public function cloud()
return $this->disks[$name] = $this->get($name);
}

/**
* Build an on-demand disk.
*
* @param string|array $root
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public function build($root)
{
$config = is_array($root) ? $root : ['driver' => 'local', 'root' => $root];

return $this->disks['ondemand'] = $this->resolve('ondemand', $config);
}

/**
* Attempt to get the disk from the local cache.
*
Expand All @@ -109,9 +122,9 @@ protected function get($name)
*
* @throws \InvalidArgumentException
*/
protected function resolve($name)
protected function resolve($name, $config = null)
{
$config = $this->getConfig($name);
$config = $config ?? $this->getConfig($name);

if (empty($config['driver'])) {
throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver.");
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @method static \Illuminate\Contracts\Filesystem\Filesystem assertExists(string|array $path)
* @method static \Illuminate\Contracts\Filesystem\Filesystem assertMissing(string|array $path)
* @method static \Illuminate\Contracts\Filesystem\Filesystem cloud()
* @method static \Illuminate\Contracts\Filesystem\Filesystem build(string|array $root)
* @method static \Illuminate\Contracts\Filesystem\Filesystem disk(string|null $name = null)
* @method static \Illuminate\Filesystem\FilesystemManager extend(string $driver, \Closure $callback)
* @method static \Symfony\Component\HttpFoundation\StreamedResponse download(string $path, string|null $name = null, array|null $headers = [])
Expand Down
15 changes: 15 additions & 0 deletions tests/Filesystem/FilesystemManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Filesystem;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Foundation\Application;
use InvalidArgumentException;
Expand All @@ -20,4 +21,18 @@ public function testExceptionThrownOnUnsupportedDriver()

$filesystem->disk('local');
}

public function testCanBuildOnDemandDisk()
{
$filesystem = new FilesystemManager(new Application);

$this->assertInstanceOf(Filesystem::class, $filesystem->build('my-custom-path'));

$this->assertInstanceOf(Filesystem::class, $filesystem->build([
'driver' => 'local',
'root' => 'my-custom-path',
'url' => 'my-custom-url',
'visibility' => 'public',
]));
}
}

0 comments on commit 3ec57b2

Please sign in to comment.