08fcc62d8887d95eddc25a15bd9d402a9f6ec575
[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 $relativePackagePathToPathTypo3 = null;
75 $packageType = null;
76 // Determine if extension is installed locally, globally or system (in this order)
77 switch (implode('/', array_slice($relativePackagePathToPathSiteSegments, 0, 2))) {
78 case 'typo3conf/Packages':
79 $packageType = 'C';
80 $relativePackagePathToPathTypo3 = '../typo3conf/Packages/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
81 break;
82 case 'typo3conf/ext':
83 $packageType = 'L';
84 $relativePackagePathToPathTypo3 = '../typo3conf/ext/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
85 break;
86 case TYPO3_mainDir . 'ext':
87 $packageType = 'G';
88 $relativePackagePathToPathTypo3 = 'ext/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
89 break;
90 case TYPO3_mainDir . 'sysext':
91 $packageType = 'S';
92 $relativePackagePathToPathTypo3 = 'sysext/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
93 break;
94 case 'typo3temp/var/tests/test_ext':
95 $packageType = 'T';
96 $relativePackagePathToPathTypo3 = '../typo3temp/var/tests/test_ext/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
97 break;
98 }
99 if ($packageType !== null && $relativePackagePathToPathSite !== null && $relativePackagePathToPathTypo3 !== null) {
100 $this->extensionInformation['type'] = $packageType;
101 $this->extensionInformation['siteRelPath'] = $relativePackagePathToPathSite;
102 $this->extensionInformation['typo3RelPath'] = $relativePackagePathToPathTypo3;
103 }
104 }
105 }
106
107 /**
108 * Initialize extension icon
109 *
110 * @return void
111 */
112 protected function initializeExtensionIcon()
113 {
114 $this->extensionInformation['ext_icon'] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionIcon($this->package->getPackagePath());
115 }
116
117 /**
118 * Register found files in extension array if extension was found
119 *
120 * @param void
121 */
122 protected function initializeExtensionFiles()
123 {
124 foreach ($this->extensionFilesToCheckFor as $fileName) {
125 $absolutePathToFile = $this->package->getPackagePath() . $fileName;
126 if (@file_exists($absolutePathToFile)) {
127 $this->extensionInformation[$fileName] = $absolutePathToFile;
128 }
129 }
130 }
131
132 /**
133 * Retrieve an external iterator
134 *
135 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
136 * @return \Traversable An instance of an object implementing Iterator or Traversable
137 */
138 public function getIterator()
139 {
140 return new \ArrayIterator($this->extensionInformation);
141 }
142
143 /**
144 * Whether an offset exists
145 *
146 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
147 * @param mixed $offset An offset to check for.
148 * @return bool TRUE on success or FALSE on failure.
149 */
150 public function offsetExists($offset)
151 {
152 return isset($this->extensionInformation[$offset]);
153 }
154
155 /**
156 * Offset to retrieve
157 *
158 * @link http://php.net/manual/en/arrayaccess.offsetget.php
159 * @param mixed $offset The offset to retrieve.
160 * @return mixed Can return all value types.
161 */
162 public function offsetGet($offset)
163 {
164 return $this->extensionInformation[$offset];
165 }
166
167 /**
168 * Offset to set
169 *
170 * @link http://php.net/manual/en/arrayaccess.offsetset.php
171 * @param mixed $offset The offset to assign the value to.
172 * @param mixed $value The value to set.
173 * @return void
174 * @throws \InvalidArgumentException
175 */
176 public function offsetSet($offset, $value)
177 {
178 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915115);
179 }
180
181 /**
182 * Offset to unset
183 *
184 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
185 * @param mixed $offset The offset to unset.
186 * @return void
187 * @throws \InvalidArgumentException
188 */
189 public function offsetUnset($offset)
190 {
191 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915206);
192 }
193
194 /**
195 * String representation of object
196 *
197 * @link http://php.net/manual/en/serializable.serialize.php
198 * @return string the string representation of the object or null
199 */
200 public function serialize()
201 {
202 return serialize($this->extensionInformation);
203 }
204
205 /**
206 * Constructs the object
207 *
208 * @link http://php.net/manual/en/serializable.unserialize.php
209 * @param string $serialized The string representation of the object.
210 * @return mixed the original value unserialized.
211 */
212 public function unserialize($serialized)
213 {
214 $this->extensionInformation = unserialize($serialized);
215 }
216
217 /**
218 * Count elements of an object
219 *
220 * @link http://php.net/manual/en/countable.count.php
221 * @return int The custom count as an integer. The return value is cast to an integer.
222 */
223 public function count()
224 {
225 return count($this->extensionInformation);
226 }
227
228 /**
229 * @return array
230 */
231 public function toArray()
232 {
233 return iterator_to_array($this);
234 }
235 }