首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Zend插件管理器实现工厂模式

使用Zend插件管理器实现工厂模式
EN

Stack Overflow用户
提问于 2019-04-30 00:58:39
回答 1查看 139关注 0票数 0

函数createInstance(String $instance) : InstanceInterface{}应该返回IntanceInterface的一个实例。我不想使用switch语句,因为有太多的实例(不同的类)。虽然一个朋友告诉我Zend-Framework3中的插件管理器,但我读到了服务管理器,因为它们似乎是相关的。

https://olegkrivtsov.github.io/using-zend-framework-3-book/html/en/Website_Operation/Plugin_Managers.html

与插件管理器不同,Service Manager有很好的文档记录和描述。

我想我实现它的方式是错误的,因为我得到了一个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

InstanceFactory

代码语言:javascript
复制
protected $instancePluginManager;

public function __construct(InstancePluginManager $instacePluginManager)
    {
        $this->instancePluginManager = $instancePluginManager;
    }


public function createInstance(string $instance) :InstanceInterface
    {

       $this->instacePluginManager->get($instance);
}

InstancePluginManager

代码语言:javascript
复制
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 Factory)

代码语言:javascript
复制
class InstanceFactoryFactory implements FactoryInterface
{

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $instancePluginManager = $container->get(InstancePluginManager::class);

        return new InstanceFactory($instancePluginManager);
    }
}

module.config.php

代码语言:javascript
复制
return [
'service_manager' => [
        'factories' => [
InstanceFactory::class => InstanceFactoryFactory::class,
InstancePluginManager::class => InvokableFactory::class,

],
],
];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-03 06:29:30

变化

代码语言:javascript
复制
InstancePluginManager::class => InvokableFactory::class

代码语言:javascript
复制
InstancePluginManager::class => function (ContainerInterface $container, $requestedName) {
    return new InstancePluginManager($container);
}

InvokableFactory正在尝试创建不带参数的InstancePluginManager。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55907804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档