[FEATURE] Integrate preliminary PackageManager API
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Compatibility / LoadedExtensionsArray.php
1 <?php
2 namespace TYPO3\CMS\Core\Compatibility;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2013 Thomas Maroschik <tmaroschik@dfau.de>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Class to simulate the "old" extension information array
31 *
32 * @internal
33 */
34 class LoadedExtensionsArray implements \Iterator, \ArrayAccess, \Serializable, \Countable {
35
36 /**
37 * @var \TYPO3\CMS\Core\Package\PackageManager Instance of package manager
38 */
39 protected $packageManager;
40
41 /**
42 * @var array Loaded element cache
43 */
44 protected $loadedExtensionArrayElementCache = array();
45
46 /**
47 * @var string Pointer to current position
48 */
49 protected $iteratorPosition;
50
51 /**
52 * @param \TYPO3\CMS\Core\Package\PackageManager
53 */
54 public function __construct(\TYPO3\CMS\Core\Package\PackageManager $packageManager) {
55 $this->packageManager = $packageManager;
56 }
57
58 /**
59 * Whether a offset exists
60 *
61 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
62 * @param mixed $offset An offset to check for.
63 * @return boolean TRUE on success or FALSE on failure.
64 */
65 public function offsetExists($offset) {
66 return $this->packageManager->isPackageActive($offset);
67 }
68
69 /**
70 * Offset to retrieve
71 *
72 * @link http://php.net/manual/en/arrayaccess.offsetget.php
73 * @param mixed $offset The offset to retrieve.
74 * @return mixed Can return all value types.
75 */
76 public function offsetGet($offset) {
77 // Pass it through the package manager, as it resolves package aliases
78 $package = $this->packageManager->getPackage($offset);
79 $packageKey = $package->getPackageKey();
80 if (!isset($this->loadedExtensionArrayElementCache[$packageKey])) {
81 $this->loadedExtensionArrayElementCache[$packageKey] = new LoadedExtensionArrayElement($package);
82 }
83 return $this->loadedExtensionArrayElementCache[$packageKey];
84 }
85
86 /**
87 * Offset to set
88 *
89 * @link http://php.net/manual/en/arrayaccess.offsetset.php
90 * @param mixed $offset The offset to assign the value to.
91 * @param mixed $value The value to set.
92 * @throws \InvalidArgumentException
93 * @return void
94 */
95 public function offsetSet($offset, $value) {
96 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915596);
97 }
98
99 /**
100 * Offset to unset
101 *
102 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
103 * @param mixed $offset The offset to unset.
104 * @throws \InvalidArgumentException
105 * @return void
106 */
107 public function offsetUnset($offset) {
108 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915610);
109 }
110
111 /**
112 * String representation of object
113 *
114 * @link http://php.net/manual/en/serializable.serialize.php
115 * @return string the string representation of the object or null
116 */
117 public function serialize() {
118 return serialize($this->loadedExtensionArrayElementCache);
119 }
120
121 /**
122 * Constructs the object
123 *
124 * @link http://php.net/manual/en/serializable.unserialize.php
125 * @param string $serialized The string representation of the object.
126 * @return mixed the original value unserialized.
127 */
128 public function unserialize($serialized) {
129 $this->loadedExtensionArrayElementCache = unserialize($serialized);
130 }
131
132 /**
133 * Count elements of an object
134 *
135 * @link http://php.net/manual/en/countable.count.php
136 * @return integer The custom count as an integer.
137 */
138 public function count() {
139 return count($this->packageManager->getActivePackages());
140 }
141
142
143 /**
144 * Return the current element
145 *
146 * @link http://php.net/manual/en/iterator.current.php
147 * @return mixed Can return any type.
148 */
149 public function current() {
150 return $this->offsetGet($this->iteratorPosition);
151 }
152
153 /**
154 * Move forward to next element
155 *
156 * @link http://php.net/manual/en/iterator.next.php
157 * @return void Any returned value is ignored.
158 */
159 public function next() {
160 $packageKeys = array_keys($this->packageManager->getActivePackages());
161 $position = array_search($this->iteratorPosition, $packageKeys);
162 if (isset($packageKeys[$position + 1])) {
163 $this->iteratorPosition = $packageKeys[$position + 1];
164 } else {
165 $this->iteratorPosition = NULL;
166 }
167 }
168
169 /**
170 * Return the key of the current element
171 *
172 * @link http://php.net/manual/en/iterator.key.php
173 * @return mixed scalar on success, or null on failure.
174 */
175 public function key() {
176 return $this->iteratorPosition;
177 }
178
179 /**
180 * Checks if current position is valid
181 *
182 * @link http://php.net/manual/en/iterator.valid.php
183 * @return boolean The return value will be casted to boolean and then evaluated. Returns true on success or false on failure.
184 */
185 public function valid() {
186 return $this->offsetExists($this->iteratorPosition);
187 }
188
189 /**
190 * Rewind the Iterator to the first element
191 *
192 * @link http://php.net/manual/en/iterator.rewind.php
193 * @return void Any returned value is ignored.
194 */
195 public function rewind() {
196 $this->iteratorPosition = array_shift(array_keys($this->packageManager->getActivePackages()));
197 }
198
199 /**
200 * Reset
201 *
202 * @return void
203 */
204 public function reset() {
205 $this->loadedExtensionArrayElementCache = array();
206 $this->rewind();
207 }
208
209 /**
210 * Whether package manager is set in class
211 *
212 * @return boolean TRUE if package manager is set
213 */
214 public function hasPackageManager() {
215 return $this->packageManager !== NULL;
216 }
217 }