Move macro resolcing functionality into separate class

Make macro resolving functionality available to all code, instead of depending on a view

resf #6392
This commit is contained in:
Matthias Jentsch 2015-05-28 14:21:05 +02:00
parent 6c44f6a11a
commit 37f58e55d8
6 changed files with 86 additions and 66 deletions

View File

@ -1,6 +1,8 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
use Icinga\Module\Monitoring\Object\Macro;
/**
* Generate icons to describe a given hosts state
*/
@ -26,7 +28,7 @@ class Zend_View_Helper_IconImage extends Zend_View_Helper_Abstract
{
if ($object->host_icon_image && ! preg_match('/[\'"]/', $object->host_icon_image)) {
return $this->view->img(
'img/icons/' . $this->view->resolveMacros($object->host_icon_image, $object),
'img/icons/' . Macro::resolveMacro($object->host_icon_image, $object),
null,
array(
'alt' => $object->host_icon_image_alt,
@ -48,7 +50,7 @@ class Zend_View_Helper_IconImage extends Zend_View_Helper_Abstract
{
if ($object->service_icon_image && ! preg_match('/[\'"]/', $object->service_icon_image)) {
return $this->view->img(
'img/icons/' . $this->view->resolveMacros($object->service_icon_image, $object),
'img/icons/' . Macro::resolveMacros($object->service_icon_image, $object),
null,
array(
'alt' => $object->service_icon_image_alt,

View File

@ -2,61 +2,17 @@
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
use \Zend_View_Helper_Abstract;
use Icinga\Module\Monitoring\Object\MonitoredObject;
use Icinga\Module\Monitoring\Object\Macro;
class Zend_View_Helper_ResolveMacros extends Zend_View_Helper_Abstract
{
/**
* Known icinga macros
*
* @var array
*/
private $icingaMacros = array(
'HOSTNAME' => 'host_name',
'HOSTADDRESS' => 'host_address',
'SERVICEDESC' => 'service_description'
);
/**
* Return the given string with macros being resolved
*
* @param string $input The string in which to look for macros
* @param MonitoredObject|stdClass $object The host or service used to resolve macros
*
* @return string The substituted or unchanged string
*/
public function resolveMacros($input, $object)
{
$matches = array();
if (preg_match_all('@\$([^\$\s]+)\$@', $input, $matches)) {
foreach ($matches[1] as $key => $value) {
$newValue = $this->resolveMacro($value, $object);
if ($newValue !== $value) {
$input = str_replace($matches[0][$key], $newValue, $input);
}
}
}
return $input;
return Macro::resolveMacros($input, $object);
}
/**
* Resolve a macro based on the given object
*
* @param string $macro The macro to resolve
* @param MonitoredObject|stdClass $object The object used to resolve the macro
*
* @return string The new value or the macro if it cannot be resolved
*/
public function resolveMacro($macro, $object)
{
if (array_key_exists($macro, $this->icingaMacros) && $object->{$this->icingaMacros[$macro]} !== false) {
return $object->{$this->icingaMacros[$macro]};
}
if (array_key_exists($macro, $object->customvars)) {
return $object->customvars[$macro];
}
return $macro;
return Macro::resolveMacro($macro, $object);
}
}

View File

@ -7,7 +7,7 @@ use InvalidArgumentException;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
/**
* A Icinga host
* An Icinga host
*/
class Host extends MonitoredObject
{

View File

@ -0,0 +1,67 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Object;
/**
* Expand macros in string in the context of MonitoredObjects
*/
class Macro
{
/**
* Known icinga macros
*
* @var array
*/
private static $icingaMacros = array(
'HOSTNAME' => 'host_name',
'HOSTADDRESS' => 'host_address',
'SERVICEDESC' => 'service_description',
'host.name' => 'host_name',
'host.address' => 'host_address',
'service.description' => 'service_description'
);
/**
* Return the given string with macros being resolved
*
* @param string $input The string in which to look for macros
* @param MonitoredObject|stdClass $object The host or service used to resolve macros
*
* @return string The substituted or unchanged string
*/
public static function resolveMacros($input, $object)
{
$matches = array();
if (preg_match_all('@\$([^\$\s]+)\$@', $input, $matches)) {
foreach ($matches[1] as $key => $value) {
$newValue = self::resolveMacro($value, $object);
if ($newValue !== $value) {
$input = str_replace($matches[0][$key], $newValue, $input);
}
}
}
return $input;
}
/**
* Resolve a macro based on the given object
*
* @param string $macro The macro to resolve
* @param MonitoredObject|stdClass $object The object used to resolve the macro
*
* @return string The new value or the macro if it cannot be resolved
*/
public static function resolveMacro($macro, $object)
{
if (array_key_exists($macro, self::$icingaMacros) && $object->{self::$icingaMacros[$macro]} !== false) {
return $object->{self::$icingaMacros[$macro]};
}
if (array_key_exists($macro, $object->customvars)) {
return $object->customvars[$macro];
}
return $macro;
}
}

View File

@ -7,7 +7,7 @@ use InvalidArgumentException;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
/**
* A Icinga service
* An Icinga service
*/
class Service extends MonitoredObject
{

View File

@ -4,10 +4,10 @@
namespace Tests\Icinga\Modules\Monitoring\Application\Views\Helpers;
use Mockery;
use Zend_View_Helper_ResolveMacros;
use Icinga\Test\BaseTestCase;
use Icinga\Module\Monitoring\Object\Macro;
require_once realpath(BaseTestCase::$moduleDir . '/monitoring/application/views/helpers/ResolveMacros.php');
require_once realpath(BaseTestCase::$moduleDir . '/monitoring/library/Monitoring/Object/Macro.php');
class ResolveMacrosTest extends BaseTestCase
{
@ -17,9 +17,8 @@ class ResolveMacrosTest extends BaseTestCase
$hostMock->host_name = 'test';
$hostMock->host_address = '1.1.1.1';
$helper = new Zend_View_Helper_ResolveMacros();
$this->assertEquals($helper->resolveMacros('$HOSTNAME$', $hostMock), $hostMock->host_name);
$this->assertEquals($helper->resolveMacros('$HOSTADDRESS$', $hostMock), $hostMock->host_address);
$this->assertEquals(Macro::resolveMacros('$HOSTNAME$', $hostMock), $hostMock->host_name);
$this->assertEquals(Macro::resolveMacros('$HOSTADDRESS$', $hostMock), $hostMock->host_address);
}
public function testServiceMacros()
@ -29,10 +28,9 @@ class ResolveMacrosTest extends BaseTestCase
$svcMock->host_address = '1.1.1.1';
$svcMock->service_description = 'a service';
$helper = new Zend_View_Helper_ResolveMacros();
$this->assertEquals($helper->resolveMacros('$HOSTNAME$', $svcMock), $svcMock->host_name);
$this->assertEquals($helper->resolveMacros('$HOSTADDRESS$', $svcMock), $svcMock->host_address);
$this->assertEquals($helper->resolveMacros('$SERVICEDESC$', $svcMock), $svcMock->service_description);
$this->assertEquals(Macro::resolveMacros('$HOSTNAME$', $svcMock), $svcMock->host_name);
$this->assertEquals(Macro::resolveMacros('$HOSTADDRESS$', $svcMock), $svcMock->host_address);
$this->assertEquals(Macro::resolveMacros('$SERVICEDESC$', $svcMock), $svcMock->service_description);
}
public function testCustomvars()
@ -42,8 +40,7 @@ class ResolveMacrosTest extends BaseTestCase
'CUSTOMVAR' => 'test'
);
$helper = new Zend_View_Helper_ResolveMacros();
$this->assertEquals($helper->resolveMacros('$CUSTOMVAR$', $objectMock), $objectMock->customvars['CUSTOMVAR']);
$this->assertEquals(Macro::resolveMacros('$CUSTOMVAR$', $objectMock), $objectMock->customvars['CUSTOMVAR']);
}
public function testFaultyMacros()
@ -55,9 +52,8 @@ class ResolveMacrosTest extends BaseTestCase
'NAME' => 'st'
);
$helper = new Zend_View_Helper_ResolveMacros();
$this->assertEquals(
$helper->resolveMacros('$$HOSTNAME$ $ HOSTNAME$ $HOST$NAME$', $hostMock),
Macro::resolveMacros('$$HOSTNAME$ $ HOSTNAME$ $HOST$NAME$', $hostMock),
'$test $ HOSTNAME$ teNAME$'
);
}
@ -69,9 +65,8 @@ class ResolveMacrosTest extends BaseTestCase
'V€RY_SP3C|@L' => 'not too special!'
);
$helper = new Zend_View_Helper_ResolveMacros();
$this->assertEquals(
$helper->resolveMacros('$V€RY_SP3C|@L$', $objectMock),
Macro::resolveMacros('$V€RY_SP3C|@L$', $objectMock),
$objectMock->customvars['V€RY_SP3C|@L']
);
}