[TASK] Remove typo3RelPath from TYPO3_LOADED_EXT
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Compatibility / LoadedExtensionArrayElement.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 use TYPO3\CMS\Core\Package\PackageInterface;
18
19 /**
20 * Class to simulate the "old" extension information array element
21 *
22 * @internal
23 */
24 class LoadedExtensionArrayElement implements \IteratorAggregate, \ArrayAccess, \Serializable, \Countable
25 {
26 /**
27 * @var PackageInterface Instance of package manager
28 */
29 protected $package;
30
31 /**
32 * @var array List of relevant extension files
33 */
34 protected $extensionFilesToCheckFor = array(
35 'ext_localconf.php',
36 'ext_tables.php',
37 'ext_tables.sql',
38 'ext_tables_static+adt.sql',
39 'ext_typoscript_constants.txt',
40 'ext_typoscript_setup.txt'
41 );
42
43 /**
44 * @var array Final extension information
45 */
46 protected $extensionInformation = array();
47
48 /**
49 * Constructor builds compatibility API
50 *
51 * @param PackageInterface $package
52 */
53 public function __construct(PackageInterface $package)
54 {
55 $this->package = $package;
56 $this->initializeBasicExtensionInformation();
57 $this->initializeExtensionFiles();
58 $this->initializeExtensionIcon();
59 }
60
61 /**
62 * Create main information
63 *
64 * @return void
65 */
66 protected function initializeBasicExtensionInformation()
67 {
68 $pathSite = PATH_site;
69 $pathSiteLength = strlen($pathSite);
70 $absolutePackagePath = $this->package->getPackagePath();
71 if (substr($absolutePackagePath, 0, $pathSiteLength) === $pathSite) {
72 $relativePackagePathToPathSite = substr($absolutePackagePath, $pathSiteLength);
73 $relativePackagePathToPathSiteSegments = explode('/', $relativePackagePathToPathSite);
74 $packageType = null;
75 // Determine if extension is installed locally, globally or system (in this order)
76 switch (implode('/', array_slice($relativePackagePathToPathSiteSegments, 0, 2))) {
77 case 'typo3conf/ext':
78 $packageType = 'L';
79 break;
80 case TYPO3_mainDir . 'ext':
81 $packageType = 'G';
82 break;
83 case TYPO3_mainDir . 'sysext':
84 $packageType = 'S';
85 break;
86 case 'typo3temp/var/tests/test_ext':
87 $packageType = 'T';
88 break;
89 }
90 if ($packageType !== null && $relativePackagePathToPathSite !== null) {
91 $this->extensionInformation['type'] = $packageType;
92 $this->extensionInformation['siteRelPath'] = $relativePackagePathToPathSite;
93 }
94 }
95 }
96
97 /**
98 * Initialize extension icon
99 *
100 * @return void
101 */
102 protected function initializeExtensionIcon()
103 {
104 $this->extensionInformation['ext_icon'] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionIcon($this->package->getPackagePath());
105 }
106
107 /**
108 * Register found files in extension array if extension was found
109 *
110 * @param void
111 */
112 protected function initializeExtensionFiles()
113 {
114 foreach ($this->extensionFilesToCheckFor as $fileName) {
115 $absolutePathToFile = $this->package->getPackagePath() . $fileName;
116 if (@file_exists($absolutePathToFile)) {
117 $this->extensionInformation[$fileName] = $absolutePathToFile;
118 }
119 }
120 }
121
122 /**
123 * Retrieve an external iterator
124 *
125 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
126 * @return \Traversable An instance of an object implementing Iterator or Traversable
127 */
128 public function getIterator()
129 {
130 return new \ArrayIterator($this->extensionInformation);
131 }
132
133 /**
134 * Whether an offset exists
135 *
136 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
137 * @param mixed $offset An offset to check for.
138 * @return bool TRUE on success or FALSE on failure.
139 */
140 public function offsetExists($offset)
141 {
142 return isset($this->extensionInformation[$offset]);
143 }
144
145 /**
146 * Offset to retrieve
147 *
148 * @link http://php.net/manual/en/arrayaccess.offsetget.php
149 * @param mixed $offset The offset to retrieve.
150 * @return mixed Can return all value types.
151 */
152 public function offsetGet($offset)
153 {
154 return $this->extensionInformation[$offset];
155 }
156
157 /**
158 * Offset to set
159 *
160 * @link http://php.net/manual/en/arrayaccess.offsetset.php
161 * @param mixed $offset The offset to assign the value to.
162 * @param mixed $value The value to set.
163 * @return void
164 * @throws \InvalidArgumentException
165 */
166 public function offsetSet($offset, $value)
167 {
168 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915115);
169 }
170
171 /**
172 * Offset to unset
173 *
174 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
175 * @param mixed $offset The offset to unset.
176 * @return void
177 * @throws \InvalidArgumentException
178 */
179 public function offsetUnset($offset)
180 {
181 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915206);
182 }
183
184 /**
185 * String representation of object
186 *
187 * @link http://php.net/manual/en/serializable.serialize.php
188 * @return string the string representation of the object or null
189 */
190 public function serialize()
191 {
192 return serialize($this->extensionInformation);
193 }
194
195 /**
196 * Constructs the object
197 *
198 * @link http://php.net/manual/en/serializable.unserialize.php
199 * @param string $serialized The string representation of the object.
200 * @return mixed the original value unserialized.
201 */
202 public function unserialize($serialized)
203 {
204 $this->extensionInformation = unserialize($serialized);
205 }
206
207 /**
208 * Count elements of an object
209 *
210 * @link http://php.net/manual/en/countable.count.php
211 * @return int The custom count as an integer. The return value is cast to an integer.
212 */
213 public function count()
214 {
215 return count($this->extensionInformation);
216 }
217
218 /**
219 * @return array
220 */
221 public function toArray()
222 {
223 return iterator_to_array($this);
224 }
225 }