LegacyConfigRenderer: move rendering to dedicated

...class, simplifies LegacyStorage
This commit is contained in:
Thomas Gelf 2017-01-11 12:00:20 +01:00
parent a03feb03c5
commit a9331a1e05
2 changed files with 233 additions and 21 deletions

View file

@ -0,0 +1,231 @@
<?php
namespace Icinga\Module\Businessprocess\Storage;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\BusinessProcess;
class LegacyConfigRenderer
{
/** @var array */
protected $renderedNodes;
/**
* LecagyConfigRenderer constructor
*
* @param BusinessProcess $config
*/
public function __construct(BusinessProcess $config)
{
$this->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 '';
}
}
}

View file

@ -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