<?php
namespace w3des\AdminBundle\Model;
use w3des\AdminBundle\Entity\Node;
use w3des\AdminBundle\Service\Nodes;
use Symfony\Component\Routing\RouterInterface;
/**
* @property-read NodeView[] $children
* @property-read NodeView $parent
* @property-read ModuleInfo[][] $modules
* @property-read int $pos
* @property-read int $id
* @property-read string|null $url
* @property-read bool $local
* @property-read string $type
* @property-read service $service
* @property-read Node $model
* @property-read ValuesMap $vars
*
*/
class NodeView
{
private Nodes $_nodes;
private Node $_model;
private $_children;
private $_parent;
private $_modules;
private $_vars;
public function __construct(Nodes $nodes, Node $node, $parent = null)
{
$this->_nodes = $nodes;
$this->_model = $node;
if ($parent) {
$this->_parent = $parent;
}
}
public function __get($name)
{
if ($name === 'model') {
return $this->_model;
}
if ($name === 'children') {
if ($this->_children === null) {
$this->_children = $this->_nodes->wrapArray($this->model->getChildren());
}
return $this->_children;
}
if ($name === 'parent') {
if ($this->_parent === null && $this->model->getParent() !== null) {
$this->_parent = $this->_nodes->wrap($this->model->getParent());
}
return $this->_parent;
}
if ($name === 'local') {
$url = $this->_nodes->hasUrl($this->model);
return $url ? true : false;
}
if ($name === 'url') {
return $this->url();
}
if ($name === 'vars') {
if ($this->_vars === null) {
$this->_vars = $this->_nodes->wrapVariables($this->_nodes->getVariables($this->model));
}
return $this->_vars;
}
if ($name === 'modules') {
if ($this->_modules === null) {
$this->calcModules();
}
return $this->_modules;
}
return $this->model->{'get' . $name}();
}
private function calcModules()
{
$this->_modules = [];
foreach ($this->_nodes->getNodeCfg($this->model->getType())['modules'] as $sectionName => $info) {
$this->_modules[$sectionName] = [];
foreach ($this->vars->{$info['field']} as $module) {
$mod = $this->_nodes->wrap($module->model, $this);
$this->_modules[$sectionName][] = $mod;
}
}
}
public function url($locale = null, $mode = RouterInterface::ABSOLUTE_PATH)
{
$url = $this->_nodes->getUrl($this->model, $mode, $locale);
if ($url) {
return $url;
} elseif ($this->vars->has('url') && ($locale == null || $locale == $this->locale)) {
return $this->vars->get('url');
}
return null;
}
public function __isset($name)
{
return true;
}
public function __sleep()
{
return [
'_vars','_children', '_modules', '_model', '_parent'
];
}
public function __wakeup()
{
$this->_nodes = $GLOBALS['kernel']->getContainer()->get('nodes');
}
public function _fetch()
{
$this->vars;
$this->modules;
$this->model->getUrls()->toArray();
foreach ($this->model->getPositions() as $p) {
$p->getTarget()->getUrls()->toArray();
}
foreach ($this->vars->getValues() as $value) {
foreach (is_array($value) ? $value : [$value] as $v) {
if ($v instanceof NodeView) {
$v->_fetch();
}
}
}
foreach ($this->modules as $mods) {
foreach ($mods as $item) {
$item->vars;
}
}
}
}