TYPO3 CMS  TYPO3_7-6
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 
154  protected function processOuterMostParent(\TYPO3\CMS\Version\Dependency\ElementEntity $element)
155  {
156  if ($this->outerMostParentsRequireReferences === false || $element->hasReferences()) {
157  $outerMostParent = $element->getOuterMostParent();
158  if ($outerMostParent !== false) {
159  $outerMostParentName = $outerMostParent->__toString();
160  if (!isset($this->outerMostParents[$outerMostParentName])) {
161  $this->outerMostParents[$outerMostParentName] = $outerMostParent;
162  }
163  }
164  }
165  }
166 
174  public function getNestedElements(\TYPO3\CMS\Version\Dependency\ElementEntity $outerMostParent)
175  {
176  $outerMostParentName = $outerMostParent->__toString();
177  if (!isset($this->outerMostParents[$outerMostParentName])) {
178  throw new \RuntimeException('Element "' . $outerMostParentName . '" was not detected as outermost parent.', 1289318609);
179  }
180  $nestedStructure = array_merge([$outerMostParentName => $outerMostParent], $outerMostParent->getNestedChildren());
181  return $nestedStructure;
182  }
183 
189  public function getElements()
190  {
191  return $this->elements;
192  }
193 
199  public function getFactory()
200  {
201  if (!isset($this->factory)) {
202  $this->factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Version\Dependency\DependencyEntityFactory::class);
203  }
204  return $this->factory;
205  }
206 }
processOuterMostParent(\TYPO3\CMS\Version\Dependency\ElementEntity $element)
setEventCallback($eventName, \TYPO3\CMS\Version\Dependency\EventCallback $callback)
setOuterMostParentsRequireReferences($outerMostParentsRequireReferences)
getNestedElements(\TYPO3\CMS\Version\Dependency\ElementEntity $outerMostParent)