[BUGFIX] Remove unused if in PasswordHashing
[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 = [];
33
34 /**
35 * @var string Pointer to current position
36 */
37 protected $iteratorPosition;
38
39 /**
40 * @param \TYPO3\CMS\Core\Package\PackageManager $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 */
85 public function offsetSet($offset, $value)
86 {
87 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915596);
88 }
89
90 /**
91 * Offset to unset
92 *
93 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
94 * @param mixed $offset The offset to unset.
95 * @throws \InvalidArgumentException
96 */
97 public function offsetUnset($offset)
98 {
99 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915610);
100 }
101
102 /**
103 * String representation of object
104 *
105 * @link http://php.net/manual/en/serializable.serialize.php
106 * @return string the string representation of the object or null
107 */
108 public function serialize()
109 {
110 return serialize($this->loadedExtensionArrayElementCache);
111 }
112
113 /**
114 * Constructs the object
115 *
116 * @link http://php.net/manual/en/serializable.unserialize.php
117 * @param string $serialized The string representation of the object.
118 * @return mixed the original value unserialized.
119 */
120 public function unserialize($serialized)
121 {
122 $this->loadedExtensionArrayElementCache = unserialize($serialized);
123 }
124
125 /**
126 * Count elements of an object
127 *
128 * @link http://php.net/manual/en/countable.count.php
129 * @return int The custom count as an integer.
130 */
131 public function count()
132 {
133 return count($this->packageManager->getActivePackages());
134 }
135
136 /**
137 * Return the current element
138 *
139 * @link http://php.net/manual/en/iterator.current.php
140 * @return mixed Can return any type.
141 */
142 public function current()
143 {
144 return $this->offsetGet($this->iteratorPosition);
145 }
146
147 /**
148 * Move forward to next element
149 *
150 * @link http://php.net/manual/en/iterator.next.php
151 */
152 public function next()
153 {
154 $packageKeys = array_keys($this->packageManager->getActivePackages());
155 $position = array_search($this->iteratorPosition, $packageKeys);
156 if (isset($packageKeys[$position + 1])) {
157 $this->iteratorPosition = $packageKeys[$position + 1];
158 } else {
159 $this->iteratorPosition = null;
160 }
161 }
162
163 /**
164 * Return the key of the current element
165 *
166 * @link http://php.net/manual/en/iterator.key.php
167 * @return mixed scalar on success, or null on failure.
168 */
169 public function key()
170 {
171 return $this->iteratorPosition;
172 }
173
174 /**
175 * Checks if current position is valid
176 *
177 * @link http://php.net/manual/en/iterator.valid.php
178 * @return bool The return value will be casted to boolean and then evaluated. Returns true on success or false on failure.
179 */
180 public function valid()
181 {
182 return $this->offsetExists($this->iteratorPosition);
183 }
184
185 /**
186 * Rewind the Iterator to the first element
187 *
188 * @link http://php.net/manual/en/iterator.rewind.php
189 */
190 public function rewind()
191 {
192 $keys = array_keys($this->packageManager->getActivePackages());
193 $this->iteratorPosition = array_shift($keys);
194 }
195
196 /**
197 * Reset
198 */
199 public function reset()
200 {
201 $this->loadedExtensionArrayElementCache = [];
202 $this->rewind();
203 }
204
205 /**
206 * Whether package manager is set in class
207 *
208 * @return bool TRUE if package manager is set
209 */
210 public function hasPackageManager()
211 {
212 return $this->packageManager !== null;
213 }
214
215 /**
216 * @return array
217 */
218 public function toArray()
219 {
220 return array_map(
221 function ($loadedExtElement) {
222 return $loadedExtElement->toArray();
223 },
224 iterator_to_array($this)
225 );
226 }
227 }