[英]ZF3: ServiceNotFoundException while creating a class with Abstract Factory registered in ServiceManager
I got problem with the Abstract Factories example. 我对“ 抽象工厂”示例有疑问 。
I get ServiceNotFoundException
while creating a class with Abstract Factory registered in the ServiceManager. 使用在ServiceManager中注册的Abstract Factory创建类时,出现
ServiceNotFoundException
。
First I download zend-servicemanager with composer 首先,我使用composer下载zend-servicemanager
composer require zendframework/zend-servicemanager
Then I run the ServiceManager example in the single file (for simplicity). 然后,我在单个文件中运行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;
Class that should be obtained with ServiceManager. 应该通过ServiceManager获取的类。
class A
{
public $text;
public function __construct()
{
$this->text = "Default text";
}
}
I use MyAbstractFactory from documentation. 我从文档中使用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();
}
}
I create ServiceManager with registered 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()
]
*/
]);
Finally I try to obtain the instance of class A. 最后,我尝试获取类A的实例。
$a = $serviceManager->get(A::class);
var_dump($a);
I get 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?
Fatal error: Uncaught Zend\\ServiceManager\\Exception\\ServiceNotFoundException: Unable to resolve service "A" to a factory; are you certain you provided it during configuration?
with the stack trace 与堆栈跟踪
.\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
My formulation of the problem was wrong. 我对这个问题的表述是错误的。 @rkeet's comment made it clear.
@rkeet的评论很清楚。
As my objective was the working example of the Abstract Factory registration within ServiceManager, I post the single-file script where canCreate()
is simplified just to check if the class exists. 由于我的目标是在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()})";
}
?>
As @rkeet pointed, if someone needs the original example working, class A
should implement Traversable . 正如@rkeet所指出的,如果有人需要原始示例工作,则
A
类应实现Traversable 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.