Open rarog opened 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...}
The example must be added to the documentation.
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;
`
This feature should be added to laminas-cli, the current solution is only a workaround.
Feature Request
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.