繁体   English   中英

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

[英]ZF3: ServiceNotFoundException while creating a class with Abstract Factory registered in ServiceManager

我对“ 抽象工厂”示例有疑问

使用在ServiceManager中注册的Abstract Factory创建类时,出现ServiceNotFoundException

首先,我使用composer下载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();
  }
}

我用注册的抽象工厂创建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? 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

我对这个问题的表述是错误的。 @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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM