AbstractPluginManager 主要用于验证和过滤插件。您可以创建类,并且在验证时,您可以传递使过滤器或验证器可重用的特定配置。
您正在寻找的可能是一个抽象工厂。您注册工厂一次,它可以为您创建服务。在您的情况下,具有一组特定的依赖项。
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 工具,可以创建工厂、处理程序和中间件。