Make retrieval of module config static

This commit is contained in:
Eric Lippmann 2013-07-12 12:11:59 +02:00
parent 78cbeadff2
commit df1e595604
3 changed files with 55 additions and 39 deletions

View File

@ -2,6 +2,7 @@
namespace Icinga\Application;
use Icinga\Application\Modules\Module;
use Zend_Config_Ini;
use Zend_Config;
@ -32,17 +33,19 @@ class Config extends Zend_Config_Ini
return parent::__construct($filename, $section, $options);
}
public function getModuleConfig($key, $module)
public static function module($name, $file = null)
{
$manager = Icinga::app()->moduleManager();
$res = null;
if ($manager->hasInstalled($module)) {
$filename = $manager->getModuleConfigDir($module) . "/$key.ini";
if (file_exists($filename)) {
return $this->$key = new Config($filename);
}
if ($file === null) {
$file = $name . '.ini'; // TODO: default should be module/config.ini
}
return $res;
$filename = Module::get($name)->getConfigDir() . '/' . $file;
if (file_exists($filename)) {
$config = new Config($filename);
// Compat: $config->$module->$whatever
self::getInstance()->$name = $config;
return $config;
}
return null;
}
public function __get($key)

View File

@ -3,6 +3,7 @@
namespace Icinga\Application\Modules;
use Icinga\Application\ApplicationBootstrap;
use Icinga\Application\Icinga;
use Icinga\Web\Hook;
use Zend_Controller_Router_Route as Route;
@ -38,6 +39,23 @@ class Module
return true;
}
public static function exists($name)
{
return Icinga::app()->moduleManager()->hasEnabled($name);
}
public static function get($name, $autoload = false)
{
$manager = Icinga::app()->moduleManager();
if (! $manager->hasLoaded($name)) {
if ($autoload === true && $manager->hasEnabled($name)) {
$manager->loadModule($name);
}
}
// @throws ProgrammingError:
return $manager->getModule($name);
}
public function hasCss()
{
return file_exists($this->getCssFilename());
@ -58,6 +76,13 @@ class Module
return $this->configdir;
}
public function getConfig($file = null)
{
return $this->app
->getConfig()
->module($this->name, $file);
}
protected function registerLibrary()
{
if (file_exists($this->libdir) && is_dir($this->libdir)) {
@ -88,17 +113,14 @@ class Module
}
$this->registerLocales()
->registerRoutes()
->registerMenuEntries();
->registerRoutes();
// ->registerMenuEntries();
return $this;
}
protected function registerMenuEntries()
{
$cfg = $this->app
->getConfig()
->getModuleConfig('menu', $this->name);
$cfg = $this->getConfig('menu.ini');
$view = $this->app->getView();
if ($cfg) {
$view->view->navigation = $cfg->merge($view->view->navigation);
@ -119,6 +141,17 @@ class Module
)
)
);
$this->app->frontController()->getRouter()->addRoute(
$this->name . '_img',
new Route(
'img/' . $this->name . '/:file',
array(
'controller' => 'static',
'action' => 'img',
'moduleName' => $this->name
)
)
);
return $this;
}

View File

@ -1,28 +1,8 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* Icinga 2 Web - Head for multiple monitoring frontends
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
/**
* Module action controller
*/
namespace Icinga\Web;
use Icinga\Application\Config;
@ -84,7 +64,7 @@ class ModuleActionController extends ActionController
*/
protected function loadConfig()
{
$this->config = Config::getInstance()->{$this->module_name};
$this->config = Config::module($this->module_name);
}
/**