235435896d3ad4aa3f2c5c5e5e3e179e4e9a0131
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / ListUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
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 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\VersionNumberUtility;
21 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
22
23 /**
24 * Utility for dealing with extension list related functions
25 *
26 * @TODO: Refactor this API class:
27 * - The methods depend on each other, they take each others result, that could be done internally
28 * - There is no good wording to distinguish existing and loaded extensions
29 * - The name 'listUtility' is not good, the methods could be moved to some 'extensionInformationUtility', or a repository?
30 */
31 class ListUtility implements \TYPO3\CMS\Core\SingletonInterface
32 {
33 /**
34 * @var \TYPO3\CMS\Extensionmanager\Utility\EmConfUtility
35 */
36 protected $emConfUtility;
37
38 /**
39 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
40 */
41 protected $extensionRepository;
42
43 /**
44 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
45 */
46 protected $installUtility;
47
48 /**
49 * @var \TYPO3\CMS\Core\Package\PackageManager
50 */
51 protected $packageManager;
52
53 /**
54 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
55 */
56 protected $signalSlotDispatcher;
57
58 /**
59 * @var array
60 */
61 protected $availableExtensions = NULL;
62
63 /**
64 * @param \TYPO3\CMS\Extensionmanager\Utility\EmConfUtility $emConfUtility
65 */
66 public function injectEmConfUtility(\TYPO3\CMS\Extensionmanager\Utility\EmConfUtility $emConfUtility)
67 {
68 $this->emConfUtility = $emConfUtility;
69 }
70
71 /**
72 * @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository
73 */
74 public function injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository)
75 {
76 $this->extensionRepository = $extensionRepository;
77 }
78
79 /**
80 * @param \TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility
81 */
82 public function injectInstallUtility(\TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility)
83 {
84 $this->installUtility = $installUtility;
85 }
86
87 /**
88 * @param \TYPO3\CMS\Core\Package\PackageManager $packageManager
89 */
90 public function injectPackageManager(\TYPO3\CMS\Core\Package\PackageManager $packageManager)
91 {
92 $this->packageManager = $packageManager;
93 }
94
95 /**
96 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
97 */
98 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
99 {
100 $this->signalSlotDispatcher = $signalSlotDispatcher;
101 }
102
103 /**
104 * Returns the list of available, but not necessarily loaded extensions
105 *
106 * @return array[] All extensions with info
107 */
108 public function getAvailableExtensions()
109 {
110 if ($this->availableExtensions === null) {
111 $this->availableExtensions = [];
112 $this->emitPackagesMayHaveChangedSignal();
113 foreach ($this->packageManager->getAvailablePackages() as $package) {
114 $installationType = $this->getInstallTypeForPackage($package);
115 $this->availableExtensions[$package->getPackageKey()] = array(
116 'siteRelPath' => str_replace(PATH_site, '', $package->getPackagePath()),
117 'type' => $installationType,
118 'key' => $package->getPackageKey(),
119 'ext_icon' => ExtensionManagementUtility::getExtensionIcon($package->getPackagePath()),
120 );
121 }
122 }
123
124 return $this->availableExtensions;
125 }
126
127 /**
128 * Reset and reload the available extensions
129 */
130 public function reloadAvailableExtensions() {
131 $this->availableExtensions = null;
132 $this->getAvailableExtensions();
133 }
134
135 /**
136 * @param string $extensionKey
137 * @return \TYPO3\CMS\Core\Package\PackageInterface
138 * @throws \TYPO3\CMS\Core\Package\Exception\UnknownPackageException if the specified package is unknown
139 */
140 public function getExtension($extensionKey)
141 {
142 return $this->packageManager->getPackage($extensionKey);
143 }
144
145 /**
146 * Emits packages may have changed signal
147 */
148 protected function emitPackagesMayHaveChangedSignal()
149 {
150 $this->signalSlotDispatcher->dispatch('PackageManagement', 'packagesMayHaveChanged');
151 }
152
153 /**
154 * Returns "System", "Global" or "Local" based on extension position in filesystem.
155 *
156 * @param PackageInterface $package
157 * @return string
158 */
159 protected function getInstallTypeForPackage(PackageInterface $package)
160 {
161 foreach (Extension::returnInstallPaths() as $installType => $installPath) {
162 if (GeneralUtility::isFirstPartOfStr($package->getPackagePath(), $installPath)) {
163 return $installType;
164 }
165 }
166 return '';
167 }
168
169 /**
170 * Enrich the output of getAvailableExtensions() with an array key installed = 1 if an extension is loaded.
171 *
172 * @param array $availableExtensions
173 * @return array
174 */
175 public function getAvailableAndInstalledExtensions(array $availableExtensions)
176 {
177 foreach ($this->packageManager->getActivePackages() as $extKey => $_) {
178 if (isset($availableExtensions[$extKey])) {
179 $availableExtensions[$extKey]['installed'] = true;
180 }
181 }
182 return $availableExtensions;
183 }
184
185 /**
186 * Adds the information from the emconf array to the extension information
187 *
188 * @param array $extensions
189 * @return array
190 */
191 public function enrichExtensionsWithEmConfAndTerInformation(array $extensions)
192 {
193 foreach ($extensions as $extensionKey => $properties) {
194 $emconf = $this->emConfUtility->includeEmConf($properties);
195 if ($emconf) {
196 $extensions[$extensionKey] = array_merge($emconf, $properties);
197 $terObject = $this->getExtensionTerData($extensionKey, $extensions[$extensionKey]['version']);
198 if ($terObject !== null) {
199 $extensions[$extensionKey]['terObject'] = $terObject;
200 $extensions[$extensionKey]['updateAvailable'] = false;
201 $extensions[$extensionKey]['updateToVersion'] = null;
202 $extensionToUpdate = $this->installUtility->getUpdateableVersion($terObject);
203 if ($extensionToUpdate !== false) {
204 $extensions[$extensionKey]['updateAvailable'] = true;
205 $extensions[$extensionKey]['updateToVersion'] = $extensionToUpdate;
206 }
207 }
208 } else {
209 unset($extensions[$extensionKey]);
210 }
211 }
212 return $extensions;
213 }
214
215 /**
216 * Tries to find given extension with given version in TER data.
217 * If extension is found but not the given version, we return TER data from highest version with version data set to
218 * given one.
219 *
220 * @param string $extensionKey Key of the extension
221 * @param string $version String representation of version number
222 * @return Extension|NULL Extension TER object or NULL if nothing found
223 */
224 protected function getExtensionTerData($extensionKey, $version)
225 {
226 $terObject = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
227 if (!$terObject instanceof Extension) {
228 // Version unknown in TER data, try to find extension
229 $terObject = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
230 if ($terObject instanceof Extension) {
231 // Found in TER now, set version information to the known ones, so we can look if there is a newer one
232 // Use a cloned object, otherwise wrong information is stored in persistenceManager
233 $terObject = clone $terObject;
234 $terObject->setVersion($version);
235 $terObject->setIntegerVersion(
236 VersionNumberUtility::convertVersionNumberToInteger($terObject->getVersion())
237 );
238 } else {
239 $terObject = null;
240 }
241 }
242
243 return $terObject;
244 }
245
246 /**
247 * Adds information about icon size to the extension information
248 *
249 * @param array $extensions
250 * @return array
251 */
252 public function enrichExtensionsWithIconInformation(array $extensions)
253 {
254 foreach ($extensions as &$properties) {
255 $iInfo = @getimagesize(PATH_site . $properties['siteRelPath'] . $properties['ext_icon']);
256 if ($iInfo !== false) {
257 $properties['ext_icon_width'] = $iInfo[0];
258 $properties['ext_icon_height'] = $iInfo[1];
259 } else {
260 $properties['ext_icon_width'] = 0;
261 $properties['ext_icon_height'] = 0;
262 }
263 }
264 unset($properties);
265 return $extensions;
266 }
267
268 /**
269 * Gets all available and installed extension with additional information
270 * from em_conf and TER (if available)
271 *
272 * @return array
273 */
274 public function getAvailableAndInstalledExtensionsWithAdditionalInformation()
275 {
276 $availableExtensions = $this->getAvailableExtensions();
277 $availableAndInstalledExtensions = $this->getAvailableAndInstalledExtensions($availableExtensions);
278 $availableAndInstalledExtensions = $this->enrichExtensionsWithIconInformation($availableAndInstalledExtensions);
279 return $this->enrichExtensionsWithEmConfAndTerInformation($availableAndInstalledExtensions);
280 }
281 }