Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_utility_directory
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_dependency.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Oliver Hader <oliver@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Object to handle and determine dependent references of elements.
30 */
31 class t3lib_utility_Dependency {
32 /**
33 * @var t3lib_utility_Dependency_Factory
34 */
35 protected $factory;
36
37 /**
38 * @var array
39 */
40 protected $elements = array();
41
42 /**
43 * @var array
44 */
45 protected $eventCallbacks = array();
46
47 /**
48 * @var boolean
49 */
50 protected $outerMostParentsRequireReferences = FALSE;
51
52 /**
53 * @var array
54 */
55 protected $outerMostParents;
56
57 /**
58 * Sets a callback for a particular event.
59 *
60 * @param string $eventName
61 * @param t3lib_utility_Dependency_Callback $callback
62 * @return t3lib_utility_Dependency
63 */
64 public function setEventCallback($eventName, t3lib_utility_Dependency_Callback $callback) {
65 $this->eventCallbacks[$eventName] = $callback;
66 return $this;
67 }
68
69 /**
70 * Executes a registered callback (if any) for a particular event.
71 *
72 * @param string $eventName
73 * @param object $caller
74 * @param array $callerArguments
75 * @return mixed
76 */
77 public function executeEventCallback($eventName, $caller, array $callerArguments = array()) {
78 if (isset($this->eventCallbacks[$eventName])) {
79 /** @var $callback t3lib_utility_Dependency_Callback */
80 $callback = $this->eventCallbacks[$eventName];
81 return $callback->execute($callerArguments, $caller, $eventName);
82 }
83 }
84
85 /**
86 * Sets the condition that outermost parents required at least one child or parent reference.
87 *
88 * @param boolean $outerMostParentsRequireReferences
89 * @return t3lib_utility_Dependency
90 */
91 public function setOuterMostParentsRequireReferences($outerMostParentsRequireReferences) {
92 $this->outerMostParentsRequireReferences = (bool) $outerMostParentsRequireReferences;
93 return $this;
94 }
95
96 /**
97 * Adds an element to be checked for dependent references.
98 *
99 * @param string $table
100 * @param integer $id
101 * @param array $data
102 * @return t3lib_utility_Dependency_Element
103 */
104 public function addElement($table, $id, array $data = array()) {
105 $element = $this->getFactory()->getElement($table, $id, $data, $this);
106 $elementName = $element->__toString();
107 $this->elements[$elementName] = $element;
108 return $element;
109 }
110
111 /**
112 * Gets the outermost parents that define complete dependent structure each.
113 *
114 * @return array
115 */
116 public function getOuterMostParents() {
117 if (!isset($this->outerMostParents)) {
118 $this->outerMostParents = array();
119
120 /** @var $element t3lib_utility_Dependency_Element */
121 foreach ($this->elements as $element) {
122 $this->processOuterMostParent($element);
123 }
124 }
125
126 return $this->outerMostParents;
127 }
128
129 /**
130 * Processes and registers the outermost parents accordant to the registered elements.
131 *
132 * @param t3lib_utility_Dependency_Element $element
133 * @return void
134 */
135 protected function processOuterMostParent(t3lib_utility_Dependency_Element $element) {
136 if ($this->outerMostParentsRequireReferences === FALSE || $element->hasReferences()) {
137 $outerMostParent = $element->getOuterMostParent();
138
139 if ($outerMostParent !== FALSE) {
140 $outerMostParentName = $outerMostParent->__toString();
141 if (!isset($this->outerMostParents[$outerMostParentName])) {
142 $this->outerMostParents[$outerMostParentName] = $outerMostParent;
143 }
144 }
145 }
146 }
147
148 /**
149 * Gets all nested elements (including the parent) of a particular outermost parent element.
150 *
151 * @throws RuntimeException
152 * @param t3lib_utility_Dependency_Element $outerMostParent
153 * @return array
154 */
155 public function getNestedElements(t3lib_utility_Dependency_Element $outerMostParent) {
156 $outerMostParentName = $outerMostParent->__toString();
157
158 if (!isset($this->outerMostParents[$outerMostParentName])) {
159 throw new RuntimeException(
160 'Element "' . $outerMostParentName . '" was detected as outermost parent.',
161 1289318609
162 );
163 }
164
165 $nestedStructure = array_merge(
166 array($outerMostParentName => $outerMostParent),
167 $outerMostParent->getNestedChildren()
168 );
169
170 return $nestedStructure;
171 }
172
173 /**
174 * Gets the registered elements.
175 *
176 * @return array
177 */
178 public function getElements() {
179 return $this->elements;
180 }
181
182 /**
183 * Gets an instance of the factory to keep track of element or reference entities.
184 *
185 * @return t3lib_utility_Dependency_Factory
186 */
187 public function getFactory() {
188 if (!isset($this->factory)) {
189 $this->factory = t3lib_div::makeInstance('t3lib_utility_Dependency_Factory');
190 }
191 return $this->factory;
192 }
193 }
194 ?>