[TASK] Cleanup EXT:version
[Packages/TYPO3.CMS.git] / typo3 / sysext / version / Classes / Dependency / DependencyResolver.php
1 <?php
2 namespace TYPO3\CMS\Version\Dependency;
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
17 /**
18 * Object to handle and determine dependent references of elements.
19 */
20 class DependencyResolver {
21
22 /**
23 * @var int
24 */
25 protected $workspace = 0;
26
27 /**
28 * @var \TYPO3\CMS\Version\Dependency\DependencyEntityFactory
29 */
30 protected $factory;
31
32 /**
33 * @var array
34 */
35 protected $elements = array();
36
37 /**
38 * @var array
39 */
40 protected $eventCallbacks = array();
41
42 /**
43 * @var boolean
44 */
45 protected $outerMostParentsRequireReferences = FALSE;
46
47 /**
48 * @var array
49 */
50 protected $outerMostParents;
51
52 /**
53 * Sets the current workspace.
54 *
55 * @param int $workspace
56 */
57 public function setWorkspace($workspace) {
58 $this->workspace = (int) $workspace;
59 }
60
61 /**
62 * Gets the current workspace.
63 *
64 * @return int
65 */
66 public function getWorkspace() {
67 return $this->workspace;
68 }
69
70 /**
71 * Sets a callback for a particular event.
72 *
73 * @param string $eventName
74 * @param \TYPO3\CMS\Version\Dependency\EventCallback $callback
75 * @return \TYPO3\CMS\Version\Dependency\DependencyResolver
76 */
77 public function setEventCallback($eventName, \TYPO3\CMS\Version\Dependency\EventCallback $callback) {
78 $this->eventCallbacks[$eventName] = $callback;
79 return $this;
80 }
81
82 /**
83 * Executes a registered callback (if any) for a particular event.
84 *
85 * @param string $eventName
86 * @param object $caller
87 * @param array $callerArguments
88 * @return mixed
89 */
90 public function executeEventCallback($eventName, $caller, array $callerArguments = array()) {
91 if (isset($this->eventCallbacks[$eventName])) {
92 /** @var $callback \TYPO3\CMS\Version\Dependency\EventCallback */
93 $callback = $this->eventCallbacks[$eventName];
94 return $callback->execute($callerArguments, $caller, $eventName);
95 }
96 return NULL;
97 }
98
99 /**
100 * Sets the condition that outermost parents required at least one child or parent reference.
101 *
102 * @param boolean $outerMostParentsRequireReferences
103 * @return \TYPO3\CMS\Version\Dependency\DependencyResolver
104 */
105 public function setOuterMostParentsRequireReferences($outerMostParentsRequireReferences) {
106 $this->outerMostParentsRequireReferences = (bool) $outerMostParentsRequireReferences;
107 return $this;
108 }
109
110 /**
111 * Adds an element to be checked for dependent references.
112 *
113 * @param string $table
114 * @param integer $id
115 * @param array $data
116 * @return \TYPO3\CMS\Version\Dependency\ElementEntity
117 */
118 public function addElement($table, $id, array $data = array()) {
119 $element = $this->getFactory()->getElement($table, $id, $data, $this);
120 $elementName = $element->__toString();
121 $this->elements[$elementName] = $element;
122 return $element;
123 }
124
125 /**
126 * Gets the outermost parents that define complete dependent structure each.
127 *
128 * @return array|\TYPO3\CMS\Version\Dependency\ElementEntity[]
129 */
130 public function getOuterMostParents() {
131 if (!isset($this->outerMostParents)) {
132 $this->outerMostParents = array();
133 /** @var $element \TYPO3\CMS\Version\Dependency\ElementEntity */
134 foreach ($this->elements as $element) {
135 $this->processOuterMostParent($element);
136 }
137 }
138 return $this->outerMostParents;
139 }
140
141 /**
142 * Processes and registers the outermost parents accordant to the registered elements.
143 *
144 * @param \TYPO3\CMS\Version\Dependency\ElementEntity $element
145 * @return void
146 */
147 protected function processOuterMostParent(\TYPO3\CMS\Version\Dependency\ElementEntity $element) {
148 if ($this->outerMostParentsRequireReferences === FALSE || $element->hasReferences()) {
149 $outerMostParent = $element->getOuterMostParent();
150 if ($outerMostParent !== FALSE) {
151 $outerMostParentName = $outerMostParent->__toString();
152 if (!isset($this->outerMostParents[$outerMostParentName])) {
153 $this->outerMostParents[$outerMostParentName] = $outerMostParent;
154 }
155 }
156 }
157 }
158
159 /**
160 * Gets all nested elements (including the parent) of a particular outermost parent element.
161 *
162 * @throws \RuntimeException
163 * @param \TYPO3\CMS\Version\Dependency\ElementEntity $outerMostParent
164 * @return array
165 */
166 public function getNestedElements(\TYPO3\CMS\Version\Dependency\ElementEntity $outerMostParent) {
167 $outerMostParentName = $outerMostParent->__toString();
168 if (!isset($this->outerMostParents[$outerMostParentName])) {
169 throw new \RuntimeException('Element "' . $outerMostParentName . '" was not detected as outermost parent.', 1289318609);
170 }
171 $nestedStructure = array_merge(array($outerMostParentName => $outerMostParent), $outerMostParent->getNestedChildren());
172 return $nestedStructure;
173 }
174
175 /**
176 * Gets the registered elements.
177 *
178 * @return array
179 */
180 public function getElements() {
181 return $this->elements;
182 }
183
184 /**
185 * Gets an instance of the factory to keep track of element or reference entities.
186 *
187 * @return \TYPO3\CMS\Version\Dependency\DependencyEntityFactory
188 */
189 public function getFactory() {
190 if (!isset($this->factory)) {
191 $this->factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Version\\Dependency\\DependencyEntityFactory');
192 }
193 return $this->factory;
194 }
195
196 }