mirror of
https://github.com/Icinga/icingaweb2-module-graphite.git
synced 2026-05-28 04:34:57 -04:00
TemplateSet/Store: new abstraction layer
This commit is contained in:
parent
10fbc18e5e
commit
0824814df1
2 changed files with 207 additions and 0 deletions
105
library/Graphite/TemplateSet.php
Normal file
105
library/Graphite/TemplateSet.php
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Graphite;
|
||||
|
||||
use DirectoryIterator;
|
||||
|
||||
class TemplateSet
|
||||
{
|
||||
protected $name;
|
||||
|
||||
protected $title;
|
||||
|
||||
protected $basedir;
|
||||
|
||||
protected $patterns = array();
|
||||
|
||||
public function __construct($configfile)
|
||||
{
|
||||
$this->basedir = dirname($configfile);
|
||||
$this->name = basename($this->basedir);
|
||||
|
||||
$config = parse_ini_file($configfile, true);
|
||||
|
||||
if (isset($config['templateset']['name'])) {
|
||||
$this->title = $config['templateset']['name'];
|
||||
}
|
||||
|
||||
if (isset($config['patterns'])) {
|
||||
$this->patterns = $config['patterns'];
|
||||
}
|
||||
}
|
||||
|
||||
public function getBasePatterns()
|
||||
{
|
||||
return $this->patterns;
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
if ($this->title === null) {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function enumTemplates()
|
||||
{
|
||||
$enum = array();
|
||||
return $this->extendEnumTemplates($enum);
|
||||
}
|
||||
|
||||
public function extendEnumTemplates(& $enum)
|
||||
{
|
||||
foreach ($this->loadTemplates() as $key => $template) {
|
||||
$enum[sprintf('%s/%s', $this->name, $key)] = $template->getTitle();
|
||||
}
|
||||
|
||||
return $enum;
|
||||
}
|
||||
|
||||
public function loadTemplate($name)
|
||||
{
|
||||
return $this->loadTemplates($name);
|
||||
}
|
||||
|
||||
public function loadTemplates($name = null)
|
||||
{
|
||||
$dir = $this->basedir;
|
||||
$templates = array();
|
||||
|
||||
foreach (new DirectoryIterator($dir) as $file) {
|
||||
if ($file->isDot()) continue;
|
||||
$filename = $file->getFilename();
|
||||
if (substr($filename, -5) === '.conf') {
|
||||
$tname = substr($filename, 0, -5);
|
||||
if ($name !== null) {
|
||||
if ($name !== $tname) continue;
|
||||
}
|
||||
$templates[$tname] = GraphTemplate::load(
|
||||
file_get_contents($file->getPathname())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($name !== null) {
|
||||
if (! array_key_exists($name, $templates)) {
|
||||
throw new NotFoundError(
|
||||
'The desired template "%s" doesn\'t exist',
|
||||
$name
|
||||
);
|
||||
}
|
||||
|
||||
return $templates[$name];
|
||||
}
|
||||
|
||||
ksort($templates);
|
||||
return $templates;
|
||||
}
|
||||
}
|
||||
102
library/Graphite/TemplateStore.php
Normal file
102
library/Graphite/TemplateStore.php
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Graphite;
|
||||
|
||||
use DirectoryIterator;
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Exception\NotFoundError;
|
||||
|
||||
class TemplateStore
|
||||
{
|
||||
protected $basedir;
|
||||
|
||||
public function __construct($basedir = null)
|
||||
{
|
||||
if ($basedir !== null) {
|
||||
$this->basedir = $basedir;
|
||||
}
|
||||
}
|
||||
|
||||
public function enumTemplateSets()
|
||||
{
|
||||
$enum = array();
|
||||
foreach ($this->loadTemplateSets() as $key => $set) {
|
||||
$enum[$key] = $set->getTitle();
|
||||
}
|
||||
|
||||
return $enum;
|
||||
}
|
||||
|
||||
public function getTemplateSets()
|
||||
{
|
||||
return $this->loadTemplateSets();
|
||||
}
|
||||
|
||||
public function enumTemplates()
|
||||
{
|
||||
$enum = array();
|
||||
foreach ($this->loadTemplateSets() as $set) {
|
||||
$enum[$set->getTitle()] = $set->enumTemplates();
|
||||
}
|
||||
|
||||
return $enum;
|
||||
}
|
||||
|
||||
public function loadTemplate($name)
|
||||
{
|
||||
list($set, $name) = preg_split('~/~', $name, 2);
|
||||
|
||||
return $this->loadTemplateSets($set)->loadTemplate($name);
|
||||
}
|
||||
|
||||
public function loadTemplateSets($name = null)
|
||||
{
|
||||
$dir = $this->getDir();
|
||||
$sets = array();
|
||||
|
||||
foreach (new DirectoryIterator($dir) as $file) {
|
||||
if ($file->isDot()) continue;
|
||||
if (! $file->isDir()) continue;
|
||||
$setname = $file->getFilename();
|
||||
$iniFilename = $file->getPathName() . '/templateset.ini';
|
||||
if (! is_readable($iniFilename)) continue;
|
||||
|
||||
$sets[$setname] = new TemplateSet($iniFilename);
|
||||
}
|
||||
|
||||
if ($name !== null) {
|
||||
if (! array_key_exists($name, $sets)) {
|
||||
throw new NotFoundError(
|
||||
'The desired template set "%s" doesn\'t exist',
|
||||
$name
|
||||
);
|
||||
}
|
||||
|
||||
return $sets[$name];
|
||||
}
|
||||
|
||||
ksort($sets);
|
||||
return $sets;
|
||||
}
|
||||
|
||||
protected function getDir($suffix = null)
|
||||
{
|
||||
$this->detectBasedir();
|
||||
if ($suffix === null) {
|
||||
return $this->basedir;
|
||||
} else {
|
||||
return $this->basedir . '/' . $suffix;
|
||||
}
|
||||
}
|
||||
|
||||
protected function detectBasedir()
|
||||
{
|
||||
if ($this->basedir === null) {
|
||||
$this->basedir = Icinga::app()
|
||||
->getModuleManager()
|
||||
->getModule('graphite')
|
||||
->getConfigDir()
|
||||
. '/templates';
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue