From 37f58e55d87de18e494d92daf7de2f199ce7991c Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Thu, 28 May 2015 14:21:05 +0200 Subject: [PATCH] Move macro resolcing functionality into separate class Make macro resolving functionality available to all code, instead of depending on a view resf #6392 --- .../application/views/helpers/IconImage.php | 6 +- .../views/helpers/ResolveMacros.php | 50 +------------- .../library/Monitoring/Object/Host.php | 2 +- .../library/Monitoring/Object/Macro.php | 67 +++++++++++++++++++ .../library/Monitoring/Object/Service.php | 2 +- .../views/helpers/ResolveMacrosTest.php | 25 +++---- 6 files changed, 86 insertions(+), 66 deletions(-) create mode 100644 modules/monitoring/library/Monitoring/Object/Macro.php diff --git a/modules/monitoring/application/views/helpers/IconImage.php b/modules/monitoring/application/views/helpers/IconImage.php index e0e969db4..ccd74ee3c 100644 --- a/modules/monitoring/application/views/helpers/IconImage.php +++ b/modules/monitoring/application/views/helpers/IconImage.php @@ -1,6 +1,8 @@ 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, diff --git a/modules/monitoring/application/views/helpers/ResolveMacros.php b/modules/monitoring/application/views/helpers/ResolveMacros.php index 7d555f8fd..e6566936e 100644 --- a/modules/monitoring/application/views/helpers/ResolveMacros.php +++ b/modules/monitoring/application/views/helpers/ResolveMacros.php @@ -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); } } diff --git a/modules/monitoring/library/Monitoring/Object/Host.php b/modules/monitoring/library/Monitoring/Object/Host.php index 6bf6b05cc..da3d66613 100644 --- a/modules/monitoring/library/Monitoring/Object/Host.php +++ b/modules/monitoring/library/Monitoring/Object/Host.php @@ -7,7 +7,7 @@ use InvalidArgumentException; use Icinga\Module\Monitoring\Backend\MonitoringBackend; /** - * A Icinga host + * An Icinga host */ class Host extends MonitoredObject { diff --git a/modules/monitoring/library/Monitoring/Object/Macro.php b/modules/monitoring/library/Monitoring/Object/Macro.php new file mode 100644 index 000000000..4a9045b58 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Object/Macro.php @@ -0,0 +1,67 @@ + '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; + } +} diff --git a/modules/monitoring/library/Monitoring/Object/Service.php b/modules/monitoring/library/Monitoring/Object/Service.php index 17fd143b5..6b5cd5bf9 100644 --- a/modules/monitoring/library/Monitoring/Object/Service.php +++ b/modules/monitoring/library/Monitoring/Object/Service.php @@ -7,7 +7,7 @@ use InvalidArgumentException; use Icinga\Module\Monitoring\Backend\MonitoringBackend; /** - * A Icinga service + * An Icinga service */ class Service extends MonitoredObject { diff --git a/modules/monitoring/test/php/application/views/helpers/ResolveMacrosTest.php b/modules/monitoring/test/php/application/views/helpers/ResolveMacrosTest.php index 31bf839a3..dc05f1e3d 100644 --- a/modules/monitoring/test/php/application/views/helpers/ResolveMacrosTest.php +++ b/modules/monitoring/test/php/application/views/helpers/ResolveMacrosTest.php @@ -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'] ); }