Introduce classes `Libraries` and `Library`

This commit is contained in:
Johannes Meyer 2020-11-10 14:06:33 +01:00
parent ea5329236b
commit a2bdc8074f
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,39 @@
<?php
/* Icinga Web 2 | (c) 2020 Icinga GmbH | GPLv2+ */
namespace Icinga\Application;
use ArrayIterator;
use IteratorAggregate;
use Icinga\Application\Libraries\Library;
class Libraries implements IteratorAggregate
{
/** @var Library[] */
protected $libraries = [];
/**
* Iterate over registered libraries
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->libraries);
}
/**
* Register a library from the given path
*
* @param string $path
*
* @return Library The registered library
*/
public function registerPath($path)
{
$library = new Library($path);
$this->libraries[] = $library;
return $library;
}
}

View File

@ -0,0 +1,32 @@
<?php
/* Icinga Web 2 | (c) 2020 Icinga GmbH | GPLv2+ */
namespace Icinga\Application\Libraries;
class Library
{
protected $path;
/**
* Create a new Library
*
* @param string $path
*/
public function __construct($path)
{
$this->path = $path;
}
/**
* Register this library's autoloader
*
* @return void
*/
public function registerAutoloader()
{
$autoloaderPath = join(DIRECTORY_SEPARATOR, [$this->path, 'vendor', 'autoload.php']);
if (file_exists($autoloaderPath)) {
require_once $autoloaderPath;
}
}
}