ZF3转换器远程装载机工厂无法解决服务问题

时间:2017-07-29 19:54:22

标签: php zend-framework3 zend-translate zend-servicemanager


使用MVCTranslator时出现问题,翻译存储在数据库中。

我将翻译配置为使用remote_translation,但我无法为自定义加载程序创建工厂。 我的文件如下:

module.config.php

'translator' => [                
    'locale' => 'en_US',
    'translation_file_patterns' => [
        [
            'type'     => 'phparray',
            'base_dir' => getcwd() .  '/data/language',
            'pattern'  => '%s.php',
            'text_domain' => 'default',
        ],             
    ],        
    'remote_translation' => [
        [                
            'type' => Model\DatabaseTranslationLoader::class,
            'text_domain' => 'default',                
        ]
    ],

],

Module.php

namespace MyNamespace;

use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    /*
    ....
    ...
    ....
    */      

    public function getControllerConfig()
    {        
        return [
            'factories' => [
                Controller\MyController::class => function($container) {                   

                    $translator = $container->get('MvcTranslator');                    

                    $translator->getPluginManager()->setFactory(Translator\DatabaseTranslationLoader::class, Factory\DatabaseTranslationLoaderFactory::class); 

                    return new Controller\MyController(
                        $translator
                    );
                },
            ],
        ];
    }

}

翻译\ DatabaseTranslationLoader.php

namespace MyNamespace\Translator;

use Zend\Db\Adapter\Adapter as DbAdapter;
use Zend\I18n\Translator\Loader\RemoteLoaderInterface;

class DatabaseTranslationLoader implements RemoteLoaderInterface
{
    protected $dbAdapter;

    public function __construct(DbAdapter $adapter)
    {        
        $this->dbAdapter = $adapter;
    }

    public function load($locale, $filename)
    {
        // Database operations
    }

厂\ DatabaseTranslationLoaderFactory.php

namespace MyNamespace\Factory\DatabaseTranslation;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;   

class DatabaseTranslationLoaderFactory implements FactoryInterface
{       
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $dbAdapter = $container->get(Zend\Db\Adapter\AdapterInterface:class);           

        return new Translator\DatabaseTranslationLoader($dbAdapter);
    }
}

使用此配置,当我尝试在DatabaseTranslationLoaderFactory中检索数据库适配器时,从服务管理器收到异常:

    无法解析服务" Zend \ Db \ Adapter \ Adapter"到工厂;你确定你在配置期间提供了吗?

看起来工厂有一个空容器(一个新的ServiceManager实例?)。

我做错了什么? 有谁知道怎么做? 我会很感激任何建议。

编辑:

如上所述,它看起来像是DatabaseTranslationLoaderFactory中的一个新的serviceMenager实例。
当我尝试做同样的错误:

$container->get('router')

$container->get('request')

2 个答案:

答案 0 :(得分:1)

首先,我看到你在打电话时弄错了。你调用变量$conteiner。它应该是$container

$conteiner->get('Zend\Db\Adapter\Adapter')

要检索数据库适配器服务,请使用

$container->get(Zend\Db\Adapter\AdapterInterface::class);

而不是

$container->get('Zend\Db\Adapter\Adapter')

答案 1 :(得分:1)

经过几个小时的搜索,我找到了解决问题的方法 该问题的解释见于 Zend \ I18n \ Translator \ Translator.php

public function setPluginManager(LoaderPluginManager $pluginManager)
{
    $this->pluginManager = $pluginManager;

    return $this;
}

public function getPluginManager()
{
    if (! $this->pluginManager instanceof LoaderPluginManager) {
        $this->setPluginManager(new LoaderPluginManager(new ServiceManager));
    }

    return $this->pluginManager;
}

从上面可以看出,getPluginManager默认使用新的ServiceManager实例创建一个新的LoaderPluginManager实例。

在我的情况下,要访问DatabaseTranslationLoaderFactory中的ServiceManager,我必须首先使用setPluginManager方法,如下所示:

Module.php

namespace MyNamespace;

use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    /*
    ....
    ...
    ....
    */      

    public function getControllerConfig()
    {        
        return [
            'factories' => [
                Controller\MyController::class => function($container) {                   

                    $translator = $container->get('MvcTranslator');  

                    // This line solved my problem
                    $translator->setPluginManager(new \Zend\I18n\Translator\LoaderPluginManager($container));

                    $translator->getPluginManager()->setFactory(Translator\DatabaseTranslationLoader::class, Factory\DatabaseTranslationLoaderFactory::class); 

                    return new Controller\MyController(
                        $translator
                    );
                },
            ],
        ];
    }
}

我不知道这个解决方案是否正确,但它确实有效。
也许它会帮助某人:)