bundle/w3des/AdminBundle/src/Model/NodeView.php line 22

Open in your IDE?
  1. <?php
  2. namespace w3des\AdminBundle\Model;
  3. use w3des\AdminBundle\Entity\Node;
  4. use w3des\AdminBundle\Service\Nodes;
  5. use Symfony\Component\Routing\RouterInterface;
  6. /**
  7. * @property-read NodeView[] $children
  8. * @property-read NodeView $parent
  9. * @property-read ModuleInfo[][] $modules
  10. * @property-read int $pos
  11. * @property-read int $id
  12. * @property-read string|null $url
  13. * @property-read bool $local
  14. * @property-read string $type
  15. * @property-read service $service
  16. * @property-read Node $model
  17. * @property-read ValuesMap $vars
  18. *
  19. */
  20. class NodeView
  21. {
  22. private Nodes $_nodes;
  23. private Node $_model;
  24. private $_children;
  25. private $_parent;
  26. private $_modules;
  27. private $_vars;
  28. public function __construct(Nodes $nodes, Node $node, $parent = null)
  29. {
  30. $this->_nodes = $nodes;
  31. $this->_model = $node;
  32. if ($parent) {
  33. $this->_parent = $parent;
  34. }
  35. }
  36. public function __get($name)
  37. {
  38. if ($name === 'model') {
  39. return $this->_model;
  40. }
  41. if ($name === 'children') {
  42. if ($this->_children === null) {
  43. $this->_children = $this->_nodes->wrapArray($this->model->getChildren());
  44. }
  45. return $this->_children;
  46. }
  47. if ($name === 'parent') {
  48. if ($this->_parent === null && $this->model->getParent() !== null) {
  49. $this->_parent = $this->_nodes->wrap($this->model->getParent());
  50. }
  51. return $this->_parent;
  52. }
  53. if ($name === 'local') {
  54. $url = $this->_nodes->hasUrl($this->model);
  55. return $url ? true : false;
  56. }
  57. if ($name === 'url') {
  58. return $this->url();
  59. }
  60. if ($name === 'vars') {
  61. if ($this->_vars === null) {
  62. $this->_vars = $this->_nodes->wrapVariables($this->_nodes->getVariables($this->model));
  63. }
  64. return $this->_vars;
  65. }
  66. if ($name === 'modules') {
  67. if ($this->_modules === null) {
  68. $this->calcModules();
  69. }
  70. return $this->_modules;
  71. }
  72. return $this->model->{'get' . $name}();
  73. }
  74. private function calcModules()
  75. {
  76. $this->_modules = [];
  77. foreach ($this->_nodes->getNodeCfg($this->model->getType())['modules'] as $sectionName => $info) {
  78. $this->_modules[$sectionName] = [];
  79. foreach ($this->vars->{$info['field']} as $module) {
  80. $mod = $this->_nodes->wrap($module->model, $this);
  81. $this->_modules[$sectionName][] = $mod;
  82. }
  83. }
  84. }
  85. public function url($locale = null, $mode = RouterInterface::ABSOLUTE_PATH)
  86. {
  87. $url = $this->_nodes->getUrl($this->model, $mode, $locale);
  88. if ($url) {
  89. return $url;
  90. } elseif ($this->vars->has('url') && ($locale == null || $locale == $this->locale)) {
  91. return $this->vars->get('url');
  92. }
  93. return null;
  94. }
  95. public function __isset($name)
  96. {
  97. return true;
  98. }
  99. public function __sleep()
  100. {
  101. return [
  102. '_vars','_children', '_modules', '_model', '_parent'
  103. ];
  104. }
  105. public function __wakeup()
  106. {
  107. $this->_nodes = $GLOBALS['kernel']->getContainer()->get('nodes');
  108. }
  109. public function _fetch()
  110. {
  111. $this->vars;
  112. $this->modules;
  113. $this->model->getUrls()->toArray();
  114. foreach ($this->model->getPositions() as $p) {
  115. $p->getTarget()->getUrls()->toArray();
  116. }
  117. foreach ($this->vars->getValues() as $value) {
  118. foreach (is_array($value) ? $value : [$value] as $v) {
  119. if ($v instanceof NodeView) {
  120. $v->_fetch();
  121. }
  122. }
  123. }
  124. foreach ($this->modules as $mods) {
  125. foreach ($mods as $item) {
  126. $item->vars;
  127. }
  128. }
  129. }
  130. }