‪TYPO3CMS  10.4
DependencyResolver.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 
24 {
28  protected ‪$workspace = 0;
29 
33  protected ‪$factory;
34 
38  protected ‪$elements = [];
39 
43  protected ‪$eventCallbacks = [];
44 
48  protected ‪$outerMostParentsRequireReferences = false;
49 
53  protected ‪$outerMostParents;
54 
60  public function ‪setWorkspace(‪$workspace)
61  {
62  $this->workspace = (int)‪$workspace;
63  }
64 
70  public function ‪getWorkspace()
71  {
72  return ‪$this->workspace;
73  }
74 
82  public function ‪setEventCallback($eventName, ‪EventCallback $callback)
83  {
84  $this->eventCallbacks[$eventName] = $callback;
85  return $this;
86  }
87 
96  public function ‪executeEventCallback($eventName, $caller, array $callerArguments = [])
97  {
98  if (isset($this->eventCallbacks[$eventName])) {
100  $callback = $this->eventCallbacks[$eventName];
101  return $callback->execute($callerArguments, $caller, $eventName);
102  }
103  return null;
104  }
105 
113  {
114  $this->outerMostParentsRequireReferences = (bool)‪$outerMostParentsRequireReferences;
115  return $this;
116  }
117 
126  public function ‪addElement($table, $id, array $data = [])
127  {
128  $element = $this->‪getFactory()->‪getElement($table, $id, $data, $this);
129  $elementName = $element->‪__toString();
130  $this->elements[$elementName] = $element;
131  return $element;
132  }
133 
139  public function ‪getOuterMostParents()
140  {
141  if (!isset($this->outerMostParents)) {
142  $this->outerMostParents = [];
144  foreach ($this->elements as $element) {
145  $this->‪processOuterMostParent($element);
146  }
147  }
149  }
150 
156  protected function ‪processOuterMostParent(‪ElementEntity $element)
157  {
158  if ($this->outerMostParentsRequireReferences === false || $element->‪hasReferences()) {
159  $outerMostParent = $element->‪getOuterMostParent();
160  if ($outerMostParent !== false) {
161  $outerMostParentName = $outerMostParent->‪__toString();
162  if (!isset($this->outerMostParents[$outerMostParentName])) {
163  $this->outerMostParents[$outerMostParentName] = $outerMostParent;
164  }
165  }
166  }
167  }
168 
176  public function ‪getNestedElements(‪ElementEntity $outerMostParent)
177  {
178  $outerMostParentName = $outerMostParent->‪__toString();
179  if (!isset($this->outerMostParents[$outerMostParentName])) {
180  throw new \RuntimeException('Element "' . $outerMostParentName . '" was not detected as outermost parent.', 1289318609);
181  }
182  $nestedStructure = array_merge([$outerMostParentName => $outerMostParent], $outerMostParent->‪getNestedChildren());
183  return $nestedStructure;
184  }
185 
191  public function ‪getElements()
192  {
193  return ‪$this->elements;
194  }
195 
201  public function ‪getFactory()
202  {
203  if (!isset($this->factory)) {
204  $this->factory = GeneralUtility::makeInstance(DependencyEntityFactory::class);
205  }
206  return ‪$this->factory;
207  }
208 }
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$eventCallbacks
‪array $eventCallbacks
Definition: DependencyResolver.php:39
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$workspace
‪int $workspace
Definition: DependencyResolver.php:27
‪TYPO3\CMS\Workspaces\Dependency\DependencyEntityFactory\getElement
‪ElementEntity getElement($table, $id, array $data=[], DependencyResolver $dependency)
Definition: DependencyEntityFactory.php:42
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__toString
‪string __toString()
Definition: ElementEntity.php:192
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$outerMostParents
‪array $outerMostParents
Definition: DependencyResolver.php:47
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity
Definition: ElementEntity.php:25
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\setWorkspace
‪setWorkspace($workspace)
Definition: DependencyResolver.php:54
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\executeEventCallback
‪mixed executeEventCallback($eventName, $caller, array $callerArguments=[])
Definition: DependencyResolver.php:90
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver
Definition: DependencyResolver.php:24
‪TYPO3\CMS\Workspaces\Dependency\EventCallback
Definition: EventCallback.php:22
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getFactory
‪DependencyEntityFactory getFactory()
Definition: DependencyResolver.php:195
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getElements
‪array getElements()
Definition: DependencyResolver.php:185
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$outerMostParentsRequireReferences
‪bool $outerMostParentsRequireReferences
Definition: DependencyResolver.php:43
‪TYPO3\CMS\Workspaces\Dependency
Definition: DependencyEntityFactory.php:16
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getWorkspace
‪int getWorkspace()
Definition: DependencyResolver.php:64
‪TYPO3\CMS\Workspaces\Dependency\DependencyEntityFactory
Definition: DependencyEntityFactory.php:24
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getOuterMostParent
‪ElementEntity bool getOuterMostParent()
Definition: ElementEntity.php:347
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getOuterMostParents
‪array ElementEntity[] getOuterMostParents()
Definition: DependencyResolver.php:133
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$factory
‪DependencyEntityFactory $factory
Definition: DependencyResolver.php:31
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasReferences
‪bool hasReferences()
Definition: ElementEntity.php:337
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\$elements
‪array $elements
Definition: DependencyResolver.php:35
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\setEventCallback
‪DependencyResolver setEventCallback($eventName, EventCallback $callback)
Definition: DependencyResolver.php:76
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getNestedChildren
‪array ReferenceEntity[] getNestedChildren()
Definition: ElementEntity.php:376
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\addElement
‪ElementEntity addElement($table, $id, array $data=[])
Definition: DependencyResolver.php:120
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\setOuterMostParentsRequireReferences
‪DependencyResolver setOuterMostParentsRequireReferences($outerMostParentsRequireReferences)
Definition: DependencyResolver.php:106
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getNestedElements
‪array getNestedElements(ElementEntity $outerMostParent)
Definition: DependencyResolver.php:170
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\processOuterMostParent
‪processOuterMostParent(ElementEntity $element)
Definition: DependencyResolver.php:150