Skip to content

Commit 6d05c50

Browse files
authored
Merge pull request #123 from norkunas/urls
Allow to configure public/temporary url generation
2 parents fb338cf + 9b55b5c commit 6d05c50

File tree

9 files changed

+130
-2
lines changed

9 files changed

+130
-2
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public function getConfigTreeBuilder(): TreeBuilder
4444
->scalarNode('directory_visibility')->defaultNull()->end()
4545
->booleanNode('case_sensitive')->defaultTrue()->end()
4646
->booleanNode('disable_asserts')->defaultFalse()->end()
47+
->arrayNode('public_url')
48+
->beforeNormalization()->castToArray()->end()
49+
->defaultValue([])
50+
->scalarPrototype()->end()
51+
->end()
52+
->scalarNode('public_url_generator')->defaultNull()->end()
53+
->scalarNode('temporary_url_generator')->defaultNull()->end()
4754
->end()
4855
->end()
4956
->defaultValue([])

src/DependencyInjection/FlysystemExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ private function createLazyStorageDefinition(string $storageName, array $options
100100

101101
private function createStorageDefinition(string $storageName, Reference $adapter, array $config)
102102
{
103+
$publicUrl = null;
104+
if ($config['public_url']) {
105+
$publicUrl = 1 === count($config['public_url']) ? $config['public_url'][0] : $config['public_url'];
106+
}
107+
103108
$definition = new Definition(Filesystem::class);
104109
$definition->setPublic(false);
105110
$definition->setArgument(0, $adapter);
@@ -108,7 +113,11 @@ private function createStorageDefinition(string $storageName, Reference $adapter
108113
'directory_visibility' => $config['directory_visibility'],
109114
'case_sensitive' => $config['case_sensitive'],
110115
'disable_asserts' => $config['disable_asserts'],
116+
'public_url' => $publicUrl,
111117
]);
118+
$definition->setArgument(2, null);
119+
$definition->setArgument(3, $config['public_url_generator'] ? new Reference($config['public_url_generator']) : null);
120+
$definition->setArgument(4, $config['temporary_url_generator'] ? new Reference($config['temporary_url_generator']) : null);
112121
$definition->addTag('flysystem.storage', ['storage' => $storageName]);
113122

114123
return $definition;

tests/DependencyInjection/FlysystemExtensionTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function provideFilesystems()
4444
/**
4545
* @dataProvider provideFilesystems
4646
*/
47-
public function testFileystems(string $fsName)
47+
public function testFilesystems(string $fsName)
4848
{
4949
$kernel = $this->createFysystemKernel();
5050
$container = $kernel->getContainer()->get('test.service_container');
@@ -71,6 +71,39 @@ public function testTaggedCollection(string $fsName)
7171
$this->assertInstanceOf(FilesystemOperator::class, $storages[$fsName]);
7272
}
7373

74+
public function testPublicUrl()
75+
{
76+
$kernel = $this->createFysystemKernel();
77+
$container = $kernel->getContainer()->get('test.service_container');
78+
79+
$fs = $container->get('flysystem.test.fs_public_url');
80+
81+
self::assertSame('https://example.org/assets/test1.txt', $fs->publicUrl('test1.txt'));
82+
}
83+
84+
public function testPublicUrls()
85+
{
86+
$kernel = $this->createFysystemKernel();
87+
$container = $kernel->getContainer()->get('test.service_container');
88+
89+
$fs = $container->get('flysystem.test.fs_public_urls');
90+
91+
self::assertSame('https://cdn1.example.org/test1.txt', $fs->publicUrl('test1.txt'));
92+
self::assertSame('https://cdn2.example.org/yo/test2.txt', $fs->publicUrl('yo/test2.txt'));
93+
self::assertSame('https://cdn3.example.org/yww/test1.txt', $fs->publicUrl('yww/test1.txt'));
94+
}
95+
96+
public function testUrlGenerators()
97+
{
98+
$kernel = $this->createFysystemKernel();
99+
$container = $kernel->getContainer()->get('test.service_container');
100+
101+
$fs = $container->get('flysystem.test.fs_url_generator');
102+
103+
self::assertSame('https://example.org/generator/test1.txt', $fs->publicUrl('test1.txt'));
104+
self::assertSame('https://example.org/temporary/test1.txt?expiresAt=1670846026', $fs->temporaryUrl('test1.txt', new \DateTimeImmutable('@1670846026')));
105+
}
106+
74107
private function createFysystemKernel()
75108
{
76109
(new Dotenv())->populate([

tests/Kernel/FrameworkAppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function registerBundles(): iterable
2929
public function registerContainerConfiguration(LoaderInterface $loader)
3030
{
3131
$loader->load(function (ContainerBuilder $container) {
32-
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true]);
32+
$container->loadFromExtension('framework', ['secret' => '$ecret', 'test' => true, 'http_method_override' => false]);
3333
$container->loadFromExtension('flysystem', [
3434
'storages' => [
3535
'uploads.storage' => [

tests/Kernel/config/flysystem.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,25 @@ flysystem:
7676
privateKey: 'path/to/or/contents/of/privatekey'
7777
root: '/path/to/root'
7878
timeout: 10
79+
80+
fs_public_url:
81+
adapter: 'local'
82+
options:
83+
directory: '/tmp/storage'
84+
public_url: 'https://example.org/assets/'
85+
86+
fs_public_urls:
87+
adapter: 'local'
88+
options:
89+
directory: '/tmp/storage'
90+
public_url:
91+
- 'https://cdn1.example.org/'
92+
- 'https://cdn2.example.org/'
93+
- 'https://cdn3.example.org/'
94+
95+
fs_url_generator:
96+
adapter: 'local'
97+
options:
98+
directory: '/tmp/storage'
99+
public_url_generator: 'flysystem.test.public_url_generator'
100+
temporary_url_generator: 'flysystem.test.temporary_url_generator'

tests/Kernel/config/framework.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
framework:
22
secret: 'test_secret'
33
test: true
4+
http_method_override: false

tests/Kernel/config/services.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ services:
55
custom_adapter:
66
class: 'League\Flysystem\InMemory\InMemoryFilesystemAdapter'
77

8+
flysystem.test.public_url_generator:
9+
class: Tests\League\FlysystemBundle\PublicUrlGeneratorMock
10+
11+
flysystem.test.temporary_url_generator:
12+
class: Tests\League\FlysystemBundle\TemporaryUrlGeneratorMock
13+
814
# Aliases used to test the services construction
915
flysystem.test.fs_asyncaws: { alias: 'fs_asyncaws' }
1016
flysystem.test.fs_aws: { alias: 'fs_aws' }
@@ -16,3 +22,6 @@ services:
1622
flysystem.test.fs_local: { alias: 'fs_local' }
1723
flysystem.test.fs_memory: { alias: 'fs_memory' }
1824
flysystem.test.fs_sftp: { alias: 'fs_sftp' }
25+
flysystem.test.fs_public_url: { alias: 'fs_public_url' }
26+
flysystem.test.fs_public_urls: { alias: 'fs_public_urls' }
27+
flysystem.test.fs_url_generator: { alias: 'fs_url_generator' }

tests/PublicUrlGeneratorMock.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\League\FlysystemBundle;
13+
14+
use League\Flysystem\Config;
15+
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
16+
17+
final class PublicUrlGeneratorMock implements PublicUrlGenerator
18+
{
19+
public function publicUrl(string $path, Config $config): string
20+
{
21+
return "https://example.org/generator/$path";
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\League\FlysystemBundle;
13+
14+
use DateTimeInterface;
15+
use League\Flysystem\Config;
16+
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
17+
18+
final class TemporaryUrlGeneratorMock implements TemporaryUrlGenerator
19+
{
20+
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config $config): string
21+
{
22+
return "https://example.org/temporary/$path?expiresAt={$expiresAt->format('U')}";
23+
}
24+
}

0 commit comments

Comments
 (0)