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