ZF3:ServiceNotFoundException并在ServiceManager中注册抽象工厂创建类时

时间:2018-09-30 17:20:42

标签: zend-framework zend-framework3 abstract-factory zend-servicemanager

我对Abstract Factories示例有疑问。

在ServiceManager中注册了Abstract Factory的类创建类时,我得到ServiceNotFoundException

首先,我用作曲家下载zend-servicemanager

composer require zendframework/zend-servicemanager

然后,我在单个文件中运行ServiceManager示例(为简单起见)。

<?php
require_once 'vendor/autoload.php';

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

应通过ServiceManager获得的类。

class A
{
    public $text;

    public function __construct() 
    {
        $this->text = "Default text";
    }
}

我使用文档中的MyAbstractFactory。

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

  public function __invoke(ContainerInterface $container, 
                           $requestedName,
                           array $options = null)
  {
    return $requestedName();
  }
}

我用注册的Abstract Factory创建ServiceManager。

$serviceManager = new ServiceManager([
    // Neither works    
  //'abstract_factories' => array('MyAbstractFactory')
  'abstract_factories' => array( new MyAbstractFactory() )
  //'abstract_factories' => array( MyAbstractFactory::class )
  /*  
    'abstract_factories' => [
        MyAbstractFactory::class => new MyAbstractFactory()
    ]
  */  
]);

最后,我尝试获取A类的实例。

$a = $serviceManager->get(A::class);
var_dump($a);

我明白了 Fatal error: Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException: Unable to resolve service "A" to a factory; are you certain you provided it during configuration? 与堆栈跟踪

.\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(763): Zend\ServiceManager\ServiceManager->getFactory('A') #1
.\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(200): Zend\ServiceManager\ServiceManager->doCreate('A') #2
.\script.php(53): Zend\ServiceManager\ServiceManager->get('A') #3

1 个答案:

答案 0 :(得分:0)

我对问题的表述是错误的。 @rkeet的评论很清楚。

由于我的目标是在ServiceManager中进行抽象工厂注册的工作示例,因此我发布了单文件脚本,其中canCreate()被简化,只是为了检查该类是否存在。

<?php
// composer require zendframework/zend-servicemanager

require_once 'vendor/autoload.php';

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


class A
{
    public $text;

    public function __construct() 
    {
        $this->text = "Default text";
    }
}


class MyAbstractFactory implements AbstractFactoryInterface
{
    public function canCreate(ContainerInterface $container, $requestedName)
    {
        return class_exists($requestedName);
    }

    public function __invoke(ContainerInterface $container, 
                             $requestedName,
                             array $options = null)
    {
        return new $requestedName();
    }
}


$serviceManager = new ServiceManager([
  //'abstract_factories' => array('MyAbstractFactory') // works
  //'abstract_factories' => array( new MyAbstractFactory() ) // works
  'abstract_factories' => array( MyAbstractFactory::class ) // also works
]);


try {
    $a = $serviceManager->get(A::class);
    var_dump($a);
    echo "<br/>\n";
    $b = $serviceManager->get(B::class);
    var_dump($b);
} catch (Exception $e) {
    echo $e->getMessage() . "<br/>\n";
    echo "{$e->getFile()}  ({$e->getLine()})";
}
?>

正如@rkeet所指出的,如果有人需要原始示例工作,则类A应该实现Traversable