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