DirectorDb: new controller extension

This commit is contained in:
Thomas Gelf 2017-06-14 14:55:11 +02:00
parent 7502706284
commit e88c13663f
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<?php
namespace Icinga\Module\Director\Web\Controller\Extension;
use Icinga\Module\Director\Db;
trait DirectorDb
{
/** @var Db */
private $db;
/**
* @throws \Icinga\Exception\ConfigurationError
*
* @return Db
*/
public function db()
{
if ($this->db === null) {
$resourceName = $this->Config()->get('db', 'resource');
if ($resourceName) {
$this->db = Db::fromResourceName($resourceName);
} else {
if ($this->getRequest()->isApiRequest()) {
throw new ConfigurationError('Icinga Director is not correctly configured');
} else {
$this->redirectNow('director');
}
}
}
return $this->db;
}
}