TYPO3 CMS  TYPO3_8-7
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 
21 {
25  protected $workspace = 0;
26 
30  protected $factory;
31 
35  protected $elements = [];
36 
40  protected $eventCallbacks = [];
41 
46 
50  protected $outerMostParents;
51 
57  public function setWorkspace($workspace)
58  {
59  $this->workspace = (int)$workspace;
60  }
61 
67  public function getWorkspace()
68  {
69  return $this->workspace;
70  }
71 
79  public function setEventCallback($eventName, \TYPO3\CMS\Version\Dependency\EventCallback $callback)
80  {
81  $this->eventCallbacks[$eventName] = $callback;
82  return $this;
83  }
84 
93  public function executeEventCallback($eventName, $caller, array $callerArguments = [])
94  {
95  if (isset($this->eventCallbacks[$eventName])) {
97  $callback = $this->eventCallbacks[$eventName];
98  return $callback->execute($callerArguments, $caller, $eventName);
99  }
100  return null;
101  }
102 
110  {
111  $this->outerMostParentsRequireReferences = (bool)$outerMostParentsRequireReferences;
112  return $this;
113  }
114 
123  public function addElement($table, $id, array $data = [])
124  {
125  $element = $this->getFactory()->getElement($table, $id, $data, $this);
126  $elementName = $element->__toString();
127  $this->elements[$elementName] = $element;
128  return $element;
129  }
130 
136  public function getOuterMostParents()
137  {
138  if (!isset($this->outerMostParents)) {
139  $this->outerMostParents = [];
141  foreach ($this->elements as $element) {
142  $this->processOuterMostParent($element);
143  }
144  }
146  }
147 
153  protected function processOuterMostParent(\TYPO3\CMS\Version\Dependency\ElementEntity $element)
154  {
155  if ($this->outerMostParentsRequireReferences === false || $element->hasReferences()) {
156  $outerMostParent = $element->getOuterMostParent();
157  if ($outerMostParent !== false) {
158  $outerMostParentName = $outerMostParent->__toString();
159  if (!isset($this->outerMostParents[$outerMostParentName])) {
160  $this->outerMostParents[$outerMostParentName] = $outerMostParent;
161  }
162  }
163  }
164  }
165 
173  public function getNestedElements(\TYPO3\CMS\Version\Dependency\ElementEntity $outerMostParent)
174  {
175  $outerMostParentName = $outerMostParent->__toString();
176  if (!isset($this->outerMostParents[$outerMostParentName])) {
177  throw new \RuntimeException('Element "' . $outerMostParentName . '" was not detected as outermost parent.', 1289318609);
178  }
179  $nestedStructure = array_merge([$outerMostParentName => $outerMostParent], $outerMostParent->getNestedChildren());
180  return $nestedStructure;
181  }
182 
188  public function getElements()
189  {
190  return $this->elements;
191  }
192 
198  public function getFactory()
199  {
200  if (!isset($this->factory)) {
201  $this->factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Version\Dependency\DependencyEntityFactory::class);
202  }
203  return $this->factory;
204  }
205 }
processOuterMostParent(\TYPO3\CMS\Version\Dependency\ElementEntity $element)
setEventCallback($eventName, \TYPO3\CMS\Version\Dependency\EventCallback $callback)
setOuterMostParentsRequireReferences($outerMostParentsRequireReferences)
static makeInstance($className,... $constructorArguments)
getNestedElements(\TYPO3\CMS\Version\Dependency\ElementEntity $outerMostParent)