2014-11-05 15:32:09 +01:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
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 13:16:49 +01:00
|
|
|
* Web path
|
2014-11-05 15:32:09 +01:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-11-13 13:16:49 +01:00
|
|
|
protected $webPath;
|
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(
|
|
|
|
'{webPath}',
|
2014-11-13 13:16:49 +01:00
|
|
|
'{documentRoot}',
|
2014-11-07 10:34:53 +01:00
|
|
|
'{configPath}',
|
2014-11-05 15:32:09 +01:00
|
|
|
);
|
|
|
|
$replaceTokens = array(
|
|
|
|
$this->getWebPath(),
|
2014-11-13 13:16:49 +01:00
|
|
|
$this->getDocumentRoot(),
|
|
|
|
Icinga::app()->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 13:47:08 +01:00
|
|
|
public function setWebPath($urlPath)
|
2014-11-05 15:32:09 +01:00
|
|
|
{
|
2014-11-13 13:47:08 +01:00
|
|
|
$this->webPath = '/' . ltrim(trim((string) $urlPath), '/');
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function getWebPath()
|
|
|
|
{
|
|
|
|
return $this->webPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
}
|