php - 使用 Zend 插件管理器实现工厂模式
问题描述
该函数createInstance(String $instance) : InstanceInterface{}
应返回IntanceInterface
. 我不想使用 switch 语句,因为实例太多(不同的类)。虽然一位朋友告诉我 Zend-Framework 3 中的插件管理器。我读到了服务管理器,因为它们似乎是相关的。
代替插件管理器,服务管理器有很好的文档和描述。
我认为我以错误的方式实现它,因为我收到一个 php 错误:
Deprecated: Zend\ServiceManager\AbstractPluginManager::__construct now expects a Interop\Container\ContainerInterface instance representing the parent container; please update your code in /website/admin/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php on line 85
Warning: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time in /website/admin/vendor/zendframework/zend-session/src/Config/SessionConfig.php on line 148
实例工厂
protected $instancePluginManager;
public function __construct(InstancePluginManager $instacePluginManager)
{
$this->instancePluginManager = $instancePluginManager;
}
public function createInstance(string $instance) :InstanceInterface
{
$this->instacePluginManager->get($instance);
}
InstancePluginManager
class InstancePluginManager extends AbstractPluginManager
{
protected $instanceOf = InstanceInterface::class;
/**
* @var string[]|callable[] Default factories
*/
protected $factories = [
A::class => InvokableFactory::class,
B::class => InvokableFactory::class,
C::class => InvokableFactory::class,
D::class => InvokableFactory::class,
E::class => InvokableFactory::class,
F::class => InvokableFactory::class,
G::class => InvokableFactory::class,
H::class => InvokableFactory::class,
I::class => InvokableFactory::class,
J::class => InvokableFactory::class,
];
public function validate($instance)
{
if (! $instance instanceof $this->instanceOf) {
throw new InvalidServiceException(sprintf(
'Chart of type "%s" is invalid; must implement %s',
(is_object($instance) ? get_class($instance) : gettype($instance)),
$this->instanceOf
));
}
}
}
InstanceFactoryFactory(Zend 工厂)
class InstanceFactoryFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$instancePluginManager = $container->get(InstancePluginManager::class);
return new InstanceFactory($instancePluginManager);
}
}
模块.config.php
return [
'service_manager' => [
'factories' => [
InstanceFactory::class => InstanceFactoryFactory::class,
InstancePluginManager::class => InvokableFactory::class,
],
],
];
解决方案
改变
InstancePluginManager::class => InvokableFactory::class
至
InstancePluginManager::class => function (ContainerInterface $container, $requestedName) {
return new InstancePluginManager($container);
}
InvokableFactory 正在尝试创建不带参数的 InstancePluginManager。
推荐阅读
- xml - 如何在 Nuxt.js 中创建 Google 新闻提要 (xml)?
- postgresql - 如何删除postgreSQL中相差小于10秒的重复数据
- c++ - Boost状态图:尝试实现正交状态的内部初始子状态但编译失败
- ios - React native 0.63.4 ios 14 无静态图像
- azure-functions - azure functionapp 在输出 blob 绑定上设置元数据
- vhdl - 如何在VHDL中的每个时钟周期设置一个标志
- regex - 正则表达式 - 仅在字符串的一部分中更改逗号
- python - 从类传递函数而不启动它python
- php - ACF“acf/save_post”挂钩中的 WooCommerce WC_Product set_category_ids() 方法存在问题
- python - Beautifulsoup:如何迭代表