Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugins support #13

Merged
merged 1 commit into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
}
}