laminas-cli icon indicating copy to clipboard operation
laminas-cli copied to clipboard

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

Open rarog opened this issue 4 years ago • 4 comments

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,
    ],
];

rarog avatar Jun 05 '21 23:06 rarog

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...}

weierophinney avatar Jun 07 '21 14:06 weierophinney

The example must be added to the documentation.

froschdesign avatar Jun 07 '21 20:06 froschdesign

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;
`

rarog avatar Jun 08 '21 07:06 rarog

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

froschdesign avatar Aug 24 '21 14:08 froschdesign