‪TYPO3CMS  9.5
DependencyResolver.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
23 {
27  protected ‪$workspace = 0;
28 
32  protected ‪$factory;
33 
37  protected ‪$elements = [];
38 
42  protected ‪$eventCallbacks = [];
43 
47  protected ‪$outerMostParentsRequireReferences = false;
48 
52  protected ‪$outerMostParents;
53 
59  public function ‪setWorkspace(‪$workspace)
60  {
61  $this->workspace = (int)‪$workspace;
62  }
63 
69  public function ‪getWorkspace()
70  {
71  return ‪$this->workspace;
72  }
73 
81  public function ‪setEventCallback($eventName, ‪EventCallback $callback)
82  {
83  $this->eventCallbacks[$eventName] = $callback;
84  return $this;
85  }
86 
95  public function ‪executeEventCallback($eventName, $caller, array $callerArguments = [])
96  {
97  if (isset($this->eventCallbacks[$eventName])) {
99  $callback = $this->eventCallbacks[$eventName];
100  return $callback->execute($callerArguments, $caller, $eventName);
101  }
102  return null;
103  }
104 
112  {
113  $this->outerMostParentsRequireReferences = (bool)‪$outerMostParentsRequireReferences;
114  return $this;
115  }
116 
125  public function ‪addElement($table, $id, array $data = [])
126  {
127  $element = $this->‪getFactory()->‪getElement($table, $id, $data, $this);
128  $elementName = $element->‪__toString();
129  $this->elements[$elementName] = $element;
130  return $element;
131  }
132 
138  public function ‪getOuterMostParents()
139  {
140  if (!isset($this->outerMostParents)) {
141  $this->outerMostParents = [];
143  foreach ($this->elements as $element) {
144  $this->‪processOuterMostParent($element);
145  }
146  }
148  }
149 
155  protected function ‪processOuterMostParent(‪ElementEntity $element)
156  {
157  if ($this->outerMostParentsRequireReferences === false || $element->‪hasReferences()) {
158  $outerMostParent = $element->‪getOuterMostParent();
159  if ($outerMostParent !== false) {
160  $outerMostParentName = $outerMostParent->‪__toString();
161  if (!isset($this->outerMostParents[$outerMostParentName])) {
162  $this->outerMostParents[$outerMostParentName] = $outerMostParent;
163  }
164  }
165  }
166  }
167 
175  public function ‪getNestedElements(‪ElementEntity $outerMostParent)
176  {
177  $outerMostParentName = $outerMostParent->‪__toString();
178  if (!isset($this->outerMostParents[$outerMostParentName])) {
179  throw new \RuntimeException('Element "' . $outerMostParentName . '" was not detected as outermost parent.', 1289318609);
180  }
181  $nestedStructure = array_merge([$outerMostParentName => $outerMostParent], $outerMostParent->‪getNestedChildren());
182  return $nestedStructure;
183  }
184 
190  public function ‪getElements()
191  {
192  return ‪$this->elements;
193  }
194 
200  public function ‪getFactory()
201  {
202  if (!isset($this->factory)) {
203  $this->factory = GeneralUtility::makeInstance(DependencyEntityFactory::class);
204  }
205  return ‪$this->factory;
206  }
207 }
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$eventCallbacks
‪array $eventCallbacks
Definition: DependencyResolver.php:38
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$workspace
‪int $workspace
Definition: DependencyResolver.php:26
‪TYPO3\CMS\Workspaces\Dependency\DependencyEntityFactory\getElement
‪ElementEntity getElement($table, $id, array $data=[], DependencyResolver $dependency)
Definition: DependencyEntityFactory.php:41
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__toString
‪string __toString()
Definition: ElementEntity.php:191
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$outerMostParents
‪array $outerMostParents
Definition: DependencyResolver.php:46
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity
Definition: ElementEntity.php:24
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\setWorkspace
‪setWorkspace($workspace)
Definition: DependencyResolver.php:53
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\executeEventCallback
‪mixed executeEventCallback($eventName, $caller, array $callerArguments=[])
Definition: DependencyResolver.php:89
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver
Definition: DependencyResolver.php:23
‪TYPO3\CMS\Workspaces\Dependency\EventCallback
Definition: EventCallback.php:21
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getFactory
‪DependencyEntityFactory getFactory()
Definition: DependencyResolver.php:194
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getElements
‪array getElements()
Definition: DependencyResolver.php:184
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$outerMostParentsRequireReferences
‪bool $outerMostParentsRequireReferences
Definition: DependencyResolver.php:42
‪TYPO3\CMS\Workspaces\Dependency
Definition: DependencyEntityFactory.php:2
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getWorkspace
‪int getWorkspace()
Definition: DependencyResolver.php:63
‪TYPO3\CMS\Workspaces\Dependency\DependencyEntityFactory
Definition: DependencyEntityFactory.php:23
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getOuterMostParent
‪ElementEntity bool getOuterMostParent()
Definition: ElementEntity.php:346
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getOuterMostParents
‪array ElementEntity[] getOuterMostParents()
Definition: DependencyResolver.php:132
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$factory
‪DependencyEntityFactory $factory
Definition: DependencyResolver.php:30
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasReferences
‪bool hasReferences()
Definition: ElementEntity.php:336
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$elements
‪array $elements
Definition: DependencyResolver.php:34
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\setEventCallback
‪DependencyResolver setEventCallback($eventName, EventCallback $callback)
Definition: DependencyResolver.php:75
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getNestedChildren
‪array ReferenceEntity[] getNestedChildren()
Definition: ElementEntity.php:375
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\addElement
‪ElementEntity addElement($table, $id, array $data=[])
Definition: DependencyResolver.php:119
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\setOuterMostParentsRequireReferences
‪DependencyResolver setOuterMostParentsRequireReferences($outerMostParentsRequireReferences)
Definition: DependencyResolver.php:105
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getNestedElements
‪array getNestedElements(ElementEntity $outerMostParent)
Definition: DependencyResolver.php:169
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\processOuterMostParent
‪processOuterMostParent(ElementEntity $element)
Definition: DependencyResolver.php:149