[FEATURE] Integrate preliminary PackageManager API
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Compatibility / LoadedExtensionArrayElement.php
1 <?php
2 namespace TYPO3\CMS\Core\Compatibility;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2013 Thomas Maroschik <tmaroschik@dfau.de>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Class to simulate the "old" extension information array element
31 *
32 * @internal
33 */
34 class LoadedExtensionArrayElement implements \IteratorAggregate, \ArrayAccess, \Serializable, \Countable {
35
36 /**
37 * @var \TYPO3\Flow\Package\PackageInterface Instance of package manager
38 */
39 protected $package;
40
41 /**
42 * @var array List of relevant extension files
43 */
44 protected $extensionFilesToCheckFor = array(
45 'ext_localconf.php',
46 'ext_tables.php',
47 'ext_tables.sql',
48 'ext_tables_static+adt.sql',
49 'ext_typoscript_constants.txt',
50 'ext_typoscript_setup.txt'
51 );
52
53 /**
54 * @var array Final extension information
55 */
56 protected $extensionInformation = array();
57
58 /**
59 * Constructor builds compatibility API
60 *
61 * @param \TYPO3\Flow\Package\PackageInterface $package
62 */
63 public function __construct(\TYPO3\Flow\Package\PackageInterface $package) {
64 $this->package = $package;
65 $this->initializeBasicExtensionInformation();
66 $this->initializeExtensionFiles();
67 $this->initializeExtensionIcon();
68 }
69
70 /**
71 * Create main information
72 *
73 * @return void
74 */
75 protected function initializeBasicExtensionInformation() {
76 $pathSite = PATH_site;
77 $pathSiteLength = strlen($pathSite);
78 $absolutePackagePath = $this->package->getPackagePath();
79 if (substr($absolutePackagePath, 0, $pathSiteLength) === $pathSite) {
80 $relativePackagePathToPathSite = substr($absolutePackagePath, $pathSiteLength);
81 $relativePackagePathToPathSiteSegments = explode('/', $relativePackagePathToPathSite);
82 $relativePackagePathToPathTypo3 = NULL;
83 $packageType = NULL;
84 // Determine if extension is installed locally, globally or system (in this order)
85 switch (implode('/', array_slice($relativePackagePathToPathSiteSegments, 0, 2))) {
86 case 'typo3conf/Packages':
87 $packageType = 'C';
88 $relativePackagePathToPathTypo3 = '../typo3conf/Packages/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
89 break;
90 case 'typo3conf/ext':
91 $packageType = 'L';
92 $relativePackagePathToPathTypo3 = '../typo3conf/ext/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
93 break;
94 case TYPO3_mainDir . 'ext':
95 $packageType = 'G';
96 $relativePackagePathToPathTypo3 = 'ext/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
97 break;
98 case TYPO3_mainDir . 'sysext':
99 $packageType = 'S';
100 $relativePackagePathToPathTypo3 = 'sysext/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
101 break;
102 case 'typo3temp/test_ext':
103 $packageType = 'T';
104 $relativePackagePathToPathTypo3 = '../typo3temp/test_ext/' . implode('/', array_slice($relativePackagePathToPathSiteSegments, 2));
105 break;
106 }
107 if ($packageType !== NULL && $relativePackagePathToPathSite !== NULL && $relativePackagePathToPathTypo3 !== NULL) {
108 $this->extensionInformation['type'] = $packageType;
109 $this->extensionInformation['siteRelPath'] = $relativePackagePathToPathSite;
110 $this->extensionInformation['typo3RelPath'] = $relativePackagePathToPathTypo3;
111 }
112 }
113 }
114
115 /**
116 * Initialize extension icon
117 *
118 * @return void
119 */
120 protected function initializeExtensionIcon() {
121 $this->extensionInformation['ext_icon'] = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionIcon($this->package->getPackagePath());
122 }
123
124 /**
125 * Register found files in extension array if extension was found
126 *
127 * @param void
128 */
129 protected function initializeExtensionFiles() {
130 foreach ($this->extensionFilesToCheckFor as $fileName) {
131 $absolutePathToFile = $this->package->getPackagePath() . $fileName;
132 if (@is_file($absolutePathToFile)) {
133 $this->extensionInformation[$fileName] = $absolutePathToFile;
134 }
135 }
136 }
137
138 /**
139 * Retrieve an external iterator
140 *
141 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
142 * @return \Traversable An instance of an object implementing Iterator or Traversable
143 */
144 public function getIterator() {
145 return new \ArrayIterator($this->extensionInformation);
146 }
147
148 /**
149 * Whether a offset exists
150 *
151 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
152 * @param mixed $offset An offset to check for.
153 * @return boolean TRUE on success or FALSE on failure.
154 */
155 public function offsetExists($offset) {
156 return isset($this->extensionInformation[$offset]);
157 }
158
159 /**
160 * Offset to retrieve
161 *
162 * @link http://php.net/manual/en/arrayaccess.offsetget.php
163 * @param mixed $offset The offset to retrieve.
164 * @return mixed Can return all value types.
165 */
166 public function offsetGet($offset) {
167 return $this->extensionInformation[$offset];
168 }
169
170 /**
171 * Offset to set
172 *
173 * @link http://php.net/manual/en/arrayaccess.offsetset.php
174 * @param mixed $offset The offset to assign the value to.
175 * @param mixed $value The value to set.
176 * @return void
177 * @throws \InvalidArgumentException
178 */
179 public function offsetSet($offset, $value) {
180 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915115);
181 }
182
183 /**
184 * Offset to unset
185 *
186 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
187 * @param mixed $offset The offset to unset.
188 * @return void
189 * @throws \InvalidArgumentException
190 */
191 public function offsetUnset($offset) {
192 throw new \InvalidArgumentException('The array $GLOBALS[\'TYPO3_LOADED_EXT\'] may not be modified.', 1361915206);
193 }
194
195 /**
196 * String representation of object
197 *
198 * @link http://php.net/manual/en/serializable.serialize.php
199 * @return string the string representation of the object or null
200 */
201 public function serialize() {
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 $this->extensionInformation = unserialize($serialized);
214 }
215
216 /**
217 * Count elements of an object
218 *
219 * @link http://php.net/manual/en/countable.count.php
220 * @return integer The custom count as an integer. The return value is cast to an integer.
221 */
222 public function count() {
223 return count($this->extensionInformation);
224 }
225 }