使用AbstractPluginManager和Zend Expressive 3

时间:2018-04-12 13:42:59

标签: php dependency-injection zend-expressive zend-servicemanager

我想在我的一个服务中注入一个实现公共接口的对象数组。我使用zend servicemanager作为DI容器。我现在已经阅读了很多文档,在我看来,AbstractPluginManager是最佳选择。我还没有能够让它发挥作用。 有没有使用AbstractPluginManager + Zend Expressive 3的例子,我可以看一下?

我的最终目标是动态地将实现公共接口的所有已注册类注入我的服务中。

示例:

interface I{}
class A implements I{}
class B implements I{}
class C{}

为MyService

__construct(array Iimplementations){...}

$service = $container->get('myservice')

$service has Iimplementations

提前致谢

2 个答案:

答案 0 :(得分:2)

AbstractPluginManager主要用于验证和过滤插件。您可以创建类,在验证时,您可以传递特定的配置,使过滤器或验证器可重复使用。

您正在寻找的可能是abstract factory。您注册工厂一次,它可以为您创建服务。在您的情况下,具有一组特定的依赖项。

interface I{}
class A implements I{}
class B implements I{}

class MyAbstractFactory implements AbstractFactoryInterface
{
    public function canCreate(ContainerInterface $container, $requestedName)
    {
        return in_array('I', class_implements($requestedName), true);
    }

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        return new $requestedName(
            $container->get(DependencyFoo::class),
            $container->get(DependencyBar::class)
        );
    }
}

// config/autoload/dependencies.global.php
return [
    'dependencies' => [
        'factories' => [
            // ...
        ],

        'abstract_factories' => [
            MyAbstractFactory::class,
        ],
    ],
];

如果每个类的不同,你也可以发疯并使用反射来检测依赖关系,但这会增加很多开销。我认为创建单独的工厂更容易,更易于维护。然后有zend-expressive-tooling这是一个可以创建工厂,处理程序和中间件的cli工具。

答案 1 :(得分:0)

/*Getting I concrete implementations via the plugin manager will ensure the implementation of the I interface*/   
class IPluginManager extends AbstractPluginManager
{
   protected $instanceOf = I::class;

   public function getIConcreteImplementations()
   {
       $concreteImpl = [];
       foreach(array_keys($this->factories) as $key)
       {
           $concreteImpl[] = $this->get($key);
       }

       return $concreteImpl;
    }
}
/*IPluginManagerFactory*/
class TransactionSourcePluginManagerFactory
{
  const CONFIG_KEY = 'i-implementations-config-key';

public function __invoke(ContainerInterface $container, $name, array $options = null)
{
    $pluginManager = new IPluginManager($container, $options ?: []);

    // If this is in a zend-mvc application, the ServiceListener will inject
    // merged configuration during bootstrap.
    if ($container->has('ServiceListener')) {
        return $pluginManager;
    }

    // If we do not have a config service, nothing more to do
    if (! $container->has('config')) {
        return $pluginManager;
    }

    $config = $container->get('config');

    // If we do not have validators configuration, nothing more to do
    if (! isset($config[self::CONFIG_KEY]) || ! 
      is_array($config[self::CONFIG_KEY])) {
        return $pluginManager;
    }

    // Wire service configuration for validators
    (new Config($config[self::CONFIG_KEY]))->configureServiceManager($pluginManager);

    return $pluginManager;
  }
}
/*In the ConfigProvider of the module or global config*/
class ConfigProvider
{
/**
 * Returns the configuration array
 *
 * To add a bit of a structure, each section is defined in a separate
 * method which returns an array with its configuration.
 *
 */
   public function __invoke() : array
   {
    return [
        'dependencies' => $this->getDependencies(),
        'routes' => $this->getRoutes(),
        'i-implementations-config-key' => $this->getIConcreteImplementations(),
    ];
   }
   public function getIConcreteImplementations() : array
   {
    return [
        'factories' => [
            A::class => AFactory::class,
            B::class => InvokableFactory::class,
        ],
    ];
   }
}
/*I can now be sure that I am injecting an array of I implementations into my Service*/
class ServiceFactory
{
    public function __invoke(ContainerInterface $container) : Service
    {
        $pluginManager = $container->get(IPluginManager::class);

        $impl = $pluginManager->getIConcreteImplementations();

        return new Service($impl);
    }
 }