diff --git a/library/Icinga/Application/ApplicationBootstrap.php b/library/Icinga/Application/ApplicationBootstrap.php index f3d8c086f..04cc930d2 100644 --- a/library/Icinga/Application/ApplicationBootstrap.php +++ b/library/Icinga/Application/ApplicationBootstrap.php @@ -3,6 +3,7 @@ namespace Icinga\Application; +use DirectoryIterator; use ErrorException; use Exception; use LogicException; @@ -62,7 +63,7 @@ abstract class ApplicationBootstrap protected $vendorDir; /** - * Library directory + * Icinga library directory * * @var string */ @@ -89,6 +90,20 @@ abstract class ApplicationBootstrap */ protected $storageDir; + /** + * External library paths + * + * @var string[] + */ + protected $libraryPaths; + + /** + * Loaded external libraries + * + * @var Libraries + */ + protected $libraries; + /** * Icinga class loader * @@ -176,6 +191,20 @@ abstract class ApplicationBootstrap $canonical = realpath($storageDir); $this->storageDir = $canonical ? $canonical : $storageDir; + if ($this->libraryPaths === null) { + $libraryPaths = getenv('ICINGAWEB_LIBDIR'); + if ($libraryPaths !== false) { + $this->libraryPaths = array_filter(array_map( + 'realpath', + explode(':', $libraryPaths) + ), 'is_dir'); + } else { + $this->libraryPaths = is_dir('/usr/share/php-icinga') + ? ['/usr/share/php-icinga'] + : []; + } + } + set_include_path( implode( PATH_SEPARATOR, @@ -197,6 +226,16 @@ abstract class ApplicationBootstrap */ abstract protected function bootstrap(); + /** + * Get loaded external libraries + * + * @return Libraries + */ + public function getLibraries() + { + return $this->libraries; + } + /** * Getter for module manager * @@ -499,6 +538,26 @@ abstract class ApplicationBootstrap return @file_exists($this->config->resolvePath('setup.token')); } + /** + * Load external libraries + * + * @return $this + */ + protected function loadLibraries() + { + $this->libraries = new Libraries(); + foreach ($this->libraryPaths as $libraryPath) { + foreach (new DirectoryIterator($libraryPath) as $path) { + if (! $path->isDot() && is_dir($path->getRealPath())) { + $this->libraries->registerPath($path->getPathname()) + ->registerAutoloader(); + } + } + } + + return $this; + } + /** * Setup default logging * diff --git a/library/Icinga/Application/Cli.php b/library/Icinga/Application/Cli.php index 194a86a52..70bd1c8e3 100644 --- a/library/Icinga/Application/Cli.php +++ b/library/Icinga/Application/Cli.php @@ -38,6 +38,7 @@ class Cli extends ApplicationBootstrap $this->assertRunningOnCli(); $this->setupLogging() ->setupErrorHandling() + ->loadLibraries() ->loadConfig() ->setupTimezone() ->setupInternationalization() diff --git a/library/Icinga/Application/EmbeddedWeb.php b/library/Icinga/Application/EmbeddedWeb.php index 8cf4fe31e..4ca3caca1 100644 --- a/library/Icinga/Application/EmbeddedWeb.php +++ b/library/Icinga/Application/EmbeddedWeb.php @@ -65,6 +65,7 @@ class EmbeddedWeb extends ApplicationBootstrap return $this ->setupZendAutoloader() ->setupErrorHandling() + ->loadLibraries() ->loadConfig() ->setupLogging() ->setupLogger() diff --git a/library/Icinga/Application/Web.php b/library/Icinga/Application/Web.php index 834e584fe..a865c7d3a 100644 --- a/library/Icinga/Application/Web.php +++ b/library/Icinga/Application/Web.php @@ -83,6 +83,7 @@ class Web extends EmbeddedWeb ->setupZendAutoloader() ->setupLogging() ->setupErrorHandling() + ->loadLibraries() ->loadConfig() ->setupLogger() ->setupRequest()