Skip to content

Commit 6697e20

Browse files
committed
Add plugins support
1 parent 993e8a4 commit 6697e20

6 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <galopintitouan@gmail.com>
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 League\FlysystemBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
class PluginPass implements CompilerPassInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function process(ContainerBuilder $container)
25+
{
26+
$plugins = array_map(function ($id) {
27+
return new Reference($id);
28+
}, array_keys($container->findTaggedServiceIds('flysystem.plugin')));
29+
30+
if (0 === count($plugins)) {
31+
return;
32+
}
33+
34+
/** @var Definition[] $storages */
35+
$storages = array_map(function ($id) use ($container) {
36+
return $container->findDefinition($id);
37+
}, array_keys($container->findTaggedServiceIds('flysystem.storage')));
38+
39+
foreach ($storages as $storage) {
40+
foreach ($plugins as $plugin) {
41+
$storage->addMethodCall('addPlugin', [$plugin]);
42+
}
43+
}
44+
}
45+
}

src/DependencyInjection/FlysystemExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use League\Flysystem\Filesystem;
1515
use League\Flysystem\FilesystemInterface;
16+
use League\Flysystem\PluginInterface;
1617
use League\FlysystemBundle\Adapter\AdapterDefinitionFactory;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
1819
use Symfony\Component\DependencyInjection\Definition;
@@ -33,6 +34,11 @@ public function load(array $configs, ContainerBuilder $container)
3334

3435
$adapterFactory = new AdapterDefinitionFactory();
3536

37+
$container
38+
->registerForAutoconfiguration(PluginInterface::class)
39+
->addTag('flysystem.plugin')
40+
;
41+
3642
foreach ($config['storages'] as $storageName => $storageConfig) {
3743
// Create adapter service definition
3844
if ($adapter = $adapterFactory->createDefinition($storageConfig['adapter'], $storageConfig['options'])) {
@@ -61,6 +67,7 @@ private function createStorageDefinition(Reference $adapter, array $config)
6167
'case_sensitive' => $config['case_sensitive'],
6268
'disable_asserts' => $config['disable_asserts'],
6369
]);
70+
$definition->addTag('flysystem.storage');
6471

6572
return $definition;
6673
}

src/FlysystemBundle.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace League\FlysystemBundle;
1313

14+
use League\FlysystemBundle\DependencyInjection\Compiler\PluginPass;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
1416
use Symfony\Component\HttpKernel\Bundle\Bundle;
1517

1618
/**
@@ -20,4 +22,13 @@
2022
*/
2123
class FlysystemBundle extends Bundle
2224
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function build(ContainerBuilder $container)
29+
{
30+
parent::build($container);
31+
32+
$container->addCompilerPass(new PluginPass());
33+
}
2334
}

tests/DependencyInjection/FlysytemExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function testCreateFileystems()
4444
foreach ($this->getFilesystems() as $fsName) {
4545
$fs = $container->get('flysystem.test.'.$fsName);
4646
$this->assertInstanceOf(FilesystemInterface::class, $fs, 'Filesystem "'.$fsName.'" should be an instance of FilesystemInterface');
47+
$this->assertEquals('plugin', $fs->pluginTest());
4748
}
4849
}
4950

tests/Kernel/config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ services:
2121
flysystem.test.fs_sftp: { alias: 'fs_sftp' }
2222
flysystem.test.fs_webdav: { alias: 'fs_webdav' }
2323
flysystem.test.fs_zip: { alias: 'fs_zip' }
24+
25+
Tests\League\FlysystemBundle\Plugin\DummyPlugin:
26+
autoconfigure: true

tests/Plugin/DummyPlugin.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <galopintitouan@gmail.com>
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\Plugin;
13+
14+
use League\Flysystem\FilesystemInterface;
15+
use League\Flysystem\PluginInterface;
16+
17+
class DummyPlugin implements PluginInterface
18+
{
19+
public function handle()
20+
{
21+
return 'plugin';
22+
}
23+
24+
public function getMethod()
25+
{
26+
return 'pluginTest';
27+
}
28+
29+
public function setFilesystem(FilesystemInterface $filesystem)
30+
{
31+
}
32+
}

0 commit comments

Comments
 (0)