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
56 stars 24 forks source link

Optional include of config/laminas-cli.php or something similar #78

Open rarog opened 3 years ago

rarog commented 3 years ago

Feature Request

Q A
New Feature yes
RFC yes
BC Break no

Summary

For some of the logic of console commands it would be good and safe to be able to include an additional config file to override module options. In my specific case I use console command to clean caches and in case some of the module listener caches are horribly invalid, my command would break. By including additional config file, I could implement a safety net by including following lines, which would prevent those caches to be loaded even if they are enabled globally.

return [
    'module_listener_options' => [
        'config_cache_enabled' => false,
        'module_map_cache_enabled' => false,
    ],
];
weierophinney commented 3 years ago

There's a way to do this as of 1.1.0.

As of 1.1.0, you can specify the --container global option and point it at an alternate file that will return your PSR-11 container. So, as an example, you could do something like this:

<?php // in config/clear-cache-container.php

use Laminas\Mvc\Service\ServiceManagerConfig;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\ArrayUtils;

$config        = require __DIR__ . '/application.config.php';
$devConfig = __DIR__ . '/development.config.php';
if (file_exists($devConfig)) {
    $devConfig = include $devConfig;
    $config        = ArrayUtils::merge($config, $devConfig);
}

$config['modules_listener_options'] => [
    'config_cache_enabled' => false,
    'module_map_cache_enabled' => false,
];

$container = new ServiceManager();
(new ServiceManagerConfig($config['service_manager'] ?? []))->configureServiceManager($container);
$container->setService('ApplicationConfig', $config);
$container->get('ModuleManager')->loadModules();

return $container;

Then, when calling laminas-cli for such operations, pass the location of this file:

$ ./vendor/bin/laminas-cli --container ./config/clear-cache-container.php {command...}
froschdesign commented 3 years ago

The example must be added to the documentation.

rarog commented 3 years ago

A small correction to above code, that I successfully implemented in my project. Thx for the help!


<?php // in config/clear-cache-container.php

use Laminas\Mvc\Service\ServiceManagerConfig;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Stdlib\ArrayUtils;

$config = require __DIR__ . '/application.config.php';
$devConfig = __DIR__ . '/development.config.php';
if (file_exists($devConfig)) {
    $devConfig = include $devConfig;
    $config = ArrayUtils::merge($config, $devConfig);
}

$configDisableCaches = [
    'modules_listener_options' => [
        'config_cache_enabled' => false,
        'module_map_cache_enabled' => false,
    ]
];
$config = ArrayUtils::merge($config, $configDisableCaches);

$container = new ServiceManager();
(new ServiceManagerConfig($config['service_manager'] ?? []))->configureServiceManager($container);
$container->setService('ApplicationConfig', $config);
$container->get('ModuleManager')->loadModules();

return $container;
`
froschdesign commented 3 years ago

This feature should be added to laminas-cli, the current solution is only a workaround.