Cli\Command: provide Config()

This makes it feel more like action controllers

fixes #6954
This commit is contained in:
Thomas Gelf 2014-08-19 10:27:26 +02:00
parent f67d273bbd
commit a38d71f17c
1 changed files with 50 additions and 0 deletions

View File

@ -7,6 +7,7 @@ namespace Icinga\Cli;
use Icinga\Cli\Screen;
use Icinga\Util\Translator;
use Icinga\Cli\Params;
use Icinga\Application\Config;
use Icinga\Application\ApplicationBootstrap as App;
use Exception;
@ -23,6 +24,10 @@ abstract class Command
protected $commandName;
protected $actionName;
private $config;
private $configs;
protected $defaultActionName = 'default';
public function __construct(App $app, $moduleName, $commandName, $actionName, $initialize = true)
@ -41,6 +46,51 @@ abstract class Command
}
}
public function Config($file = null)
{
if ($this->isModule()) {
return $this->getModuleConfig($file);
} else {
return $this->getMainConfig($file);
}
}
private function getModuleConfig($file = null)
{
if ($file === null) {
if ($this->config === null) {
$this->config = Config::module($this->moduleName);
}
return $this->config;
} else {
if (! array_key_exists($file, $this->configs)) {
$this->configs[$file] = Config::module($this->moduleName, $file);
}
return $this->configs[$file];
}
}
private function getMainConfig($file = null)
{
if ($file === null) {
if ($this->config === null) {
$this->config = Config::app();
}
return $this->config;
} else {
if (! array_key_exists($file, $this->configs)) {
$this->configs[$file] = Config::module($module, $file);
}
return $this->configs[$file];
}
return $this->config;
}
public function isModule()
{
return substr(get_class($this), 0, 14) === 'Icinga\\Module\\';
}
public function setParams(Params $params)
{
$this->params = $params;