Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Servicemanager Laravel Package

laminas/laminas-servicemanager

Powerful dependency injection and service container for PHP. Manage factories, abstract factories, delegators, aliases, and shared services, with PSR-11 interoperability and robust configuration for complex applications.

View on GitHub
Deep Wiki
Context7

Plugin managers

Plugin managers are specialized service managers, typically used to create homogeneous objects of a specific type.

Because a plugin manager extends a service manager, it works the same and can be configured similarly. It provides a separation of concerns (it will be used in specific contexts), and provides additional instance validation.

Laminas components extensively use plugin managers to create services that share common functionalities. For instance, all validator services are specified inside a specialized ValidatorPluginManager.

Creating a plugin manager

To create a plugin manager, you first need to create a new class that extends Laminas\ServiceManager\AbstractPluginManager:

class ValidatorPluginManager extends AbstractPluginManager
{
    protected $instanceOf = ValidatorInterface::class;
}

The $instanceOf variable specifies a class/interface type that all instances retrieved from the plugin manager must fulfill. If an instance created by the plugin manager does not match, a Laminas\ServiceManager\Exception\InvalidServiceException exception will be thrown.

Most of the time, this shortcut is enough. However if you have more complex validation rules, you can override the validate() method:

class ValidatorPluginManager extends AbstractPluginManager
{
    public function validate($instance)
    {
        if ($instance instanceof Foo || $instance instanceof Bar) {
            return;
        }

        throw new InvalidServiceException('This is not a valid service!');
    }
}

Configuring a plugin manager

A plugin manager requires that you pass a parent service manager (typically, the application's service manager) as well as service configuration. Service configuration follows the exact same pattern as for a normal service manager; refer to the configuring the service manager section for details.

Because a plugin manager is often a service itself, we recommend you to register the plugin manager as part of the general service manager, as shown below:

$serviceManager = new ServiceManager([
    'factories' => [
        ValidatorPluginManager::class => function(ContainerInterface $container, $requestedName) {
            return new ValidatorPluginManager($container, [
                'factories' => [
                    StringLengthValidator::class => InvokableFactory::class,
                ],
            ]);
        },
    ],
]);

// Get the plugin manager:

$pluginManager = $serviceManager->get(ValidatorPluginManager::class);

// Use the plugin manager

$validator = $pluginManager->get(StringLengthValidator::class);

When inside the context of the factory of a service created by a plugin manager, the passed container will not be the plugin manager, but the parent service manager instead. If you need access to other plugins of the same type, you will need to fetch the plugin manager from the container:

function ($container, $name, array $options = []) {
    $validators = $container->get(ValidatorPluginManager::class);
    // ...
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4