[FEATURE] Introduce .typoscript file extension
[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 = [
35 'ext_localconf.php',
36 'ext_tables.php',
37 'ext_tables.sql',
38 'ext_tables_static+adt.sql',
39 'ext_typoscript_constants.typoscript',
40 'ext_typoscript_setup.typoscript',
41 'ext_typoscript_constants.txt',
42 'ext_typoscript_setup.txt'
43 ];
44
45 /**
46 * @var array Final extension information
47 */
48 protected $extensionInformation = [];
49
50 /**
51 * Constructor builds compatibility API
52 *
53 * @param PackageInterface $package
54 */
55 public function __construct(PackageInterface $package)
56 {
57 $this->package = $package;
58 $this->initializeBasicExtensionInformation();
59 $this->initializeExtensionFiles();
60 $this->initializeExtensionIcon();
61 }
62
63 /**
64 * Create main information
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) {
91 $this->extensionInformation['type'] = $packageType;
92 }
93 }
94 }
95
96 /**
97 * Initialize extension icon
98 */
99 protected function initializeExtensionIcon()
100 {
101 $this->extensionInformation['ext_icon'] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionIcon($this->package->getPackagePath());
102 }
103
104 /**
105 * Register found files in extension array if extension was found
106 */
107 protected function initializeExtensionFiles()
108 {
109 foreach ($this->extensionFilesToCheckFor as $fileName) {
110 $absolutePathToFile = $this->package->getPackagePath() . $fileName;
111 if (@file_exists($absolutePathToFile)) {
112 $this->extensionInformation[$fileName] = $absolutePathToFile;
113 }
114 }
115 }
116
117 /**
118 * Retrieve an external iterator
119 *
120 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
121 * @return \Traversable An instance of an object implementing Iterator or Traversable
122 */
123 public function getIterator()
124 {
125 return new \ArrayIterator($this->extensionInformation);
126 }
127
128 /**
129 * Whether an offset exists
130 *
131 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
132 * @param mixed $offset An offset to check for.
133 * @return bool TRUE on success or FALSE on failure.
134 */
135 public function offsetExists($offset)
136 {
137 return isset($this->extensionInformation[$offset]);
138 }
139
140 /**
141 * Offset to retrieve
142 *
143 * @link http://php.net/manual/en/arrayaccess.offsetget.php
144 * @param mixed $offset The offset to retrieve.
145 * @return mixed Can return all value types.
146 */
147 public function offsetGet($offset)
148 {
149 return $this->extensionInformation[$offset];
150 }
151
152 /**
153 * Offset to set
154 *
155 * @link http://php.net/manual/en/arrayaccess.offsetset.php
156 * @param mixed $offset The offset to assign the value to.
157 * @param mixed $value The value to set.
158 * @throws \InvalidArgumentException
159 */
160 public function offsetSet($offset, $value)
161 {
162 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915115);
163 }
164
165 /**
166 * Offset to unset
167 *
168 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
169 * @param mixed $offset The offset to unset.
170 * @throws \InvalidArgumentException
171 */
172 public function offsetUnset($offset)
173 {
174 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915206);
175 }
176
177 /**
178 * String representation of object
179 *
180 * @link http://php.net/manual/en/serializable.serialize.php
181 * @return string the string representation of the object or null
182 */
183 public function serialize()
184 {
185 return serialize($this->extensionInformation);
186 }
187
188 /**
189 * Constructs the object
190 *
191 * @link http://php.net/manual/en/serializable.unserialize.php
192 * @param string $serialized The string representation of the object.
193 * @return mixed the original value unserialized.
194 */
195 public function unserialize($serialized)
196 {
197 $this->extensionInformation = unserialize($serialized);
198 }
199
200 /**
201 * Count elements of an object
202 *
203 * @link http://php.net/manual/en/countable.count.php
204 * @return int The custom count as an integer. The return value is cast to an integer.
205 */
206 public function count()
207 {
208 return count($this->extensionInformation);
209 }
210
211 /**
212 * @return array
213 */
214 public function toArray()
215 {
216 return iterator_to_array($this);
217 }
218 }