user3717206
user3717206

Reputation: 75

TypeError for Argument 2 in Solarium Client

I'm trying to start a client using Search_api_solr v4.1, Solarium v6.0 and symfony/event-dispatcher v3.4.47 but I keep getting a 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

I'm not quite sure why it's expecting an instance of Psr\EventDispatcher\EventDispatcherInterface when all the documentation on solarium says to use Symfony\Component\EventDispatcher\EventDispatcher.

My adapter and event Dispatcher is as below

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

Has anyone else ran into this issue or knows a fix for this?

Upvotes: 0

Views: 930

Answers (2)

balintpekker
balintpekker

Reputation: 1844

When you are creating the solarium client at new Client() the second parameter expects you to give a class implementing Psr\EventDispatcher\EventDispatcherInterface. However, in your use statement the EventDispatcher you are creating is Symfony\Component\EventDispatcher\EventDispatcher.

Change the use statement to the PSR event dispatcher and you should be fine.

Upvotes: 1

jona303
jona303

Reputation: 1568

When you dig into the Symfony source you can view this :

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*/);
    }
}

So your issue is that interface_exists(PsrEventDispatcherInterface::class) returns false. THis interface comes from this package https://packagist.org/packages/psr/event-dispatcher so I would try a composer require psr/event-dispatcher to fix it.

EDIT : Your version of symfony's event dispatcher is outdated. Update to 5.3 to fix the issue.

Upvotes: 0

Related Questions