[TASK] Introduce unique signal registration
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / ListUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Susanne Moog <typo3@susannemoog.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 use TYPO3\CMS\Core\Package\PackageInterface;
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * Utility for dealing with extension list related functions
34 *
35 * @TODO: Refactor this API class:
36 * - The methods depend on each other, they take each others result, that could be done internally
37 * - There is no good wording to distinguish existing and loaded extensions
38 * - The name 'listUtility' is not good, the methods could be moved to some 'extensionInformationUtility', or a repository?
39 *
40 * @author Susanne Moog <typo3@susannemoog.de>
41 */
42 class ListUtility implements \TYPO3\CMS\Core\SingletonInterface {
43
44 /**
45 * @var \TYPO3\CMS\Extensionmanager\Utility\EmConfUtility
46 * @inject
47 */
48 protected $emConfUtility;
49
50 /**
51 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
52 * @inject
53 */
54 protected $extensionRepository;
55
56 /**
57 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
58 * @inject
59 */
60 protected $installUtility;
61
62 /**
63 * @var \TYPO3\CMS\Core\Package\PackageManager
64 * @inject
65 */
66 protected $packageManager;
67
68 /**
69 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
70 * @inject
71 */
72 protected $signalSlotDispatcher;
73
74 /**
75 * Returns the list of available, but not necessarily loaded extensions
76 *
77 * @return array Array with two sub-arrays, list array (all extensions with info) and category index
78 * @see getInstExtList()
79 */
80 public function getAvailableExtensions() {
81 $this->emitPackagesMayHaveChangedSignal();
82 $extensions = array();
83 foreach ($this->packageManager->getAvailablePackages() as $package) {
84 // Only TYPO3 related packages could be handled by the extension manager
85 // Composer packages from "Packages" folder will be instanciated as \TYPO3\Flow\Package\Package
86 if (!($package instanceof \TYPO3\CMS\Core\Package\PackageInterface)) {
87 continue;
88 }
89 $installationType = $this->getInstallTypeForPackage($package);
90 $extensions[$package->getPackageKey()] = array(
91 'siteRelPath' => str_replace(PATH_site, '', $package->getPackagePath()),
92 'type' => $installationType,
93 'key' => $package->getPackageKey(),
94 'ext_icon' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionIcon($package->getPackagePath()),
95 );
96 }
97 return $extensions;
98 }
99
100 /**
101 * Emits packages may have changed signal
102 */
103 protected function emitPackagesMayHaveChangedSignal() {
104 $this->signalSlotDispatcher->dispatch('PackageManagement', 'packagesMayHaveChanged');
105 }
106
107 /**
108 * Returns "System", "Global" or "Local" based on extension position in filesystem.
109 *
110 * @param PackageInterface $package
111 * @return string
112 */
113 protected function getInstallTypeForPackage(PackageInterface $package) {
114 foreach (\TYPO3\CMS\Extensionmanager\Domain\Model\Extension::returnInstallPaths() as $installType => $installPath) {
115 if (GeneralUtility::isFirstPartOfStr($package->getPackagePath(), $installPath)) {
116 return $installType;
117 }
118 }
119 return '';
120 }
121
122 /**
123 * Enrich the output of getAvailableExtensions() with an array key installed = 1 if an extension is loaded.
124 *
125 * @param array $availableExtensions
126 * @return array
127 */
128 public function getAvailableAndInstalledExtensions(array $availableExtensions) {
129 foreach (array_keys($this->packageManager->getActivePackages()) as $extKey) {
130 if (isset($availableExtensions[$extKey])) {
131 $availableExtensions[$extKey]['installed'] = TRUE;
132 }
133 }
134 return $availableExtensions;
135 }
136
137 /**
138 * Adds the information from the emconf array to the extension information
139 *
140 * @param array $extensions
141 * @return array
142 */
143 public function enrichExtensionsWithEmConfAndTerInformation(array $extensions) {
144 foreach ($extensions as $extensionKey => $properties) {
145 $emconf = $this->emConfUtility->includeEmConf($properties);
146 if ($emconf) {
147 $extensions[$extensionKey] = array_merge($emconf, $properties);
148 $terObject = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $extensions[$extensionKey]['version']);
149 if ($terObject instanceof \TYPO3\CMS\Extensionmanager\Domain\Model\Extension) {
150 $extensions[$extensionKey]['terObject'] = $terObject;
151 $extensions[$extensionKey]['updateAvailable'] = $this->installUtility->isUpdateAvailable($terObject);
152 $extensions[$extensionKey]['updateToVersion'] = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
153 }
154 } else {
155 unset($extensions[$extensionKey]);
156 }
157 }
158 return $extensions;
159 }
160
161 /**
162 * Gets all available and installed extension with additional information
163 * from em_conf and TER (if available)
164 *
165 * @return array
166 */
167 public function getAvailableAndInstalledExtensionsWithAdditionalInformation() {
168 $availableExtensions = $this->getAvailableExtensions();
169 $availableAndInstalledExtensions = $this->getAvailableAndInstalledExtensions($availableExtensions);
170 return $this->enrichExtensionsWithEmConfAndTerInformation($availableAndInstalledExtensions);
171 }
172 }