mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-28 16:24:04 +02:00
Library: Add accessors for meta data
This commit is contained in:
parent
a60f511cfc
commit
84c23fe92b
@ -3,10 +3,20 @@
|
|||||||
|
|
||||||
namespace Icinga\Application\Libraries;
|
namespace Icinga\Application\Libraries;
|
||||||
|
|
||||||
|
use Icinga\Exception\ConfigurationError;
|
||||||
|
use Icinga\Exception\Json\JsonDecodeException;
|
||||||
|
use Icinga\Util\Json;
|
||||||
|
|
||||||
class Library
|
class Library
|
||||||
{
|
{
|
||||||
protected $path;
|
protected $path;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $version;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $metaData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Library
|
* Create a new Library
|
||||||
*
|
*
|
||||||
@ -17,6 +27,39 @@ class Library
|
|||||||
$this->path = $path;
|
$this->path = $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register this library's autoloader
|
* Register this library's autoloader
|
||||||
*
|
*
|
||||||
@ -29,4 +72,26 @@ class Library
|
|||||||
require_once $autoloaderPath;
|
require_once $autoloaderPath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user