Skip to content

Commit

Permalink
Add plugins support
Browse files Browse the repository at this point in the history
  • Loading branch information
BoShurik committed May 31, 2019
1 parent 993e8a4 commit 6697e20
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/DependencyInjection/Compiler/PluginPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 League\FlysystemBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class PluginPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$plugins = array_map(function ($id) {
return new Reference($id);
}, array_keys($container->findTaggedServiceIds('flysystem.plugin')));

if (0 === count($plugins)) {
return;
}

/** @var Definition[] $storages */
$storages = array_map(function ($id) use ($container) {
return $container->findDefinition($id);
}, array_keys($container->findTaggedServiceIds('flysystem.storage')));

foreach ($storages as $storage) {
foreach ($plugins as $plugin) {
$storage->addMethodCall('addPlugin', [$plugin]);
}
}
}
}
7 changes: 7 additions & 0 deletions src/DependencyInjection/FlysystemExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\PluginInterface;
use League\FlysystemBundle\Adapter\AdapterDefinitionFactory;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -33,6 +34,11 @@ public function load(array $configs, ContainerBuilder $container)

$adapterFactory = new AdapterDefinitionFactory();

$container
->registerForAutoconfiguration(PluginInterface::class)
->addTag('flysystem.plugin')
;

foreach ($config['storages'] as $storageName => $storageConfig) {
// Create adapter service definition
if ($adapter = $adapterFactory->createDefinition($storageConfig['adapter'], $storageConfig['options'])) {
Expand Down Expand Up @@ -61,6 +67,7 @@ private function createStorageDefinition(Reference $adapter, array $config)
'case_sensitive' => $config['case_sensitive'],
'disable_asserts' => $config['disable_asserts'],
]);
$definition->addTag('flysystem.storage');

return $definition;
}
Expand Down
11 changes: 11 additions & 0 deletions src/FlysystemBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace League\FlysystemBundle;

use League\FlysystemBundle\DependencyInjection\Compiler\PluginPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -20,4 +22,13 @@
*/
class FlysystemBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new PluginPass());
}
}
1 change: 1 addition & 0 deletions tests/DependencyInjection/FlysytemExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function testCreateFileystems()
foreach ($this->getFilesystems() as $fsName) {
$fs = $container->get('flysystem.test.'.$fsName);
$this->assertInstanceOf(FilesystemInterface::class, $fs, 'Filesystem "'.$fsName.'" should be an instance of FilesystemInterface');
$this->assertEquals('plugin', $fs->pluginTest());
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Kernel/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ services:
flysystem.test.fs_sftp: { alias: 'fs_sftp' }
flysystem.test.fs_webdav: { alias: 'fs_webdav' }
flysystem.test.fs_zip: { alias: 'fs_zip' }

Tests\League\FlysystemBundle\Plugin\DummyPlugin:
autoconfigure: true
32 changes: 32 additions & 0 deletions tests/Plugin/DummyPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Plugin;

use League\Flysystem\FilesystemInterface;
use League\Flysystem\PluginInterface;

class DummyPlugin implements PluginInterface
{
public function handle()
{
return 'plugin';
}

public function getMethod()
{
return 'pluginTest';
}

public function setFilesystem(FilesystemInterface $filesystem)
{
}
}

0 comments on commit 6697e20

Please sign in to comment.