[TASK] Re-work/simplify copyright header in PHP files - Part 9
[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 use TYPO3\CMS\Core\Package\PackageInterface;
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Utility for dealing with extension list related functions
21 *
22 * @TODO: Refactor this API class:
23 * - The methods depend on each other, they take each others result, that could be done internally
24 * - There is no good wording to distinguish existing and loaded extensions
25 * - The name 'listUtility' is not good, the methods could be moved to some 'extensionInformationUtility', or a repository?
26 *
27 * @author Susanne Moog <typo3@susannemoog.de>
28 */
29 class ListUtility implements \TYPO3\CMS\Core\SingletonInterface {
30
31 /**
32 * @var \TYPO3\CMS\Extensionmanager\Utility\EmConfUtility
33 * @inject
34 */
35 protected $emConfUtility;
36
37 /**
38 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
39 * @inject
40 */
41 protected $extensionRepository;
42
43 /**
44 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
45 * @inject
46 */
47 protected $installUtility;
48
49 /**
50 * @var \TYPO3\CMS\Core\Package\PackageManager
51 * @inject
52 */
53 protected $packageManager;
54
55 /**
56 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
57 * @inject
58 */
59 protected $signalSlotDispatcher;
60
61 /**
62 * Returns the list of available, but not necessarily loaded extensions
63 *
64 * @return array Array with two sub-arrays, list array (all extensions with info) and category index
65 * @see getInstExtList()
66 */
67 public function getAvailableExtensions() {
68 $this->emitPackagesMayHaveChangedSignal();
69 $extensions = array();
70 foreach ($this->packageManager->getAvailablePackages() as $package) {
71 // Only TYPO3 related packages could be handled by the extension manager
72 // Composer packages from "Packages" folder will be instanciated as \TYPO3\Flow\Package\Package
73 if (!($package instanceof \TYPO3\CMS\Core\Package\PackageInterface)) {
74 continue;
75 }
76 $installationType = $this->getInstallTypeForPackage($package);
77 $extensions[$package->getPackageKey()] = array(
78 'siteRelPath' => str_replace(PATH_site, '', $package->getPackagePath()),
79 'type' => $installationType,
80 'key' => $package->getPackageKey(),
81 'ext_icon' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionIcon($package->getPackagePath()),
82 );
83 }
84 return $extensions;
85 }
86
87 /**
88 * Emits packages may have changed signal
89 */
90 protected function emitPackagesMayHaveChangedSignal() {
91 $this->signalSlotDispatcher->dispatch('PackageManagement', 'packagesMayHaveChanged');
92 }
93
94 /**
95 * Returns "System", "Global" or "Local" based on extension position in filesystem.
96 *
97 * @param PackageInterface $package
98 * @return string
99 */
100 protected function getInstallTypeForPackage(PackageInterface $package) {
101 foreach (\TYPO3\CMS\Extensionmanager\Domain\Model\Extension::returnInstallPaths() as $installType => $installPath) {
102 if (GeneralUtility::isFirstPartOfStr($package->getPackagePath(), $installPath)) {
103 return $installType;
104 }
105 }
106 return '';
107 }
108
109 /**
110 * Enrich the output of getAvailableExtensions() with an array key installed = 1 if an extension is loaded.
111 *
112 * @param array $availableExtensions
113 * @return array
114 */
115 public function getAvailableAndInstalledExtensions(array $availableExtensions) {
116 foreach (array_keys($this->packageManager->getActivePackages()) as $extKey) {
117 if (isset($availableExtensions[$extKey])) {
118 $availableExtensions[$extKey]['installed'] = TRUE;
119 }
120 }
121 return $availableExtensions;
122 }
123
124 /**
125 * Adds the information from the emconf array to the extension information
126 *
127 * @param array $extensions
128 * @return array
129 */
130 public function enrichExtensionsWithEmConfAndTerInformation(array $extensions) {
131 foreach ($extensions as $extensionKey => $properties) {
132 $emconf = $this->emConfUtility->includeEmConf($properties);
133 if ($emconf) {
134 $extensions[$extensionKey] = array_merge($emconf, $properties);
135 $terObject = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $extensions[$extensionKey]['version']);
136 if ($terObject instanceof \TYPO3\CMS\Extensionmanager\Domain\Model\Extension) {
137 $extensions[$extensionKey]['terObject'] = $terObject;
138 $extensions[$extensionKey]['updateAvailable'] = $this->installUtility->isUpdateAvailable($terObject);
139 $extensions[$extensionKey]['updateToVersion'] = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
140 }
141 } else {
142 unset($extensions[$extensionKey]);
143 }
144 }
145 return $extensions;
146 }
147
148 /**
149 * Gets all available and installed extension with additional information
150 * from em_conf and TER (if available)
151 *
152 * @return array
153 */
154 public function getAvailableAndInstalledExtensionsWithAdditionalInformation() {
155 $availableExtensions = $this->getAvailableExtensions();
156 $availableAndInstalledExtensions = $this->getAvailableAndInstalledExtensions($availableExtensions);
157 return $this->enrichExtensionsWithEmConfAndTerInformation($availableAndInstalledExtensions);
158 }
159 }