【问题标题】:TypeError for Argument 2 in Solarium ClientSolarium 客户端中参数 2 的类型错误
【发布时间】:2021-08-26 23:33:30
【问题描述】:

我正在尝试使用 Search_api_solr v4.1、Solarium v​​6.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

当日光浴室的所有文档都说要使用 Symfony\Component\EventDispatcher\EventDispatcher 时,我不太确定为什么它会期待 Psr\EventDispatcher\EventDispatcherInterface 的实例。

我的适配器和事件调度器如下

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();
}

有没有其他人遇到过这个问题或知道解决这个问题的方法?

【问题讨论】:

    标签: symfony drupal drupal-8 solarium


    【解决方案1】:

    当您深入研究 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。这个接口来自这个包https://packagist.org/packages/psr/event-dispatcher所以我会尝试一个composer require psr/event-dispatcher来修复它。

    编辑:你的 symfony 事件调度器版本已经过时了。更新到 5.3 以解决此问题。

    【讨论】:

    • 我也这么认为,并且已经 composer require psr/event-dispatcher 但这根本没有改变问题
    • 安装的是哪个版本?
    • 为 psr/event-dispatcher 安装了 1.0 版
    【解决方案2】:

    当您在new Client() 创建日光浴室客户端时,第二个参数要求您提供一个实现Psr\EventDispatcher\EventDispatcherInterface 的类。但是,在您的 use 语句中,您创建的 EventDispatcher 是 Symfony\Component\EventDispatcher\EventDispatcher

    把use语句改成PSR事件分派器应该没问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-03
      • 2022-01-04
      • 1970-01-01
      • 2018-11-29
      • 2019-08-05
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      相关资源
      最近更新 更多