icingaweb2/library/Icinga/Application/ApplicationBootstrap.php
Marius Hein 316893ad2c Add new autoloader implementation
New namespace implementation created to load application code
like forms with this autoloader. Consumpting services can register
their own, multiple namespaces. Overlapping namespaces matched
by closest name.

refs #4407
2013-07-12 16:11:03 +02:00

298 lines
7.4 KiB
PHP
Executable File

<?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}}}
namespace Icinga\Application;
use Icinga\Application\Modules\Manager as ModuleManager;
use Icinga\Application\Platform;
use Zend_Loader_Autoloader as ZendLoader;
use Icinga\Exception\ConfigurationError;
/**
* This class bootstraps a thin Icinga application layer
*
* Usage example for CLI:
* <code>
* use Icinga\Application\Cli;
* Cli::start();
* </code>
*
* Usage example for Icinga Web application:
* <code>
* use Icinga\Application\Web;
* Web::start()->dispatch();
* </code>
*
* Usage example for Icinga-Web 1.x compatibility mode:
* <code>
* use Icinga\Application\LegacyWeb;
* LegacyWeb::start()->setIcingaWebBasedir(ICINGAWEB_BASEDIR)->dispatch();
* </code>
*
* @copyright Copyright (c) 2013 Icinga-Web Team <info@icinga.org>
* @author Icinga-Web Team <info@icinga.org>
* @package Icinga\Application
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
*/
abstract class ApplicationBootstrap
{
protected $loader;
protected $libdir;
protected $config;
protected $configDir;
protected $appdir;
protected $moduleManager;
protected $isCli = false;
protected $isWeb = false;
/**
* Constructor
*
* The constructor is protected to avoid incorrect usage
*/
protected function __construct($configDir)
{
$this->checkPrerequisites();
$this->libdir = realpath(__DIR__. '/../..');
if (! defined('ICINGA_LIBDIR')) {
define('ICINGA_LIBDIR', $this->libdir);
}
// TODO: Make appdir configurable for packagers
$this->appdir = realpath($this->libdir. '/../application');
if (! defined('ICINGA_APPDIR')) {
define('ICINGA_APPDIR', $this->appdir);
}
$this->registerAutoloader();
$this->registerZendAutoloader();
Benchmark::measure('Bootstrap, autoloader registered');
Icinga::setApp($this);
// Unfortunately this is needed to get the Zend Plugin loader working:
set_include_path(
implode(
PATH_SEPARATOR,
array($this->libdir, get_include_path())
)
);
$this->configDir = $configDir;
require_once dirname(__FILE__) . '/functions.php';
}
abstract protected function bootstrap();
public function moduleManager()
{
if ($this->moduleManager === null) {
$this->moduleManager = new ModuleManager($this, $this->configDir . '/enabledModules');
}
return $this->moduleManager;
}
/**
* Getter for class loader
* @return Loader
*/
public function getLoader()
{
return $this->loader;
}
protected function loadEnabledModules()
{
$this->moduleManager()->loadEnabledModules();
return $this;
}
public function isCli()
{
return $this->isCli;
}
public function isWeb()
{
return $this->isWeb;
}
public function getApplicationDir($subdir = null)
{
$dir = $this->appdir;
if ($subdir !== null) {
$dir .= '/' . ltrim($subdir, '/');
}
return $dir;
}
public function hasModule($name)
{
return $this->moduleManager()->hasLoaded($name);
}
public function getModule($name)
{
return $this->moduleManager()->getModule($name);
}
public function loadModule($name)
{
return $this->moduleManager()->loadModule($name);
}
public function getConfig()
{
return $this->config;
}
public static function start($configDir)
{
$class = get_called_class();
$obj = new $class($configDir);
$obj->bootstrap();
return $obj;
}
public function registerAutoloader()
{
require $this->libdir. '/Icinga/Exception/ProgrammingError.php';
require $this->libdir. '/Icinga/Application/Loader.php';
$this->loader = new Loader();
$this->loader->registerNamespace('Icinga', $this->libdir. '/Icinga');
$this->loader->registerNamespace('Icinga\\Form', $this->appdir. '/forms');
$this->loader->register();
}
/**
* Register the Zend Autoloader
*
* @return self
*/
protected function registerZendAutoloader()
{
require_once 'Zend/Loader/Autoloader.php';
ZendLoader::getInstance();
return $this;
}
/**
* Check whether we have all we need
*
* Pretty useless right now as a namespaces class would not work
* with PHP 5.3
*
* @return self
*/
protected function checkPrerequisites()
{
if (version_compare(phpversion(), '5.3.0', '<') === true) {
die('PHP > 5.3.0 required');
}
return $this;
}
/**
* Check whether a given PHP extension is available
*
* @return boolean
*/
protected function hasExtension($name)
{
if (!extension_loaded($name)) {
if (! @ dl($name)) {
throw new ConfigurationError(
sprintf(
'The PHP extension %s is not available',
$name
)
);
}
}
}
/**
* Load Configuration
*
* @return self
*/
protected function loadConfig()
{
Config::$configDir = $this->configDir;
$this->config = Config::app();
return $this;
}
/**
* Configure cache settings
*
* TODO: Right now APC is hardcoded, make this configurable
*
* @return self
*/
protected function configureCache()
{
// TODO: Provide Zend_Cache_Frontend_File for statusdat
//$this->cache = \Zend_Cache::factory('Core', 'Apc');
return $this;
}
/**
* Error handling configuration
*
* @return self
*/
protected function configureErrorHandling()
{
if ($this->config->get('global', 'environment') == 'development') {
error_reporting(E_ALL | E_NOTICE);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
}
Logger::create($this->config->logging);
return $this;
}
/**
* Set timezone settings
*
* @return self
*/
protected function setTimezone()
{
date_default_timezone_set(
$this->config->global->get('timezone', 'UTC')
);
return $this;
}
}