簡體   English   中英

Solarium 客戶端中參數 2 的類型錯誤

[英]TypeError for Argument 2 in Solarium Client

我正在嘗試使用 Search_api_solr v4.1、Solarium v6.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();
}

有沒有其他人遇到過這個問題或知道解決這個問題的方法?

當您在new Client()創建日光浴室客戶端時,第二個參數希望您提供一個 class 實現Psr\EventDispatcher\EventDispatcherInterface 但是,在您的使用語句中,您正在創建的 EventDispatcher 是Symfony\Component\EventDispatcher\EventDispatcher

將 use 語句更改為 PSR 事件調度程序,您應該沒問題。

當您深入研究 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。 這個接口來自這個 package https://packagist.org/packages/psr/event-dispatcher所以我會嘗試一個composer require psr/event-dispatcher來修復它。

編輯:您的 symfony 事件調度程序版本已過時。 更新到 5.3 以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM