2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-07-12 15:00:59 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
2013-07-15 12:26:10 +02:00
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
2013-07-12 15:00:59 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-07-15 12:26:10 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* 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.
|
2013-07-15 12:26:10 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* 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.
|
2013-07-15 12:26:10 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* 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.
|
2013-07-15 12:26:10 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
2013-07-15 12:26:10 +02:00
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
namespace Icinga\Application\Modules;
|
|
|
|
|
|
|
|
use Icinga\Application\ApplicationBootstrap;
|
2013-08-12 15:02:25 +02:00
|
|
|
use \Icinga\Application\Config;
|
2013-07-26 15:58:16 +02:00
|
|
|
use Icinga\Application\Icinga;
|
2013-06-07 11:44:37 +02:00
|
|
|
use Icinga\Web\Hook;
|
|
|
|
use Zend_Controller_Router_Route as Route;
|
|
|
|
|
2013-07-15 12:16:14 +02:00
|
|
|
/**
|
|
|
|
* Module handling
|
|
|
|
*
|
|
|
|
* Register modules and initialize it
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
class Module
|
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Module name
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base directory of module
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $basedir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directory for styles
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $cssdir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Library directory
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $libdir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directory containing translations
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $localedir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directory where controllers reside
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $controllerdir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directory containing form implementations
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $formdir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module bootstrapping script
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $registerscript;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Icinga application
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @var \Icinga\Application\Web
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
|
|
|
private $app;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
2013-07-30 10:47:50 +02:00
|
|
|
* Create a new module object
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @param ApplicationBootstrap $app
|
|
|
|
* @param string $name
|
|
|
|
* @param strinb $basedir
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function __construct(ApplicationBootstrap $app, $name, $basedir)
|
|
|
|
{
|
|
|
|
$this->app = $app;
|
|
|
|
$this->name = $name;
|
|
|
|
$this->basedir = $basedir;
|
2013-07-12 15:00:59 +02:00
|
|
|
$this->cssdir = $basedir. '/public/css';
|
|
|
|
$this->libdir = $basedir. '/library';
|
|
|
|
$this->configdir = $basedir. '/config';
|
|
|
|
$this->localedir = $basedir. '/application/locale';
|
|
|
|
$this->formdir = $basedir. '/application/forms';
|
|
|
|
$this->controllerdir = $basedir. '/application/controllers';
|
|
|
|
$this->registerscript = $basedir. '/register.php';
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Register module
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function register()
|
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
$this->registerAutoloader()
|
2013-06-07 11:44:37 +02:00
|
|
|
->registerWebIntegration()
|
|
|
|
->runRegisterScript();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-15 12:16:14 +02:00
|
|
|
/**
|
|
|
|
* Test for an enabled module by name
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-15 12:16:14 +02:00
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-07-12 12:11:59 +02:00
|
|
|
public static function exists($name)
|
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
return Icinga::app()->getModuleManager()->hasEnabled($name);
|
2013-07-12 12:11:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Get module by name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param bool $autoload
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-07-12 12:11:59 +02:00
|
|
|
public static function get($name, $autoload = false)
|
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
$manager = Icinga::app()->getModuleManager();
|
|
|
|
if (!$manager->hasLoaded($name)) {
|
2013-07-12 12:11:59 +02:00
|
|
|
if ($autoload === true && $manager->hasEnabled($name)) {
|
|
|
|
$manager->loadModule($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// @throws ProgrammingError:
|
|
|
|
return $manager->getModule($name);
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Test if module provide css
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function hasCss()
|
|
|
|
{
|
|
|
|
return file_exists($this->getCssFilename());
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Getter for module name
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for css file name
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function getCssFilename()
|
|
|
|
{
|
|
|
|
return $this->cssdir . '/module.less';
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Getter for base directory
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function getBaseDir()
|
|
|
|
{
|
|
|
|
return $this->basedir;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Getter for library directory
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLibDir()
|
|
|
|
{
|
|
|
|
return $this->libdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for configuration directory
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function getConfigDir()
|
|
|
|
{
|
|
|
|
return $this->configdir;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Getter for form directory
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFormDir()
|
|
|
|
{
|
|
|
|
return $this->formdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for module config object
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @param null|string $file
|
2013-07-12 15:00:59 +02:00
|
|
|
* @return Config
|
|
|
|
*/
|
2013-07-12 12:11:59 +02:00
|
|
|
public function getConfig($file = null)
|
|
|
|
{
|
|
|
|
return $this->app
|
|
|
|
->getConfig()
|
|
|
|
->module($this->name, $file);
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Register new namespaces on the autoloader
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @return self
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
|
|
|
protected function registerAutoloader()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
if (is_dir($this->getBaseDir()) && is_dir($this->getLibDir())) {
|
|
|
|
$moduleName = ucfirst($this->getName());
|
|
|
|
$moduleLibraryDir = $this->getLibDir(). '/'. $moduleName;
|
|
|
|
|
|
|
|
$this->app->getLoader()->registerNamespace($moduleName, $moduleLibraryDir);
|
2013-07-15 13:37:43 +02:00
|
|
|
if (is_dir($this->getFormDir())) {
|
|
|
|
$this->app->getLoader()->registerNamespace($moduleName. '\\Form', $this->getFormDir());
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-07-12 15:00:59 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Bind text domain for i18n
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @return self
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected function registerLocales()
|
|
|
|
{
|
|
|
|
if (file_exists($this->localedir) && is_dir($this->localedir)) {
|
|
|
|
bindtextdomain($this->name, $this->localedir);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Register web integration
|
|
|
|
*
|
|
|
|
* Add controller directory to mvc
|
|
|
|
*
|
2013-07-26 15:58:16 +02:00
|
|
|
* @return self
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected function registerWebIntegration()
|
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
if (!$this->app->isWeb()) {
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($this->controllerdir) && is_dir($this->controllerdir)) {
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->app->getfrontController()->addControllerDirectory(
|
2013-06-07 11:44:37 +02:00
|
|
|
$this->controllerdir,
|
|
|
|
$this->name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->registerLocales()
|
2013-07-12 15:44:55 +02:00
|
|
|
->registerRoutes()
|
|
|
|
->registerMenuEntries();
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Register menu entries
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @return self
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected function registerMenuEntries()
|
|
|
|
{
|
2013-07-12 15:37:36 +02:00
|
|
|
$cfg = $this->app
|
|
|
|
->getConfig()
|
|
|
|
->module($this->name, 'menu');
|
2013-07-26 15:58:16 +02:00
|
|
|
$view = $this->app->getViewRenderer();
|
2013-06-07 11:44:37 +02:00
|
|
|
if ($cfg) {
|
|
|
|
$view->view->navigation = $cfg->merge($view->view->navigation);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Register routes for web access
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @return self
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected function registerRoutes()
|
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->app->getFrontController()->getRouter()->addRoute(
|
2013-06-07 11:44:37 +02:00
|
|
|
$this->name . '_jsprovider',
|
|
|
|
new Route(
|
|
|
|
'js/' . $this->name . '/:file',
|
|
|
|
array(
|
|
|
|
'controller' => 'static',
|
|
|
|
'action' =>'javascript',
|
|
|
|
'moduleName' => $this->name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->app->getFrontController()->getRouter()->addRoute(
|
2013-07-12 12:11:59 +02:00
|
|
|
$this->name . '_img',
|
|
|
|
new Route(
|
|
|
|
'img/' . $this->name . '/:file',
|
|
|
|
array(
|
|
|
|
'controller' => 'static',
|
|
|
|
'action' => 'img',
|
|
|
|
'moduleName' => $this->name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Run module bootstrap script
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
|
|
|
* @return self
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected function runRegisterScript()
|
|
|
|
{
|
|
|
|
if (file_exists($this->registerscript)
|
|
|
|
&& is_readable($this->registerscript)) {
|
|
|
|
include($this->registerscript);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Register hook
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @param string $name
|
|
|
|
* @param string $class
|
|
|
|
* @param string $key
|
2013-07-26 15:58:16 +02:00
|
|
|
* @return self
|
2013-07-12 15:00:59 +02:00
|
|
|
*/
|
|
|
|
protected function registerHook($name, $key, $class)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:00:59 +02:00
|
|
|
Hook::register($name, $key, $class);
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|