Skip to content

Commit

Permalink
Merge pull request #300 from plank/scoped-disk-support
Browse files Browse the repository at this point in the history
support scoped disk driver
  • Loading branch information
frasmage committed Jan 3, 2023
2 parents e3f69cf + dbeea96 commit f613f66
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/UrlGenerators/UrlGeneratorFactory.php
Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions tests/Integration/UrlGenerators/UrlGeneratorFactoryTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
7 changes: 6 additions & 1 deletion tests/TestCase.php
Expand Up @@ -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', [
Expand Down

0 comments on commit f613f66

Please sign in to comment.