[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Compatibility / LoadedExtensionsArray.php
1 <?php
2 namespace TYPO3\CMS\Core\Compatibility;
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 * Class to simulate the "old" extension information array
19 *
20 * @internal
21 */
22 class LoadedExtensionsArray implements \Iterator, \ArrayAccess, \Serializable, \Countable
23 {
24 /**
25 * @var \TYPO3\CMS\Core\Package\PackageManager Instance of package manager
26 */
27 protected $packageManager;
28
29 /**
30 * @var array Loaded element cache
31 */
32 protected $loadedExtensionArrayElementCache = array();
33
34 /**
35 * @var string Pointer to current position
36 */
37 protected $iteratorPosition;
38
39 /**
40 * @param \TYPO3\CMS\Core\Package\PackageManager
41 */
42 public function __construct(\TYPO3\CMS\Core\Package\PackageManager $packageManager)
43 {
44 $this->packageManager = $packageManager;
45 }
46
47 /**
48 * Whether an offset exists
49 *
50 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
51 * @param mixed $offset An offset to check for.
52 * @return bool TRUE on success or FALSE on failure.
53 */
54 public function offsetExists($offset)
55 {
56 return $this->packageManager->isPackageActive($offset);
57 }
58
59 /**
60 * Offset to retrieve
61 *
62 * @link http://php.net/manual/en/arrayaccess.offsetget.php
63 * @param mixed $offset The offset to retrieve.
64 * @return mixed Can return all value types.
65 */
66 public function offsetGet($offset)
67 {
68 // Pass it through the package manager, as it resolves package aliases
69 $package = $this->packageManager->getPackage($offset);
70 $packageKey = $package->getPackageKey();
71 if (!isset($this->loadedExtensionArrayElementCache[$packageKey])) {
72 $this->loadedExtensionArrayElementCache[$packageKey] = new LoadedExtensionArrayElement($package);
73 }
74 return $this->loadedExtensionArrayElementCache[$packageKey];
75 }
76
77 /**
78 * Offset to set
79 *
80 * @link http://php.net/manual/en/arrayaccess.offsetset.php
81 * @param mixed $offset The offset to assign the value to.
82 * @param mixed $value The value to set.
83 * @throws \InvalidArgumentException
84 * @return void
85 */
86 public function offsetSet($offset, $value)
87 {
88 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915596);
89 }
90
91 /**
92 * Offset to unset
93 *
94 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
95 * @param mixed $offset The offset to unset.
96 * @throws \InvalidArgumentException
97 * @return void
98 */
99 public function offsetUnset($offset)
100 {
101 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915610);
102 }
103
104 /**
105 * String representation of object
106 *
107 * @link http://php.net/manual/en/serializable.serialize.php
108 * @return string the string representation of the object or null
109 */
110 public function serialize()
111 {
112 return serialize($this->loadedExtensionArrayElementCache);
113 }
114
115 /**
116 * Constructs the object
117 *
118 * @link http://php.net/manual/en/serializable.unserialize.php
119 * @param string $serialized The string representation of the object.
120 * @return mixed the original value unserialized.
121 */
122 public function unserialize($serialized)
123 {
124 $this->loadedExtensionArrayElementCache = unserialize($serialized);
125 }
126
127 /**
128 * Count elements of an object
129 *
130 * @link http://php.net/manual/en/countable.count.php
131 * @return int The custom count as an integer.
132 */
133 public function count()
134 {
135 return count($this->packageManager->getActivePackages());
136 }
137
138 /**
139 * Return the current element
140 *
141 * @link http://php.net/manual/en/iterator.current.php
142 * @return mixed Can return any type.
143 */
144 public function current()
145 {
146 return $this->offsetGet($this->iteratorPosition);
147 }
148
149 /**
150 * Move forward to next element
151 *
152 * @link http://php.net/manual/en/iterator.next.php
153 * @return void Any returned value is ignored.
154 */
155 public function next()
156 {
157 $packageKeys = array_keys($this->packageManager->getActivePackages());
158 $position = array_search($this->iteratorPosition, $packageKeys);
159 if (isset($packageKeys[$position + 1])) {
160 $this->iteratorPosition = $packageKeys[$position + 1];
161 } else {
162 $this->iteratorPosition = null;
163 }
164 }
165
166 /**
167 * Return the key of the current element
168 *
169 * @link http://php.net/manual/en/iterator.key.php
170 * @return mixed scalar on success, or null on failure.
171 */
172 public function key()
173 {
174 return $this->iteratorPosition;
175 }
176
177 /**
178 * Checks if current position is valid
179 *
180 * @link http://php.net/manual/en/iterator.valid.php
181 * @return bool The return value will be casted to boolean and then evaluated. Returns true on success or false on failure.
182 */
183 public function valid()
184 {
185 return $this->offsetExists($this->iteratorPosition);
186 }
187
188 /**
189 * Rewind the Iterator to the first element
190 *
191 * @link http://php.net/manual/en/iterator.rewind.php
192 * @return void Any returned value is ignored.
193 */
194 public function rewind()
195 {
196 $keys = array_keys($this->packageManager->getActivePackages());
197 $this->iteratorPosition = array_shift($keys);
198 }
199
200 /**
201 * Reset
202 *
203 * @return void
204 */
205 public function reset()
206 {
207 $this->loadedExtensionArrayElementCache = array();
208 $this->rewind();
209 }
210
211 /**
212 * Whether package manager is set in class
213 *
214 * @return bool TRUE if package manager is set
215 */
216 public function hasPackageManager()
217 {
218 return $this->packageManager !== null;
219 }
220
221 /**
222 * @return array
223 */
224 public function toArray()
225 {
226 return array_map(
227 function ($loadedExtElement) {
228 return $loadedExtElement->toArray();
229 },
230 iterator_to_array($this)
231 );
232 }
233 }