From dbeea965807b64f461fc84126033043dcb3c3365 Mon Sep 17 00:00:00 2001 From: Sean Fraser Date: Tue, 3 Jan 2023 10:39:25 -0500 Subject: [PATCH] support scoped disk driver --- composer.json | 1 + src/UrlGenerators/UrlGeneratorFactory.php | 6 +++++- .../UrlGenerators/UrlGeneratorFactoryTest.php | 21 +++++++++++++++++++ tests/TestCase.php | 7 ++++++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6af5b4cc..3ffafaeb 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "intervention/image": "^2.7.1", "guzzlehttp/guzzle": "^6.5.5|^7.4.1", "symfony/http-foundation": "^5.0.11|^6.0.3" + }, "require-dev": { "orchestra/testbench": "^6.6|^7.0", diff --git a/src/UrlGenerators/UrlGeneratorFactory.php b/src/UrlGenerators/UrlGeneratorFactory.php index 7a263a73..a5b8b647 100644 --- a/src/UrlGenerators/UrlGeneratorFactory.php +++ b/src/UrlGenerators/UrlGeneratorFactory.php @@ -70,6 +70,10 @@ protected function validateGeneratorClass(string $class): void */ protected function getDriverForDisk(string $disk): string { - return (string) config("filesystems.disks.{$disk}.driver"); + $driver = (string) config("filesystems.disks.{$disk}.driver"); + if ($driver === 'scoped') { + return $this->getDriverForDisk(config("filesystems.disks.{$disk}.disk")); + } + return $driver; } } diff --git a/tests/Integration/UrlGenerators/UrlGeneratorFactoryTest.php b/tests/Integration/UrlGenerators/UrlGeneratorFactoryTest.php index 54d1ddd5..d81dca53 100644 --- a/tests/Integration/UrlGenerators/UrlGeneratorFactoryTest.php +++ b/tests/Integration/UrlGenerators/UrlGeneratorFactoryTest.php @@ -2,9 +2,11 @@ namespace Plank\Mediable\Tests\Integration\UrlGenerators; +use League\Flysystem\PathPrefixing\PathPrefixedAdapter; use Plank\Mediable\Exceptions\MediaUrlException; use Plank\Mediable\Media; use Plank\Mediable\Tests\TestCase; +use Plank\Mediable\UrlGenerators\LocalUrlGenerator; use Plank\Mediable\UrlGenerators\UrlGeneratorFactory; use Plank\Mediable\UrlGenerators\UrlGeneratorInterface; use stdClass; @@ -38,4 +40,23 @@ public function test_it_throws_exception_if_cant_map_to_driver() $this->expectException(MediaUrlException::class); $factory->create($media); } + + public function test_it_follows_scoped_prefix() + { + if (version_compare($this->app->version(), '9.30.0', '<')) { + $this->markTestSkipped("scoped disk prefixes are only supported in laravel 9.30.0+"); + } + // TODO: league/flysystem-path-prefixing requires PHP 8, we still support 7.4 + // so can't include it in composer.json yet. To be fixed in next major version + if (!class_exists(PathPrefixedAdapter::class)) { + $this->markTestSkipped("path prefixing not installed"); + } + $factory = new UrlGeneratorFactory; + $factory->setGeneratorForFilesystemDriver(LocalUrlGenerator::class, 'local'); + /** @var Media $media */ + $media = factory(Media::class)->make(['disk' => 'scoped']); + $result = $factory->create($media); + $this->assertInstanceOf(LocalUrlGenerator::class, $result); + $this->assertEquals('/storage/scoped/' . $media->getDiskPath(), $result->getUrl()); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index c393242d..01a437a4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -80,7 +80,12 @@ protected function getEnvironmentSetUp($app) 'visibility' => 'public', // set random root to avoid parallel test runs from deleting each other's files 'root' => Factory::create()->md5 - ] + ], + 'scoped' => [ + 'driver' => 'scoped', + 'disk' => 'tmp', + 'prefix' => 'scoped' + ], ]); $app['config']->set('mediable.allowed_disks', [