TYPO3 CMS  TYPO3_8-7
ListUtility.php
Go to the documentation of this file.
1 <?php
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 
23 
33 {
37  protected $emConfUtility;
38 
43 
47  protected $installUtility;
48 
52  protected $packageManager;
53 
58 
62  protected $availableExtensions = null;
63 
67  public function injectEmConfUtility(\TYPO3\CMS\Extensionmanager\Utility\EmConfUtility $emConfUtility)
68  {
69  $this->emConfUtility = $emConfUtility;
70  }
71 
75  public function injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository)
76  {
77  $this->extensionRepository = $extensionRepository;
78  }
79 
83  public function injectInstallUtility(\TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility)
84  {
85  $this->installUtility = $installUtility;
86  }
87 
91  public function injectPackageManager(\TYPO3\CMS\Core\Package\PackageManager $packageManager)
92  {
93  $this->packageManager = $packageManager;
94  }
95 
99  public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
100  {
101  $this->signalSlotDispatcher = $signalSlotDispatcher;
102  }
103 
109  public function getAvailableExtensions()
110  {
111  if ($this->availableExtensions === null) {
112  $this->availableExtensions = [];
114  foreach ($this->packageManager->getAvailablePackages() as $package) {
115  $installationType = $this->getInstallTypeForPackage($package);
116  $this->availableExtensions[$package->getPackageKey()] = [
117  'siteRelPath' => str_replace(PATH_site, '', $package->getPackagePath()),
118  'type' => $installationType,
119  'key' => $package->getPackageKey(),
120  'ext_icon' => ExtensionManagementUtility::getExtensionIcon($package->getPackagePath()),
121  ];
122  }
123  }
124 
126  }
127 
131  public function reloadAvailableExtensions()
132  {
133  $this->availableExtensions = null;
134  $this->packageManager->scanAvailablePackages();
135  $this->getAvailableExtensions();
136  }
137 
143  public function getExtension($extensionKey)
144  {
145  return $this->packageManager->getPackage($extensionKey);
146  }
147 
151  protected function emitPackagesMayHaveChangedSignal()
152  {
153  $this->signalSlotDispatcher->dispatch('PackageManagement', 'packagesMayHaveChanged');
154  }
155 
162  protected function getInstallTypeForPackage(PackageInterface $package)
163  {
164  foreach (Extension::returnInstallPaths() as $installType => $installPath) {
165  if (GeneralUtility::isFirstPartOfStr($package->getPackagePath(), $installPath)) {
166  return $installType;
167  }
168  }
169  return '';
170  }
171 
179  {
180  foreach ($this->packageManager->getActivePackages() as $extKey => $_) {
181  if (isset($availableExtensions[$extKey])) {
182  $availableExtensions[$extKey]['installed'] = true;
183  }
184  }
185  return $availableExtensions;
186  }
187 
194  public function enrichExtensionsWithEmConfInformation(array $extensions)
195  {
196  foreach ($extensions as $extensionKey => $properties) {
197  $emconf = $this->emConfUtility->includeEmConf($properties);
198  if ($emconf) {
199  $extensions[$extensionKey] = array_merge($emconf, $properties);
200  } else {
201  unset($extensions[$extensionKey]);
202  }
203  }
204  return $extensions;
205  }
206 
213  public function enrichExtensionsWithEmConfAndTerInformation(array $extensions)
214  {
215  $extensions = $this->enrichExtensionsWithEmConfInformation($extensions);
216  foreach ($extensions as $extensionKey => $properties) {
217  $terObject = $this->getExtensionTerData($extensionKey, $extensions[$extensionKey]['version']);
218  if ($terObject !== null) {
219  $extensions[$extensionKey]['terObject'] = $terObject;
220  $extensions[$extensionKey]['updateAvailable'] = false;
221  $extensions[$extensionKey]['updateToVersion'] = null;
222  $extensionToUpdate = $this->installUtility->getUpdateableVersion($terObject);
223  if ($extensionToUpdate !== false) {
224  $extensions[$extensionKey]['updateAvailable'] = true;
225  $extensions[$extensionKey]['updateToVersion'] = $extensionToUpdate;
226  }
227  }
228  }
229  return $extensions;
230  }
231 
241  protected function getExtensionTerData($extensionKey, $version)
242  {
243  $terObject = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
244  if (!$terObject instanceof Extension) {
245  // Version unknown in TER data, try to find extension
246  $terObject = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
247  if ($terObject instanceof Extension) {
248  // Found in TER now, set version information to the known ones, so we can look if there is a newer one
249  // Use a cloned object, otherwise wrong information is stored in persistenceManager
250  $terObject = clone $terObject;
251  $terObject->setVersion($version);
252  $terObject->setIntegerVersion(
254  );
255  } else {
256  $terObject = null;
257  }
258  }
259 
260  return $terObject;
261  }
262 
269  public function enrichExtensionsWithIconInformation(array $extensions)
270  {
271  foreach ($extensions as &$properties) {
272  $extIconPath = PATH_site . $properties['siteRelPath'] . $properties['ext_icon'];
273  if (@is_file($extIconPath)) {
274  $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $extIconPath);
275  $properties['ext_icon_width'] = $imageInfo->getWidth();
276  $properties['ext_icon_height'] = $imageInfo->getHeight();
277  } else {
278  $properties['ext_icon_width'] = 0;
279  $properties['ext_icon_height'] = 0;
280  }
281  }
282  unset($properties);
283  return $extensions;
284  }
285 
293  {
295  $availableAndInstalledExtensions = $this->getAvailableAndInstalledExtensions($availableExtensions);
296  $availableAndInstalledExtensions = $this->enrichExtensionsWithIconInformation($availableAndInstalledExtensions);
297  return $this->enrichExtensionsWithEmConfAndTerInformation($availableAndInstalledExtensions);
298  }
299 }
injectPackageManager(\TYPO3\CMS\Core\Package\PackageManager $packageManager)
Definition: ListUtility.php:91
getInstallTypeForPackage(PackageInterface $package)
static isFirstPartOfStr($str, $partStr)
injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
Definition: ListUtility.php:99
getAvailableAndInstalledExtensions(array $availableExtensions)
injectEmConfUtility(\TYPO3\CMS\Extensionmanager\Utility\EmConfUtility $emConfUtility)
Definition: ListUtility.php:67
static makeInstance($className,... $constructorArguments)
enrichExtensionsWithEmConfAndTerInformation(array $extensions)
injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository)
Definition: ListUtility.php:75
injectInstallUtility(\TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility)
Definition: ListUtility.php:83
static getExtensionIcon($extensionPath, $returnFullPath=false)