Laminas-servicemanager: initializer config won't work

I’m trying to use initializers in my service manager config in module.config.php. This is my config:

<?php

/**
 * @see       https://github.com/laminas/laminas-mvc-skeleton for the canonical source repository
 *
 * @copyright https://github.com/laminas/laminas-mvc-skeleton/blob/master/COPYRIGHT.md
 * @license   https://github.com/laminas/laminas-mvc-skeleton/blob/master/LICENSE.md New BSD License
 */

declare(strict_types=1);

namespace Worker;

use Laminas\ServiceManager\Factory\InvokableFactory;
use Worker\Command\HelloCommand;
use Worker\Command\PublishCommand;
use Worker\Command\QueueCommandInitializer;
use Worker\Service\ElasticService;
use Worker\Service\ElasticServiceFactory;
use Worker\Service\RabbitmqService;
use Worker\Service\RabbitmqServiceFactory;

return [
    'laminas-cli'     => [
        'commands' => [
            'app:hello'   => HelloCommand::class,
            'app:publish' => PublishCommand::class,
        ],
    ],
    'service_manager' => [
        'factories' => [
            HelloCommand::class    => InvokableFactory::class,
            PublishCommand::class  => InvokableFactory::class,
            RabbitmqService::class => RabbitmqServiceFactory::class,
            ElasticService::class => ElasticServiceFactory::class
        ],
        'initializers' => [
            new QueueCommandInitializer()
        ]
    ],
];

The QueueCommandInitializer looks like this:

<?php

/**
 * @see       https://github.com/laminas/laminas-mvc-skeleton for the canonical source repository
 *
 * @copyright https://github.com/laminas/laminas-mvc-skeleton/blob/master/COPYRIGHT.md
 * @license   https://github.com/laminas/laminas-mvc-skeleton/blob/master/LICENSE.md New BSD License
 */

declare(strict_types=1);

namespace Worker\Command;

use Laminas\ServiceManager\Initializer\InitializerInterface;
use Psr\Container\ContainerInterface;
use Worker\Service\RabbitmqService;

class QueueCommandInitializer implements InitializerInterface
{
    public function __invoke(ContainerInterface $container, HelloCommand $instance)
    {
        if (!$instance instanceof QueueCommandInterface) {
            return;
        }
        $instance->setQueue($container->get(RabbitmqService::class));
    }
}

The QueueCommandInterface is an empty interface.

When I try to run my app I get the following error:

Fatal error: Uncaught Error: Class "Worker\Command\QueueCommandInitializer" not found in /Users/<redacted>/module/Worker/config/module.config.php:38

And I can’t figure out why. Any ideas?

Use the delegators instead: Migration Guide - laminas-servicemanager - Laminas Docs

The class name and the namespace looks good, please check the filename.

Filename looks good, too I think: module/Worker/src/Command/QueueCommandInitializer.php

The class QueueCommandInitializer is not compatible with the interface of Laminas\ServiceManager\Initializer\InitializerInterface:

It must be:

public function __invoke(ContainerInterface $container, $instance);

Apart from that, everything works for me.

Thanks! How would I hint my IDE so it knows what methods are available on that instance, though? A DocBlock…?

EDIT: Ah. You mention the methods in the interface.

<?php

declare(strict_types=1);

namespace Worker\Command;

use Worker\Service\RabbitmqService;

interface QueueCommandInterface
{
    public function setQueue(RabbitmqService $queue): void;
}