2020-11-10 14:06:33 +01:00
|
|
|
<?php
|
|
|
|
/* Icinga Web 2 | (c) 2020 Icinga GmbH | GPLv2+ */
|
|
|
|
|
|
|
|
namespace Icinga\Application\Libraries;
|
|
|
|
|
2020-11-10 15:11:46 +01:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
use Icinga\Exception\Json\JsonDecodeException;
|
|
|
|
use Icinga\Util\Json;
|
|
|
|
|
2020-11-10 14:06:33 +01:00
|
|
|
class Library
|
|
|
|
{
|
|
|
|
protected $path;
|
|
|
|
|
2020-11-10 15:11:46 +01:00
|
|
|
/** @var string */
|
|
|
|
protected $version;
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $metaData;
|
|
|
|
|
2020-11-10 14:06:33 +01:00
|
|
|
/**
|
|
|
|
* Create a new Library
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
public function __construct($path)
|
|
|
|
{
|
|
|
|
$this->path = $path;
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:11:46 +01:00
|
|
|
/**
|
|
|
|
* Get this library's name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->metaData()['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this library's version
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getVersion()
|
|
|
|
{
|
|
|
|
if ($this->version === null) {
|
|
|
|
if (isset($this->metaData()['version'])) {
|
|
|
|
$this->version = trim(ltrim($this->metaData()['version'], 'v'));
|
|
|
|
} else {
|
|
|
|
$versionFile = $this->path . DIRECTORY_SEPARATOR . 'VERSION';
|
|
|
|
if (file_exists($versionFile)) {
|
|
|
|
$this->version = trim(ltrim(file_get_contents($versionFile), 'v'));
|
|
|
|
} else {
|
|
|
|
$this->version = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->version;
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:06:33 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 15:11:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse and return this library's metadata
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError
|
|
|
|
* @throws JsonDecodeException
|
|
|
|
*/
|
|
|
|
protected function metaData()
|
|
|
|
{
|
|
|
|
if ($this->metaData === null) {
|
|
|
|
$metaData = file_get_contents($this->path . DIRECTORY_SEPARATOR . 'composer.json');
|
|
|
|
if ($metaData === false) {
|
|
|
|
throw new ConfigurationError('Library at "%s" is not a composerized project', $this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->metaData = Json::decode($metaData, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->metaData;
|
|
|
|
}
|
2020-11-10 14:06:33 +01:00
|
|
|
}
|