symfonyid/symfony-bundle-plugins

允许 Symfony 包使用插件

2.0.3 2015-09-25 03:57 UTC

This package is auto-updated.

Last update: 2024-09-20 19:04:31 UTC


README

原作者 Matthias Noback

此包通过引入插件系统,帮助您创建可扩展的包。每个插件包都可以定义自己的服务和配置。这基本上使您的包遵循开放/封闭原则。

设置

在项目中运行以下命令来安装此库:

composer require ihsanudin/symfony-bundle-plugins

示例

首先,您的包应该扩展 PluginBundle。您需要实现 getAlias 方法。它应该返回您的包配置键的名称(例如,在 config.yml 中使用)。

use Symfonian\Indonesia\BundlePlugins\PluginBundle;

class DemoBundle extends PluginBundle
{
    protected function getAlias()
    {
        return 'demo';
    }
}

每个包插件都应该实现 PluginBundle

use Symfonian\Indonesia\BundlePlugins\PluginBundle;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class FooPlugin implements PluginBundle
{
    public function name()
    {
        return 'foo';
    }

    public function load(
        array $pluginConfiguration,
        ContainerBuilder $container
    ) {
        // load specific service definitions for this plugin,
        // just like you would do in a bundle extension

        $loader = new YamlFileLoader($container, new FileLocator(__DIR__));
        $loader->load('foo.yml');

        // $pluginConfiguration contains just the values that are relevant
        // for this plugin
    }

    public function addConfiguration(ArrayNodeDefinition $pluginNode)
    {
        // add plugin-specific configuration nodes,
        // just like you would do in a bundle extension

        $pluginNode
            ->children()
                ->scalarNode('foo')
                ->isRequired()
            ->end();
    }
}

在您的 AppKernel 类中实例化此包时,您可以提供任意数量的 PluginBundle 实例

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        return array(
            ...,
            new DemoBundle(array(new FooPlugin()))
        );
    }
}

如果某些插件是必需的,只需引入一个 CorePlugin 并确保通过重写您的包的 alwaysRegisteredPlugins() 方法始终注册它

class DemoBundle
{
    ...

    protected function alwaysRegisteredPlugins()
    {
        return array(new CorePlugin());
    }
}

注册编译器遍历

当包插件需要注册编译器遍历时,它可以在其 build() 方法中这样做。

class FooPlugin implements PluginBundle
{
    ...

    public function build(ContainerBuilder $container)
    {
        $container->addCompilerPass(...);
    }
}

启动插件

每当主包启动时,插件也被允许进行一些运行时初始化。它们可以在其 boot() 方法中这样做。那时,完全初始化的服务容器是可用的。

class FooPlugin implements PluginBundle
{
    ...

    public function boot(ContainerInterface $container)
    {
        // runtime initialization (will run when the kernel itself is
        // booted)
    }
}

简单插件

如果您的插件相当简单(即只需要一个 load() 方法),只需让插件类扩展 SimplePluginBundle,其中包含您不需要的接口方法的占位符实现。

感谢

感谢 @dennisdegreef 为恢复此项目的测试套件。