2014-11-05 15:32:09 +01:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-11-05 15:32:09 +01:00
|
|
|
|
2014-11-13 12:50:55 +01:00
|
|
|
namespace Icinga\Module\Setup;
|
2014-11-05 15:32:09 +01:00
|
|
|
|
2014-11-13 13:16:49 +01:00
|
|
|
use Icinga\Application\Icinga;
|
2014-11-05 15:32:09 +01:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
|
|
|
|
/**
|
2014-11-13 12:50:55 +01:00
|
|
|
* Base class for generating webserver configuration
|
2014-11-05 15:32:09 +01:00
|
|
|
*/
|
2014-11-06 10:41:28 +01:00
|
|
|
abstract class Webserver
|
2014-11-05 15:32:09 +01:00
|
|
|
{
|
|
|
|
/**
|
2014-11-13 13:16:49 +01:00
|
|
|
* Document root
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-11-13 13:16:49 +01:00
|
|
|
protected $documentRoot;
|
2014-11-05 15:32:09 +01:00
|
|
|
|
|
|
|
/**
|
2014-11-13 14:18:14 +01:00
|
|
|
* URL path of Icinga Web 2
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-12-30 14:42:42 +01:00
|
|
|
protected $urlPath = '/icingaweb2';
|
2014-11-13 14:05:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Path to Icinga Web 2's configuration files
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $configDir;
|
2014-11-05 15:32:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create instance by type name
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
*
|
|
|
|
* @return WebServer
|
|
|
|
*
|
|
|
|
* @throws ProgrammingError
|
|
|
|
*/
|
|
|
|
public static function createInstance($type)
|
|
|
|
{
|
2014-11-13 12:50:55 +01:00
|
|
|
$class = __NAMESPACE__ . '\\Webserver\\' . ucfirst($type);
|
2014-11-05 15:32:09 +01:00
|
|
|
if (class_exists($class)) {
|
|
|
|
return new $class();
|
|
|
|
}
|
|
|
|
throw new ProgrammingError('Class "%s" does not exist', $class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate configuration
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generate()
|
|
|
|
{
|
|
|
|
$template = $this->getTemplate();
|
2014-11-07 09:18:16 +01:00
|
|
|
|
2014-11-05 15:32:09 +01:00
|
|
|
$searchTokens = array(
|
2014-11-13 14:18:14 +01:00
|
|
|
'{urlPath}',
|
2014-11-13 13:16:49 +01:00
|
|
|
'{documentRoot}',
|
2014-11-13 14:05:13 +01:00
|
|
|
'{configDir}',
|
2014-11-05 15:32:09 +01:00
|
|
|
);
|
|
|
|
$replaceTokens = array(
|
2014-11-13 14:18:14 +01:00
|
|
|
$this->getUrlPath(),
|
2014-11-13 13:16:49 +01:00
|
|
|
$this->getDocumentRoot(),
|
2014-11-13 14:05:13 +01:00
|
|
|
$this->getConfigDir()
|
2014-11-05 15:32:09 +01:00
|
|
|
);
|
|
|
|
$template = str_replace($searchTokens, $replaceTokens, $template);
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specific template
|
|
|
|
*
|
2014-11-07 09:18:16 +01:00
|
|
|
* @return string
|
2014-11-05 15:32:09 +01:00
|
|
|
*/
|
|
|
|
abstract protected function getTemplate();
|
|
|
|
|
|
|
|
/**
|
2014-11-13 13:47:08 +01:00
|
|
|
* Set the URL path of Icinga Web 2
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
2014-11-13 13:47:08 +01:00
|
|
|
* @param string $urlPath
|
|
|
|
*
|
|
|
|
* @return $this
|
2014-11-05 15:32:09 +01:00
|
|
|
*/
|
2014-11-13 14:18:14 +01:00
|
|
|
public function setUrlPath($urlPath)
|
2014-11-05 15:32:09 +01:00
|
|
|
{
|
2014-11-13 14:18:14 +01:00
|
|
|
$this->urlPath = '/' . ltrim(trim((string) $urlPath), '/');
|
2014-11-13 13:47:08 +01:00
|
|
|
return $this;
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-13 13:47:08 +01:00
|
|
|
* Get the URL path of Icinga Web 2
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-11-13 14:18:14 +01:00
|
|
|
public function getUrlPath()
|
2014-11-05 15:32:09 +01:00
|
|
|
{
|
2014-11-13 14:18:14 +01:00
|
|
|
return $this->urlPath;
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-13 13:16:49 +01:00
|
|
|
* Set the document root
|
|
|
|
*
|
|
|
|
* @param string $documentRoot
|
|
|
|
*
|
|
|
|
* @return $this
|
2014-11-05 15:32:09 +01:00
|
|
|
*/
|
2014-11-13 13:16:49 +01:00
|
|
|
public function setDocumentRoot($documentRoot)
|
2014-11-05 15:32:09 +01:00
|
|
|
{
|
2014-11-13 13:47:08 +01:00
|
|
|
$this->documentRoot = trim((string) $documentRoot);
|
2014-11-13 13:16:49 +01:00
|
|
|
return $this;
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-13 13:16:49 +01:00
|
|
|
* Detect the document root
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-11-13 13:16:49 +01:00
|
|
|
public function detectDocumentRoot()
|
2014-11-05 15:32:09 +01:00
|
|
|
{
|
2014-11-13 13:16:49 +01:00
|
|
|
return Icinga::app()->getBaseDir('public');
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-13 13:16:49 +01:00
|
|
|
* Get the document root
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-11-13 13:16:49 +01:00
|
|
|
public function getDocumentRoot()
|
2014-11-05 15:32:09 +01:00
|
|
|
{
|
2014-11-13 13:16:49 +01:00
|
|
|
if ($this->documentRoot === null) {
|
|
|
|
$this->documentRoot = $this->detectDocumentRoot();
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|
2014-11-13 13:16:49 +01:00
|
|
|
return $this->documentRoot;
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|
2014-11-13 14:05:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the configuration directory
|
|
|
|
*
|
|
|
|
* @param string $configDir
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setConfigDir($configDir)
|
|
|
|
{
|
|
|
|
$this->configDir = (string) $configDir;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the configuration directory
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getConfigDir()
|
|
|
|
{
|
|
|
|
if ($this->configDir === null) {
|
|
|
|
return Icinga::app()->getConfigDir();
|
|
|
|
}
|
|
|
|
return $this->configDir;
|
|
|
|
}
|
2014-11-05 15:32:09 +01:00
|
|
|
}
|