laminas / laminas-cli

Console command runner, exposing commands written in Laminas MVC and Mezzio components and applications
https://docs.laminas.dev/laminas-cli
BSD 3-Clause "New" or "Revised" License
55 stars 22 forks source link

feature: container option #70

Closed boesing closed 3 years ago

boesing commented 3 years ago
Q A
Documentation yes
BC Break no
New Feature yes

Description

With this feature, one can provide --container=<path> to provide a PSR-11 container which is not in a file called config/container.php.

The original idea behind this feature is, that I can create a generic PSR-11 container file which then parses the current project for a file ConfigProvider so it can automagically provide a container for that component without having to copy & paste that file to all components while creating a directory called config which has to be added to my global .gitignore.

<?php
declare(strict_types=1);

if (!file_exists('composer.json')) {
    throw new RuntimeException('Missing `composer.json`!');
}

$composer = json_decode(file_get_contents('composer.json'), true, 512, JSON_THROW_ON_ERROR);
$configProviderClassName = $composer['extra']['laminas']['config-provider'] ?? '';
if ($configProviderClassName === '') {
    throw new RuntimeException('There is no laminas `config-provider` configuration within the `composer.json`!');
}

if (!class_exists($configProviderClassName)) {
    throw new RuntimeException('Laminas `config-provider` configuration within the `composer.json` contains invalid class name!');
}

$configProvider = new $configProviderClassName;
if (!is_callable($configProvider)) {
    throw new RuntimeException('Laminas `config-provider` is not callable!'); 
}

$config = $configProvider();
$dependencies = $config['dependencies'] ?? [];
$serviceManager = new \Laminas\ServiceManager\ServiceManager($dependencies);
$serviceManager->setService('config', $config);
$serviceManager->setAlias('Config', 'config');

return $serviceManager;

This actually works fine in almost all (laminas) components which provide a config-provider information within the composer.json. I can re-use that file stored wherever I like tho and have a configured service manager (until the components has it either required as production or dev dependency).

Imho, thats super helpful - may I should add this somewhere to the docs aswell?

Thoughts, @michalbundyra @weierophinney?

Fixes #63

boesing commented 3 years ago

This PR is based on #69 and thus contains changes from that PR.

boesing commented 3 years ago

@michalbundyra I've reverted the changes you noted. If the changes are fine for you and if @weierophinney gives me the okay, I'd add this to 1.1.0 and ship it.