bootstrap: Load external libraries

This commit is contained in:
Johannes Meyer 2020-11-10 14:07:00 +01:00
parent a2bdc8074f
commit a60f511cfc
4 changed files with 63 additions and 1 deletions

View File

@ -3,6 +3,7 @@
namespace Icinga\Application; namespace Icinga\Application;
use DirectoryIterator;
use ErrorException; use ErrorException;
use Exception; use Exception;
use LogicException; use LogicException;
@ -62,7 +63,7 @@ abstract class ApplicationBootstrap
protected $vendorDir; protected $vendorDir;
/** /**
* Library directory * Icinga library directory
* *
* @var string * @var string
*/ */
@ -89,6 +90,20 @@ abstract class ApplicationBootstrap
*/ */
protected $storageDir; protected $storageDir;
/**
* External library paths
*
* @var string[]
*/
protected $libraryPaths;
/**
* Loaded external libraries
*
* @var Libraries
*/
protected $libraries;
/** /**
* Icinga class loader * Icinga class loader
* *
@ -176,6 +191,20 @@ abstract class ApplicationBootstrap
$canonical = realpath($storageDir); $canonical = realpath($storageDir);
$this->storageDir = $canonical ? $canonical : $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( set_include_path(
implode( implode(
PATH_SEPARATOR, PATH_SEPARATOR,
@ -197,6 +226,16 @@ abstract class ApplicationBootstrap
*/ */
abstract protected function bootstrap(); abstract protected function bootstrap();
/**
* Get loaded external libraries
*
* @return Libraries
*/
public function getLibraries()
{
return $this->libraries;
}
/** /**
* Getter for module manager * Getter for module manager
* *
@ -499,6 +538,26 @@ abstract class ApplicationBootstrap
return @file_exists($this->config->resolvePath('setup.token')); 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 * Setup default logging
* *

View File

@ -38,6 +38,7 @@ class Cli extends ApplicationBootstrap
$this->assertRunningOnCli(); $this->assertRunningOnCli();
$this->setupLogging() $this->setupLogging()
->setupErrorHandling() ->setupErrorHandling()
->loadLibraries()
->loadConfig() ->loadConfig()
->setupTimezone() ->setupTimezone()
->setupInternationalization() ->setupInternationalization()

View File

@ -65,6 +65,7 @@ class EmbeddedWeb extends ApplicationBootstrap
return $this return $this
->setupZendAutoloader() ->setupZendAutoloader()
->setupErrorHandling() ->setupErrorHandling()
->loadLibraries()
->loadConfig() ->loadConfig()
->setupLogging() ->setupLogging()
->setupLogger() ->setupLogger()

View File

@ -83,6 +83,7 @@ class Web extends EmbeddedWeb
->setupZendAutoloader() ->setupZendAutoloader()
->setupLogging() ->setupLogging()
->setupErrorHandling() ->setupErrorHandling()
->loadLibraries()
->loadConfig() ->loadConfig()
->setupLogger() ->setupLogger()
->setupRequest() ->setupRequest()