|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2010-2011 Oliver Hader <oliver@typo3.org> 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00027 00028 /** 00029 * Object to handle and determine dependent references of elements. 00030 */ 00031 class t3lib_utility_Dependency { 00032 /** 00033 * @var t3lib_utility_Dependency_Factory 00034 */ 00035 protected $factory; 00036 00037 /** 00038 * @var array 00039 */ 00040 protected $elements = array(); 00041 00042 /** 00043 * @var array 00044 */ 00045 protected $eventCallbacks = array(); 00046 00047 /** 00048 * @var boolean 00049 */ 00050 protected $outerMostParentsRequireReferences = FALSE; 00051 00052 /** 00053 * @var array 00054 */ 00055 protected $outerMostParents; 00056 00057 /** 00058 * Sets a callback for a particular event. 00059 * 00060 * @param string $eventName 00061 * @param t3lib_utility_Dependency_Callback $callback 00062 * @return t3lib_utility_Dependency 00063 */ 00064 public function setEventCallback($eventName, t3lib_utility_Dependency_Callback $callback) { 00065 $this->eventCallbacks[$eventName] = $callback; 00066 return $this; 00067 } 00068 00069 /** 00070 * Executes a registered callback (if any) for a particular event. 00071 * 00072 * @param string $eventName 00073 * @param object $caller 00074 * @param array $callerArguments 00075 * @return mixed 00076 */ 00077 public function executeEventCallback($eventName, $caller, array $callerArguments = array()) { 00078 if (isset($this->eventCallbacks[$eventName])) { 00079 /** @var $callback t3lib_utility_Dependency_Callback */ 00080 $callback = $this->eventCallbacks[$eventName]; 00081 return $callback->execute($callerArguments, $caller, $eventName); 00082 } 00083 } 00084 00085 /** 00086 * Sets the condition that outermost parents required at least one child or parent reference. 00087 * 00088 * @param boolean $outerMostParentsRequireReferences 00089 * @return t3lib_utility_Dependency 00090 */ 00091 public function setOuterMostParentsRequireReferences($outerMostParentsRequireReferences) { 00092 $this->outerMostParentsRequireReferences = (bool) $outerMostParentsRequireReferences; 00093 return $this; 00094 } 00095 00096 /** 00097 * Adds an element to be checked for dependent references. 00098 * 00099 * @param string $table 00100 * @param integer $id 00101 * @param array $data 00102 * @return t3lib_utility_Dependency_Element 00103 */ 00104 public function addElement($table, $id, array $data = array()) { 00105 $element = $this->getFactory()->getElement($table, $id, $data, $this); 00106 $elementName = $element->__toString(); 00107 $this->elements[$elementName] = $element; 00108 return $element; 00109 } 00110 00111 /** 00112 * Gets the outermost parents that define complete dependent structure each. 00113 * 00114 * @return array 00115 */ 00116 public function getOuterMostParents() { 00117 if (!isset($this->outerMostParents)) { 00118 $this->outerMostParents = array(); 00119 00120 /** @var $element t3lib_utility_Dependency_Element */ 00121 foreach ($this->elements as $element) { 00122 $this->processOuterMostParent($element); 00123 } 00124 } 00125 00126 return $this->outerMostParents; 00127 } 00128 00129 /** 00130 * Processes and registers the outermost parents accordant to the registered elements. 00131 * 00132 * @param t3lib_utility_Dependency_Element $element 00133 * @return void 00134 */ 00135 protected function processOuterMostParent(t3lib_utility_Dependency_Element $element) { 00136 if ($this->outerMostParentsRequireReferences === FALSE || $element->hasReferences()) { 00137 $outerMostParent = $element->getOuterMostParent(); 00138 00139 if ($outerMostParent !== FALSE) { 00140 $outerMostParentName = $outerMostParent->__toString(); 00141 if (!isset($this->outerMostParents[$outerMostParentName])) { 00142 $this->outerMostParents[$outerMostParentName] = $outerMostParent; 00143 } 00144 } 00145 } 00146 } 00147 00148 /** 00149 * Gets all nested elements (including the parent) of a particular outermost parent element. 00150 * 00151 * @throws RuntimeException 00152 * @param t3lib_utility_Dependency_Element $outerMostParent 00153 * @return array 00154 */ 00155 public function getNestedElements(t3lib_utility_Dependency_Element $outerMostParent) { 00156 $outerMostParentName = $outerMostParent->__toString(); 00157 00158 if (!isset($this->outerMostParents[$outerMostParentName])) { 00159 throw new RuntimeException( 00160 'Element "' . $outerMostParentName . '" was detected as outermost parent.', 00161 1289318609 00162 ); 00163 } 00164 00165 $nestedStructure = array_merge( 00166 array($outerMostParentName => $outerMostParent), 00167 $outerMostParent->getNestedChildren() 00168 ); 00169 00170 return $nestedStructure; 00171 } 00172 00173 /** 00174 * Gets the registered elements. 00175 * 00176 * @return array 00177 */ 00178 public function getElements() { 00179 return $this->elements; 00180 } 00181 00182 /** 00183 * Gets an instance of the factory to keep track of element or reference entities. 00184 * 00185 * @return t3lib_utility_Dependency_Factory 00186 */ 00187 public function getFactory() { 00188 if (!isset($this->factory)) { 00189 $this->factory = t3lib_div::makeInstance('t3lib_utility_Dependency_Factory'); 00190 } 00191 return $this->factory; 00192 } 00193 } 00194 ?>
1.8.0