From a9331a1e05b6617103fa83a748e06078a983eaa9 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 11 Jan 2017 12:00:20 +0100 Subject: [PATCH] LegacyConfigRenderer: move rendering to dedicated ...class, simplifies LegacyStorage --- .../Storage/LegacyConfigRenderer.php | 231 ++++++++++++++++++ .../Businessprocess/Storage/LegacyStorage.php | 23 +- 2 files changed, 233 insertions(+), 21 deletions(-) create mode 100644 library/Businessprocess/Storage/LegacyConfigRenderer.php diff --git a/library/Businessprocess/Storage/LegacyConfigRenderer.php b/library/Businessprocess/Storage/LegacyConfigRenderer.php new file mode 100644 index 0000000..4b84160 --- /dev/null +++ b/library/Businessprocess/Storage/LegacyConfigRenderer.php @@ -0,0 +1,231 @@ +config = $config; + } + + /** + * @return string + */ + public function render() + { + return $this->renderHeader() . $this->renderNodes(); + } + + /** + * @param BusinessProcess $config + * @return mixed + */ + public static function renderConfig(BusinessProcess $config) + { + $renderer = new static($config); + return $renderer->render(); + } + + /** + * @return string + */ + public function renderHeader() + { + $str = "### Business Process Config File ###\n#\n"; + + $meta = $this->config->getMetadata(); + foreach ($meta->getProperties() as $key => $value) { + if ($value === null) { + continue; + } + + $str .= sprintf("# %-11s : %s\n", $key, $value); + } + + $str .= "#\n###################################\n\n"; + + return $str; + } + + /** + * @return string + */ + public function renderNodes() + { + $this->renderedNodes = array(); + + $config = $this->config; + $str = ''; + + foreach ($config->getRootNodes() as $node) { + $str .= $this->requireRenderedBpNode($node); + } + + foreach ($config->getUnboundNodes() as $name => $node) { + $str .= $this->requireRenderedBpNode($node); + } + + return $str . "\n"; + } + + /** + * Rendered node definition, empty string if already rendered + * + * @param BpNode $node + * + * @return string + */ + protected function requireRenderedBpNode(BpNode $node) + { + $name = $node->getName(); + + if (array_key_exists($name, $this->renderedNodes)) { + + return ''; + } else { + + return $this->renderBpNode($node); + } + } + + /** + * @param BpNode $node + * @return string + */ + protected function renderBpNode(BpNode $node) + { + $name = $node->getName(); + + // Doing this before rendering children allows us to store loops + $this->renderedNodes[$name] = true; + $cfg = ''; + + foreach ($node->getChildBpNodes() as $name => $child) { + $cfg .= $this->renderBpNode($child) . "\n"; + } + + $cfg .= static::renderSingleBpNode($node); + + return $cfg; + } + + /** + * @param BpNode $node + * @return string + */ + public static function renderEqualSign(BpNode $node) + { + $op = $node->getOperator(); + if (is_numeric($op)) { + return '= ' . $op . ' of:'; + } else { + return '='; + } + } + + /** + * @param BpNode $node + * @return string + */ + public static function renderOperator(BpNode $node) + { + $op = $node->getOperator(); + if (is_numeric($op)) { + return '+'; + } else { + return $op; + } + } + + /** + * @param BpNode $node + * @return string + */ + public static function renderSingleBpNode(BpNode $node) + { + return static::renderExpression($node) + . static::renderDisplay($node) + . static::renderInfoUrl($node); + } + + /** + * @param BpNode $node + * @return string + */ + public static function renderExpression(BpNode $node) + { + return sprintf( + "%s %s %s\n", + $node->getName(), + static::renderEqualSign($node), + static::renderChildNames($node) + ); + } + + /** + * @param BpNode $node + * @return string + */ + public static function renderChildNames(BpNode $node) + { + $op = static::renderOperator($node); + $children = $node->getChildNames(); + $str = implode(' ' . $op . ' ', $children); + + if ((count($children) < 2) && $op !== '&') { + + return $op . ' ' . $str; + } else { + + return $str; + } + } + + /** + * @param BpNode $node + * @return string + */ + protected function renderDisplay(BpNode $node) + { + if ($node->hasAlias() || $node->getDisplay() > 0) { + $prio = $node->getDisplay(); + return sprintf( + "display %s;%s;%s\n", + $prio, + $node->getName(), + $node->getAlias() + ); + } else { + return ''; + } + } + + /** + * @param BpNode $node + * @return string + */ + protected function renderInfoUrl(BpNode $node) + { + if ($node->hasInfoUrl()) { + return sprintf( + "info_url;%s;%s\n", + $node->getName(), + $node->getInfoUrl() + ); + } else { + return ''; + } + } +} diff --git a/library/Businessprocess/Storage/LegacyStorage.php b/library/Businessprocess/Storage/LegacyStorage.php index 8c66e43..b8bcd48 100644 --- a/library/Businessprocess/Storage/LegacyStorage.php +++ b/library/Businessprocess/Storage/LegacyStorage.php @@ -3,10 +3,7 @@ namespace Icinga\Module\Businessprocess\Storage; use DirectoryIterator; -use Icinga\Application\Benchmark; use Icinga\Application\Icinga; -use Icinga\Exception\ConfigurationError; -use Icinga\Module\Businessprocess\BpNode; use Icinga\Module\Businessprocess\BusinessProcess; use Icinga\Exception\SystemPermissionException; use Icinga\Module\Businessprocess\Metadata; @@ -173,15 +170,13 @@ class LegacyStorage extends Storage } /** - * @param BusinessProcess $process - * - * @return void + * @inheritdoc */ public function storeProcess(BusinessProcess $process) { file_put_contents( $this->getFilename($process->getName()), - $this->render($process) + LegacyConfigRenderer::renderConfig($process) ); } @@ -254,20 +249,6 @@ class LegacyStorage extends Storage return @unlink($this->getFilename($name)); } - /** - * @return BusinessProcess - */ - public function loadProcess($name) - { - Benchmark::measure('Loading business process ' . $name); - $bp = new BusinessProcess(); - $bp->setName($name); - $this->parseFile($name, $bp); - $this->loadHeader($name, $bp); - Benchmark::measure('Business process ' . $name . ' loaded'); - return $bp; - } - /** * @param $name * @return bool