Skip to content

Commit

Permalink
Merge pull request #123 from norkunas/urls
Browse files Browse the repository at this point in the history
Allow to configure public/temporary url generation
  • Loading branch information
tgalopin authored Dec 26, 2022
2 parents fb338cf + 9b55b5c commit 6d05c50
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('directory_visibility')->defaultNull()->end()
->booleanNode('case_sensitive')->defaultTrue()->end()
->booleanNode('disable_asserts')->defaultFalse()->end()
->arrayNode('public_url')
->beforeNormalization()->castToArray()->end()
->defaultValue([])
->scalarPrototype()->end()
->end()
->scalarNode('public_url_generator')->defaultNull()->end()
->scalarNode('temporary_url_generator')->defaultNull()->end()
->end()
->end()
->defaultValue([])
Expand Down
9 changes: 9 additions & 0 deletions src/DependencyInjection/FlysystemExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ private function createLazyStorageDefinition(string $storageName, array $options

private function createStorageDefinition(string $storageName, Reference $adapter, array $config)
{
$publicUrl = null;
if ($config['public_url']) {
$publicUrl = 1 === count($config['public_url']) ? $config['public_url'][0] : $config['public_url'];
}

$definition = new Definition(Filesystem::class);
$definition->setPublic(false);
$definition->setArgument(0, $adapter);
Expand All @@ -108,7 +113,11 @@ private function createStorageDefinition(string $storageName, Reference $adapter
'directory_visibility' => $config['directory_visibility'],
'case_sensitive' => $config['case_sensitive'],
'disable_asserts' => $config['disable_asserts'],
'public_url' => $publicUrl,
]);
$definition->setArgument(2, null);
$definition->setArgument(3, $config['public_url_generator'] ? new Reference($config['public_url_generator']) : null);
$definition->setArgument(4, $config['temporary_url_generator'] ? new Reference($config['temporary_url_generator']) : null);
$definition->addTag('flysystem.storage', ['storage' => $storageName]);

return $definition;
Expand Down
35 changes: 34 additions & 1 deletion tests/DependencyInjection/FlysystemExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function provideFilesystems()
/**
* @dataProvider provideFilesystems
*/
public function testFileystems(string $fsName)
public function testFilesystems(string $fsName)
{
$kernel = $this->createFysystemKernel();
$container = $kernel->getContainer()->get('test.service_container');
Expand All @@ -71,6 +71,39 @@ public function testTaggedCollection(string $fsName)
$this->assertInstanceOf(FilesystemOperator::class, $storages[$fsName]);
}

public function testPublicUrl()
{
$kernel = $this->createFysystemKernel();
$container = $kernel->getContainer()->get('test.service_container');

$fs = $container->get('flysystem.test.fs_public_url');

self::assertSame('https://example.org/assets/test1.txt', $fs->publicUrl('test1.txt'));
}

public function testPublicUrls()
{
$kernel = $this->createFysystemKernel();
$container = $kernel->getContainer()->get('test.service_container');

$fs = $container->get('flysystem.test.fs_public_urls');

self::assertSame('https://cdn1.example.org/test1.txt', $fs->publicUrl('test1.txt'));
self::assertSame('https://cdn2.example.org/yo/test2.txt', $fs->publicUrl('yo/test2.txt'));
self::assertSame('https://cdn3.example.org/yww/test1.txt', $fs->publicUrl('yww/test1.txt'));
}

public function testUrlGenerators()
{
$kernel = $this->createFysystemKernel();
$container = $kernel->getContainer()->get('test.service_container');

$fs = $container->get('flysystem.test.fs_url_generator');

self::assertSame('https://example.org/generator/test1.txt', $fs->publicUrl('test1.txt'));
self::assertSame('https://example.org/temporary/test1.txt?expiresAt=1670846026', $fs->temporaryUrl('test1.txt', new \DateTimeImmutable('@1670846026')));
}

private function createFysystemKernel()
{
(new Dotenv())->populate([
Expand Down
2 changes: 1 addition & 1 deletion tests/Kernel/FrameworkAppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function registerBundles(): iterable
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true]);
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true, 'http_method_override' => false]);
$container->loadFromExtension('flysystem', [
'storages' => [
'uploads.storage' => [
Expand Down
22 changes: 22 additions & 0 deletions tests/Kernel/config/flysystem.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,25 @@ flysystem:
privateKey: 'path/to/or/contents/of/privatekey'
root: '/path/to/root'
timeout: 10

fs_public_url:
adapter: 'local'
options:
directory: '/tmp/storage'
public_url: 'https://example.org/assets/'

fs_public_urls:
adapter: 'local'
options:
directory: '/tmp/storage'
public_url:
- 'https://cdn1.example.org/'
- 'https://cdn2.example.org/'
- 'https://cdn3.example.org/'

fs_url_generator:
adapter: 'local'
options:
directory: '/tmp/storage'
public_url_generator: 'flysystem.test.public_url_generator'
temporary_url_generator: 'flysystem.test.temporary_url_generator'
1 change: 1 addition & 0 deletions tests/Kernel/config/framework.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
framework:
secret: 'test_secret'
test: true
http_method_override: false
9 changes: 9 additions & 0 deletions tests/Kernel/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ services:
custom_adapter:
class: 'League\Flysystem\InMemory\InMemoryFilesystemAdapter'

flysystem.test.public_url_generator:
class: Tests\League\FlysystemBundle\PublicUrlGeneratorMock

flysystem.test.temporary_url_generator:
class: Tests\League\FlysystemBundle\TemporaryUrlGeneratorMock

# Aliases used to test the services construction
flysystem.test.fs_asyncaws: { alias: 'fs_asyncaws' }
flysystem.test.fs_aws: { alias: 'fs_aws' }
Expand All @@ -16,3 +22,6 @@ services:
flysystem.test.fs_local: { alias: 'fs_local' }
flysystem.test.fs_memory: { alias: 'fs_memory' }
flysystem.test.fs_sftp: { alias: 'fs_sftp' }
flysystem.test.fs_public_url: { alias: 'fs_public_url' }
flysystem.test.fs_public_urls: { alias: 'fs_public_urls' }
flysystem.test.fs_url_generator: { alias: 'fs_url_generator' }
23 changes: 23 additions & 0 deletions tests/PublicUrlGeneratorMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the flysystem-bundle project.
*
* (c) Titouan Galopin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\League\FlysystemBundle;

use League\Flysystem\Config;
use League\Flysystem\UrlGeneration\PublicUrlGenerator;

final class PublicUrlGeneratorMock implements PublicUrlGenerator
{
public function publicUrl(string $path, Config $config): string
{
return "https://example.org/generator/$path";
}
}
24 changes: 24 additions & 0 deletions tests/TemporaryUrlGeneratorMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the flysystem-bundle project.
*
* (c) Titouan Galopin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\League\FlysystemBundle;

use DateTimeInterface;
use League\Flysystem\Config;
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;

final class TemporaryUrlGeneratorMock implements TemporaryUrlGenerator
{
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string
{
return "https://example.org/temporary/$path?expiresAt={$expiresAt->format('U')}";
}
}

0 comments on commit 6d05c50

Please sign in to comment.