Skip to content

Commit

Permalink
Refactor TLVTree to not have a View attribute
Browse files Browse the repository at this point in the history
Reasoning: The mental model was a bit confusing, since
the Model\View reads the YAML and has a Tree attribute, while
this Tree had the same View as attribute.

But we only need some attributes from the View in the Tree,
might even be possible to use Model\View instead of Tree in some cases
and then use view->getTree().
  • Loading branch information
martialblog committed Aug 2, 2024
1 parent 55dec68 commit c7a350b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion application/views/helpers/Tiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function tiles(TLVTreeNode $node, $levels = 2, $classes = array())
$title . $badges,
'toplevelview/show/tree',
array(
'name' => $node->getRoot()->getView()->getName(),
'name' => $node->getRoot()->getViewName(),
'id' => $node->getFullId()
),
array(
Expand Down
2 changes: 1 addition & 1 deletion application/views/helpers/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function tree(TLVTreeNode $node, $classes = array(), $level = 0)
$url = Url::fromPath(
'toplevelview/show/tree',
array(
'name' => $node->getRoot()->getView()->getName(),
'name' => $node->getRoot()->getViewName(),
'id' => $node->getFullId()
)
);
Expand Down
3 changes: 2 additions & 1 deletion library/Toplevelview/Model/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public function getTree(): TLVTree
if ($this->tree === null) {
$this->ensureParsed();
$this->tree = $tree = TLVTree::fromArray($this->raw);
$tree->setView($this);
$tree->setViewName($this->getName());
$tree->setViewChecksum($this->getTextChecksum());
}
return $this->tree;
}
Expand Down
43 changes: 21 additions & 22 deletions library/Toplevelview/Tree/TLVTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ class TLVTree extends TLVTreeNode

protected $cacheLifetime = 60;

/**
* @var View
*/
protected $view;
protected $viewName;

protected $viewChecksum;

/**
* Return a child by its ID
Expand Down Expand Up @@ -70,22 +69,25 @@ public function getById($id)
return $currentNode;
}

/**
* @return View
*/
public function getView()
public function getViewName(): string
{
return $this->view;
return $this->viewName;
}

/**
* @param View $view
*
* @return $this
*/
public function setView(View $view)
public function setViewName(string $name)
{
$this->viewName = $name;
return $this;
}

public function getViewChecksum(): string
{
return $this->viewChecksum;
}

public function setViewChecksum(string $checksum)
{
$this->view = $view;
$this->viewChecksum = $checksum;
return $this;
}

Expand All @@ -111,12 +113,9 @@ public function registerObject($type, $name, $class)

protected function getCacheName()
{
$view = $this->getView();
return sprintf(
'%s-%s.json',
$view->getName(),
$view->getTextChecksum()
);
$n = $this->getViewName();
$c = $this->getViewChecksum();
return sprintf('%s-%s.json', $n, $c);
}

protected function loadCache()
Expand Down
12 changes: 12 additions & 0 deletions test/php/library/Toplevelview/ViewConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Icinga\Module\Toplevelview\ViewConfig;

use Icinga\Exception\NotReadableError;
use Icinga\Exception\NotFoundError;
use PHPUnit\Framework\TestCase;

final class ViewConfigTest extends TestCase
Expand All @@ -31,4 +32,15 @@ public function testViewConfigWithNoError()
$this->assertSame(null, $clone->getName());
$this->assertFalse($clone->hasBeenLoaded());
}

public function testViewConfigWithTree()
{
$this->expectException(NotFoundError::class);

$c = new ViewConfig('test/testdata');
$view = $c->loadByName('example');

$t = $view->getTree();
$t->getById('0-1-2');
}
}

0 comments on commit c7a350b

Please sign in to comment.