我正在尝试使用Search_api_solr v4.1、SolariaV6.0和symfony/event-dispatcher v3.4.47启动一个客户端,但我一直收到一个TypeError。
TypeError: Argument 2 passed to Solarium\Core\Client\Client::__construct()
must be an instance of Psr\EventDispatcher\EventDispatcherInterface,
instance of Symfony\Component\EventDispatcher\EventDispatcher given我不太明白为什么它需要一个Psr\EventDispatcher\EventDispatcherInterface的实例,而所有关于solarium的文档都说要使用Symfony\Component\EventDispatcher\EventDispatcher.
我的适配器和事件分派器如下所示
use Solarium\Client;
use Solarium\Core\Client\Adapter\Curl;
use Symfony\Component\EventDispatcher\EventDispatcher;
function get_search_query() {
$adapter = new Curl();
$eventDispatcher = new EventDispatcher();
$config = ['endpoint' => ['localhost' => [
'host' => $id,
'port' => $port,
'path' => '/',
'collection' => '$core',],],];
$search = new Client($adapter, $eventDispatcher, $config);
$query = $search->createSelect();
}有没有其他人遇到过这个问题,或者知道解决这个问题的方法?
发布于 2021-07-03 05:04:20
当您在new Client()上创建日光客户端时,第二个参数要求您提供一个实现Psr\EventDispatcher\EventDispatcherInterface的类。但是,在use语句中,您要创建的EventDispatcher是Symfony\Component\EventDispatcher\EventDispatcher。
将use语句更改为PSR事件分派器,就可以了。
发布于 2021-06-10 15:37:35
当您深入研究Symfony源代码时,可以查看以下内容:
if (interface_exists(PsrEventDispatcherInterface::class)) {
/**
* Allows providing hooks on domain-specific lifecycles by dispatching events.
*/
interface EventDispatcherInterface extends PsrEventDispatcherInterface
{
/**
* Dispatches an event to all registered listeners.
*
* For BC with Symfony 4, the $eventName argument is not declared explicitly on the
* signature of the method. Implementations that are not bound by this BC constraint
* MUST declare it explicitly, as allowed by PHP.
*
* @param object $event The event to pass to the event handlers/listeners
* @param string|null $eventName The name of the event to dispatch. If not supplied,
* the class of $event should be used instead.
*
* @return object The passed $event MUST be returned
*/
public function dispatch($event/*, string $eventName = null*/);
}
} else {
/**
* Allows providing hooks on domain-specific lifecycles by dispatching events.
*/
interface EventDispatcherInterface
{
/**
* Dispatches an event to all registered listeners.
*
* For BC with Symfony 4, the $eventName argument is not declared explicitly on the
* signature of the method. Implementations that are not bound by this BC constraint
* MUST declare it explicitly, as allowed by PHP.
*
* @param object $event The event to pass to the event handlers/listeners
* @param string|null $eventName The name of the event to dispatch. If not supplied,
* the class of $event should be used instead.
*
* @return object The passed $event MUST be returned
*/
public function dispatch($event/*, string $eventName = null*/);
}
}所以你的问题是interface_exists(PsrEventDispatcherInterface::class)返回false。THis接口来自这个包https://packagist.org/packages/psr/event-dispatcher,所以我会尝试使用composer require psr/event-dispatcher来修复它。
编辑:您的symfony事件调度程序版本已过时。请更新到5.3以解决此问题。
https://stackoverflow.com/questions/67913020
复制相似问题