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

View File

@ -3,6 +3,7 @@
namespace Icinga\Application\Modules; namespace Icinga\Application\Modules;
use Icinga\Application\ApplicationBootstrap; use Icinga\Application\ApplicationBootstrap;
use Icinga\Application\Icinga;
use Icinga\Web\Hook; use Icinga\Web\Hook;
use Zend_Controller_Router_Route as Route; use Zend_Controller_Router_Route as Route;
@ -38,6 +39,23 @@ class Module
return true; 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() public function hasCss()
{ {
return file_exists($this->getCssFilename()); return file_exists($this->getCssFilename());
@ -58,6 +76,13 @@ class Module
return $this->configdir; return $this->configdir;
} }
public function getConfig($file = null)
{
return $this->app
->getConfig()
->module($this->name, $file);
}
protected function registerLibrary() protected function registerLibrary()
{ {
if (file_exists($this->libdir) && is_dir($this->libdir)) { if (file_exists($this->libdir) && is_dir($this->libdir)) {
@ -88,17 +113,14 @@ class Module
} }
$this->registerLocales() $this->registerLocales()
->registerRoutes() ->registerRoutes();
->registerMenuEntries(); // ->registerMenuEntries();
return $this; return $this;
} }
protected function registerMenuEntries() protected function registerMenuEntries()
{ {
$cfg = $this->app $cfg = $this->getConfig('menu.ini');
->getConfig()
->getModuleConfig('menu', $this->name);
$view = $this->app->getView(); $view = $this->app->getView();
if ($cfg) { if ($cfg) {
$view->view->navigation = $cfg->merge($view->view->navigation); $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; return $this;
} }

View File

@ -1,28 +1,8 @@
<?php <?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; namespace Icinga\Web;
use Icinga\Application\Config; use Icinga\Application\Config;
@ -84,7 +64,7 @@ class ModuleActionController extends ActionController
*/ */
protected function loadConfig() protected function loadConfig()
{ {
$this->config = Config::getInstance()->{$this->module_name}; $this->config = Config::module($this->module_name);
} }
/** /**