mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-30 09:14:08 +02:00
lib: Add a not yet customizable node renderer
This commit is contained in:
parent
ce0aee5e41
commit
6c8d35c667
58
library/Icinga/Data/Tree/NodeRenderer.php
Normal file
58
library/Icinga/Data/Tree/NodeRenderer.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}
|
||||||
|
|
||||||
|
namespace Icinga\Data\Tree;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use RecursiveIteratorIterator;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A not yet customizable node renderer
|
||||||
|
*/
|
||||||
|
class NodeRenderer extends RecursiveIteratorIterator
|
||||||
|
{
|
||||||
|
protected $content = array();
|
||||||
|
|
||||||
|
public function __construct(NodeInterface $node)
|
||||||
|
{
|
||||||
|
parent::__construct($node, RecursiveIteratorIterator::SELF_FIRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function beginIteration()
|
||||||
|
{
|
||||||
|
$this->content[] = '<ul>';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function endIteration()
|
||||||
|
{
|
||||||
|
$this->content[] = '</ul>';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function beginChildren()
|
||||||
|
{
|
||||||
|
$this->content[] = '<ul>';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function endChildren()
|
||||||
|
{
|
||||||
|
$this->content[] = '</ul>';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render($callback)
|
||||||
|
{
|
||||||
|
if (! is_callable($callback)) {
|
||||||
|
throw new RuntimeException('Callable expected');
|
||||||
|
}
|
||||||
|
foreach ($this as $node) {
|
||||||
|
try {
|
||||||
|
$content = call_user_func($callback, $node);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
throw new RuntimeException($e);
|
||||||
|
}
|
||||||
|
$this->content[] = $content;
|
||||||
|
}
|
||||||
|
return implode("\n", $this->content);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user