2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
2013-07-12 15:00:59 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 11:44:37 +02:00
|
|
|
/**
|
2013-07-15 12:26:10 +02:00
|
|
|
* This file is part of Icinga 2 Web.
|
2013-08-06 17:35:33 +02:00
|
|
|
*
|
2013-07-15 12:26:10 +02:00
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
2013-07-12 15:00:59 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-08-06 17:35:33 +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-08-06 17:35:33 +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-08-06 17:35:33 +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-08-06 17:35:33 +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-06-07 11:44:37 +02:00
|
|
|
*/
|
2013-07-12 15:00:59 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
namespace Icinga\Application;
|
|
|
|
|
2013-08-06 17:35:33 +02:00
|
|
|
use \DateTimeZone;
|
|
|
|
use \Exception;
|
2013-06-07 11:44:37 +02:00
|
|
|
use Icinga\Application\Modules\Manager as ModuleManager;
|
|
|
|
use Icinga\Application\Platform;
|
2013-08-12 15:02:25 +02:00
|
|
|
use \Icinga\Application\Config;
|
2013-06-07 11:44:37 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2013-08-06 17:35:33 +02:00
|
|
|
use Zend_Loader_Autoloader;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class bootstraps a thin Icinga application layer
|
|
|
|
*
|
|
|
|
* Usage example for CLI:
|
|
|
|
* <code>
|
|
|
|
* use Icinga\Application\Cli;
|
2013-06-20 14:06:02 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
* 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>
|
|
|
|
*/
|
|
|
|
abstract class ApplicationBootstrap
|
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Icinga auto loader
|
|
|
|
*
|
|
|
|
* @var Loader
|
|
|
|
*/
|
|
|
|
private $loader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Library directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $libDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config object
|
|
|
|
*
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $configDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Application directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $appDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module manager
|
|
|
|
*
|
|
|
|
* @var ModuleManager
|
|
|
|
*/
|
|
|
|
private $moduleManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag indicates we're on cli environment
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected $isCli = false;
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag indicates we're on web environment
|
2013-08-06 17:35:33 +02:00
|
|
|
*
|
2013-07-26 15:58:16 +02:00
|
|
|
* @var bool
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
protected $isWeb = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* The constructor is protected to avoid incorrect usage
|
|
|
|
*/
|
2013-07-12 15:37:36 +02:00
|
|
|
protected function __construct($configDir)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->libDir = realpath(__DIR__. '/../..');
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
if (!defined('ICINGA_LIBDIR')) {
|
|
|
|
define('ICINGA_LIBDIR', $this->libDir);
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
2013-07-12 15:00:59 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
// TODO: Make appdir configurable for packagers
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->appDir = realpath($this->libDir. '/../application');
|
2013-07-12 15:00:59 +02:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
if (!defined('ICINGA_APPDIR')) {
|
|
|
|
define('ICINGA_APPDIR', $this->appDir);
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->setupAutoloader();
|
|
|
|
$this->setupZendAutoloader();
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
Benchmark::measure('Bootstrap, autoloader registered');
|
|
|
|
|
|
|
|
Icinga::setApp($this);
|
2013-07-30 16:02:05 +02:00
|
|
|
$this->configDir = realpath($configDir);
|
2013-07-26 15:58:16 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
require_once dirname(__FILE__) . '/functions.php';
|
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Bootstrap interface method for concrete bootstrap objects
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
abstract protected function bootstrap();
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Getter for module manager
|
|
|
|
*
|
|
|
|
* @return ModuleManager
|
|
|
|
*/
|
|
|
|
public function getModuleManager()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
return $this->moduleManager;
|
|
|
|
}
|
|
|
|
|
2013-07-12 15:00:59 +02:00
|
|
|
/**
|
|
|
|
* Getter for class loader
|
2013-07-26 15:58:16 +02:00
|
|
|
*
|
2013-07-12 15:00:59 +02:00
|
|
|
* @return Loader
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function getLoader()
|
|
|
|
{
|
|
|
|
return $this->loader;
|
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Getter for configuration object
|
|
|
|
*
|
|
|
|
* @return Config
|
|
|
|
*/
|
|
|
|
public function getConfig()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
return $this->config;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Flag indicates we're on cli environment
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function isCli()
|
|
|
|
{
|
|
|
|
return $this->isCli;
|
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Flag indicates we're on web environment
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function isWeb()
|
|
|
|
{
|
|
|
|
return $this->isWeb;
|
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Getter for application dir
|
|
|
|
*
|
|
|
|
* Optional append sub directory
|
|
|
|
*
|
|
|
|
* @param null|string $subdir optional subdir
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-07 11:44:37 +02:00
|
|
|
public function getApplicationDir($subdir = null)
|
|
|
|
{
|
2013-07-30 16:02:05 +02:00
|
|
|
return $this->getDirWithSubDir($this->appDir, $subdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for config dir
|
|
|
|
* @param string|null $subdir
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getConfigDir($subdir = null)
|
|
|
|
{
|
|
|
|
return $this->getDirWithSubDir($this->configDir, $subdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to glue directories together
|
|
|
|
*
|
|
|
|
* @param string $dir
|
|
|
|
* @param string|null $subdir
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getDirWithSubDir($dir, $subdir=null)
|
|
|
|
{
|
2013-06-07 11:44:37 +02:00
|
|
|
if ($subdir !== null) {
|
|
|
|
$dir .= '/' . ltrim($subdir, '/');
|
|
|
|
}
|
2013-07-30 16:02:05 +02:00
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
return $dir;
|
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Starting concrete bootstrap classes
|
|
|
|
*
|
|
|
|
* @param string $configDir
|
|
|
|
* @return ApplicationBootstrap
|
|
|
|
*/
|
2013-07-12 15:37:36 +02:00
|
|
|
public static function start($configDir)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
$class = get_called_class();
|
2013-07-26 15:58:16 +02:00
|
|
|
/** @var ApplicationBootstrap $obj */
|
2013-07-12 15:37:36 +02:00
|
|
|
$obj = new $class($configDir);
|
2013-06-07 11:44:37 +02:00
|
|
|
$obj->bootstrap();
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
/**
|
|
|
|
* Setup icinga auto loader
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setupAutoloader()
|
2013-07-12 15:00:59 +02:00
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
require $this->libDir. '/Icinga/Application/Loader.php';
|
2013-07-12 15:00:59 +02:00
|
|
|
|
|
|
|
$this->loader = new Loader();
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->loader->registerNamespace('Icinga', $this->libDir. '/Icinga');
|
|
|
|
$this->loader->registerNamespace('Icinga\\Form', $this->appDir. '/forms');
|
2013-07-12 15:00:59 +02:00
|
|
|
$this->loader->register();
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
return $this;
|
2013-07-12 15:00:59 +02:00
|
|
|
}
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
/**
|
|
|
|
* Register the Zend Autoloader
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-07-26 15:58:16 +02:00
|
|
|
protected function setupZendAutoloader()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
require_once 'Zend/Loader/Autoloader.php';
|
2013-07-26 15:58:16 +02:00
|
|
|
|
|
|
|
\Zend_Loader_Autoloader::getInstance();
|
|
|
|
|
|
|
|
// Unfortunately this is needed to get the Zend Plugin loader working:
|
|
|
|
set_include_path(
|
|
|
|
implode(
|
|
|
|
PATH_SEPARATOR,
|
|
|
|
array($this->libDir, get_include_path())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-26 15:58:16 +02:00
|
|
|
* Setup module loader and all enabled modules
|
2013-06-07 11:44:37 +02:00
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-07-26 15:58:16 +02:00
|
|
|
protected function setupModules()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->moduleManager = new ModuleManager($this, $this->configDir . '/enabledModules');
|
2013-06-07 11:44:37 +02:00
|
|
|
|
2013-07-26 15:58:16 +02:00
|
|
|
$this->moduleManager->loadEnabledModules();
|
|
|
|
|
|
|
|
return $this;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load Configuration
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-07-26 15:58:16 +02:00
|
|
|
protected function setupConfig()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:37:36 +02:00
|
|
|
Config::$configDir = $this->configDir;
|
|
|
|
$this->config = Config::app();
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error handling configuration
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2013-07-26 15:58:16 +02:00
|
|
|
protected function setupErrorHandling()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-07-12 15:37:36 +02:00
|
|
|
if ($this->config->get('global', 'environment') == 'development') {
|
2013-06-07 11:44:37 +02:00
|
|
|
error_reporting(E_ALL | E_NOTICE);
|
|
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
ini_set('display_errors', 1);
|
|
|
|
}
|
|
|
|
Logger::create($this->config->logging);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-06 17:35:33 +02:00
|
|
|
* Setup timezone
|
2013-06-07 11:44:37 +02:00
|
|
|
*
|
|
|
|
* @return self
|
2013-08-06 17:35:33 +02:00
|
|
|
* @throws \Icinga\Exception\ConfigurationError if the timezone in config.ini isn't valid
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2013-07-26 15:58:16 +02:00
|
|
|
protected function setupTimezone()
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-08-06 17:35:33 +02:00
|
|
|
$tz = $this->config->global->get('timezone', 'UTC');
|
|
|
|
try {
|
|
|
|
new DateTimeZone($tz);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new ConfigurationError(t('Invalid timezone') . ' "' . $tz . '"');
|
|
|
|
}
|
|
|
|
date_default_timezone_set($tz);
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|