‪TYPO3CMS  ‪main
ListUtility.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
24 use TYPO3\CMS\Core\Package\PackageManager;
30 
41 {
45  protected ‪$emConfUtility;
46 
50  protected ‪$extensionRepository;
51 
55  protected ‪$packageManager;
56 
60  protected $availableExtensions;
61 
65  protected $eventDispatcher;
66 
70  protected $dependencyUtility;
71 
72  public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher)
73  {
74  $this->‪eventDispatcher = $eventDispatcher;
75  }
76 
77  public function ‪injectEmConfUtility(EmConfUtility ‪$emConfUtility)
78  {
79  $this->emConfUtility = ‪$emConfUtility;
80  }
81 
83  {
84  $this->extensionRepository = ‪$extensionRepository;
85  }
86 
87  public function ‪injectPackageManager(PackageManager ‪$packageManager)
88  {
89  $this->packageManager = ‪$packageManager;
90  }
91 
92  public function ‪injectDependencyUtility(‪DependencyUtility $dependencyUtility)
93  {
94  $this->dependencyUtility = $dependencyUtility;
95  }
96 
102  public function ‪getAvailableExtensions(string $filter = ''): array
103  {
104  if ($this->availableExtensions === null) {
105  $this->availableExtensions = [];
106  $this->‪eventDispatcher->dispatch(new ‪PackagesMayHaveChangedEvent());
107  foreach ($this->packageManager->getAvailablePackages() as $package) {
108  if (!$package->getPackageMetaData()->isExtensionType()) {
109  continue;
110  }
111  $installationType = $this->‪getInstallTypeForPackage($package);
112  if ($filter === '' || $filter === $installationType) {
113  $version = $package->getPackageMetaData()->getVersion();
114  $icon = $package->getPackageIcon();
115  $extensionData = [
116  'packagePath' => $package->getPackagePath(),
117  'type' => $installationType,
118  'key' => $package->getPackageKey(),
119  'version' => $version,
120  'state' => str_starts_with($version, 'dev-') ? 'alpha' : 'stable',
121  'icon' => $icon ? ‪PathUtility::getAbsoluteWebPath($package->getPackagePath() . $icon) : '',
122  'title' => $package->getPackageMetaData()->getTitle(),
123  ];
124  $this->availableExtensions[$package->getPackageKey()] = $extensionData;
125  }
126  }
127  }
128 
129  return $this->availableExtensions;
130  }
131 
135  public function ‪reloadAvailableExtensions(): void
136  {
137  $this->availableExtensions = null;
138  $this->packageManager->scanAvailablePackages();
139  $this->‪getAvailableExtensions();
140  }
141 
142  public function ‪getExtension(string $extensionKey): ‪PackageInterface
143  {
144  return $this->packageManager->getPackage($extensionKey);
145  }
146 
150  protected function ‪getInstallTypeForPackage(‪PackageInterface $package): string
151  {
153  return $package->‪getPackageMetaData()->isFrameworkType() ? 'System' : 'Local';
154  }
155  foreach (‪Extension::returnInstallPaths() as $installType => $installPath) {
156  if (str_starts_with($package->‪getPackagePath(), $installPath)) {
157  return $installType;
158  }
159  }
160  return '';
161  }
162 
166  public function ‪getAvailableAndInstalledExtensions(array $availableExtensions): array
167  {
168  foreach ($this->packageManager->getActivePackages() as $extKey => $_) {
169  if (isset($availableExtensions[$extKey])) {
170  $availableExtensions[$extKey]['installed'] = true;
171  }
172  }
173  return $availableExtensions;
174  }
175 
179  public function ‪enrichExtensionsWithEmConfInformation(array $extensions): array
180  {
181  foreach ($extensions as $extensionKey => $properties) {
182  $emConf = $this->emConfUtility->includeEmConf($extensionKey, $properties['packagePath'] ?? '');
183  if (!is_array($emConf)) {
184  continue;
185  }
186  $extensions[$extensionKey] = array_merge($emConf, $properties);
187  $extensions[$extensionKey]['state'] = $emConf['state'] ?? $extensions[$extensionKey]['state'] ?? 'stable';
188  }
189  return $extensions;
190  }
191 
195  public function ‪enrichExtensionsWithEmConfAndTerInformation(array $extensions): array
196  {
197  $extensions = $this->‪enrichExtensionsWithEmConfInformation($extensions);
198  foreach ($extensions as $extensionKey => $properties) {
199  $terObject = $this->‪getExtensionTerData($extensionKey, $properties['version'] ?? '');
200  if ($terObject === null) {
201  continue;
202  }
203  $extensions[$extensionKey]['terObject'] = $terObject;
204  $extensions[$extensionKey]['remote'] = $terObject->getRemoteIdentifier();
205  $extensions[$extensionKey]['updateAvailable'] = false;
206  $extensions[$extensionKey]['updateToVersion'] = null;
207 
208  $extensionToUpdate = $this->‪getUpdateableVersion($terObject);
209  if ($extensionToUpdate === null) {
210  continue;
211  }
212  $extensions[$extensionKey]['updateAvailable'] = true;
213  $extensions[$extensionKey]['updateToVersion'] = $extensionToUpdate;
214  }
215  return $extensions;
216  }
217 
227  protected function ‪getExtensionTerData(string $extensionKey, string $version): ?Extension
228  {
229  $terObject = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
230  if (!$terObject instanceof Extension) {
231  // Version unknown in TER data, try to find extension
232  $terObject = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
233  if ($terObject instanceof Extension) {
234  // Found in TER now, set version information to the known ones, so we can look if there is a newer one
235  // Use a cloned object, otherwise wrong information is stored in persistenceManager
236  $terObject = clone $terObject;
237  $terObject->setVersion($version);
238  $terObject->setIntegerVersion(
240  );
241  } else {
242  $terObject = null;
243  }
244  }
245 
246  return $terObject;
247  }
248 
253  public function ‪getAvailableAndInstalledExtensionsWithAdditionalInformation(string $filter = ''): array
254  {
255  $availableExtensions = $this->‪getAvailableExtensions($filter);
256  $availableAndInstalledExtensions = $this->‪getAvailableAndInstalledExtensions($availableExtensions);
257  return $this->‪enrichExtensionsWithEmConfAndTerInformation($availableAndInstalledExtensions);
258  }
259 
265  protected function ‪getUpdateableVersion(‪Extension $extensionData): ?‪Extension
266  {
267  // Only check for update for TER extensions
268  $version = $extensionData->‪getIntegerVersion();
269  $extensionUpdates = $this->extensionRepository->findByVersionRangeAndExtensionKeyOrderedByVersion(
270  $extensionData->‪getExtensionKey(),
271  $version,
272  0,
273  false
274  );
275  if ($extensionUpdates->count() > 0) {
276  foreach ($extensionUpdates as $extensionUpdate) {
278  $this->dependencyUtility->checkDependencies($extensionUpdate);
279  if (!$this->dependencyUtility->hasDependencyErrors()) {
280  return $extensionUpdate;
281  }
282  }
283  }
284  return null;
285  }
286 }
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\reloadAvailableExtensions
‪reloadAvailableExtensions()
Definition: ListUtility.php:129
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\injectEmConfUtility
‪injectEmConfUtility(EmConfUtility $emConfUtility)
Definition: ListUtility.php:71
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\getExtension
‪getExtension(string $extensionKey)
Definition: ListUtility.php:136
‪TYPO3\CMS\Core\Utility\VersionNumberUtility
Definition: VersionNumberUtility.php:26
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\getAvailableExtensions
‪array[] getAvailableExtensions(string $filter='')
Definition: ListUtility.php:96
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\$packageManager
‪PackageManager $packageManager
Definition: ListUtility.php:52
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\enrichExtensionsWithEmConfInformation
‪enrichExtensionsWithEmConfInformation(array $extensions)
Definition: ListUtility.php:173
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Utility\VersionNumberUtility\convertVersionNumberToInteger
‪static int convertVersionNumberToInteger(string $versionNumber)
Definition: VersionNumberUtility.php:33
‪TYPO3\CMS\Extensionmanager\Utility
Definition: DependencyUtility.php:18
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension
Definition: Extension.php:30
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static isComposerMode()
Definition: Environment.php:137
‪TYPO3\CMS\Core\Package\PackageInterface\getPackagePath
‪string getPackagePath()
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\injectPackageManager
‪injectPackageManager(PackageManager $packageManager)
Definition: ListUtility.php:81
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\getUpdateableVersion
‪Extension null getUpdateableVersion(Extension $extensionData)
Definition: ListUtility.php:259
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension\getIntegerVersion
‪getIntegerVersion()
Definition: Extension.php:365
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility
Definition: ListUtility.php:41
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:24
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\injectDependencyUtility
‪injectDependencyUtility(DependencyUtility $dependencyUtility)
Definition: ListUtility.php:86
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath(string $targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:52
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\enrichExtensionsWithEmConfAndTerInformation
‪enrichExtensionsWithEmConfAndTerInformation(array $extensions)
Definition: ListUtility.php:189
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\getInstallTypeForPackage
‪getInstallTypeForPackage(PackageInterface $package)
Definition: ListUtility.php:144
‪TYPO3\CMS\Extensionmanager\Utility\DependencyUtility
Definition: DependencyUtility.php:37
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
Definition: ExtensionRepository.php:37
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\getExtensionTerData
‪Extension null getExtensionTerData(string $extensionKey, string $version)
Definition: ListUtility.php:221
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\eventDispatcher
‪array< string, $availableExtensions;protected EventDispatcherInterface $eventDispatcher;protected DependencyUtility $dependencyUtility;public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher) { $this-> eventDispatcher
Definition: ListUtility.php:68
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\$emConfUtility
‪EmConfUtility $emConfUtility
Definition: ListUtility.php:44
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\injectExtensionRepository
‪injectExtensionRepository(ExtensionRepository $extensionRepository)
Definition: ListUtility.php:76
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension\returnInstallPaths
‪static returnInstallPaths()
Definition: Extension.php:297
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\getAvailableAndInstalledExtensionsWithAdditionalInformation
‪getAvailableAndInstalledExtensionsWithAdditionalInformation(string $filter='')
Definition: ListUtility.php:247
‪TYPO3\CMS\Core\Package\PackageInterface\getPackageMetaData
‪getPackageMetaData()
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Package\Event\PackagesMayHaveChangedEvent
Definition: PackagesMayHaveChangedEvent.php:23
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension\getExtensionKey
‪getExtensionKey()
Definition: Extension.php:176
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\getAvailableAndInstalledExtensions
‪getAvailableAndInstalledExtensions(array $availableExtensions)
Definition: ListUtility.php:160
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility\$extensionRepository
‪ExtensionRepository $extensionRepository
Definition: ListUtility.php:48
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension\setVersion
‪setVersion(string $version)
Definition: Extension.php:262