‪TYPO3CMS  ‪main
InstallUtility.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;
21 use Psr\Log\LoggerAwareInterface;
22 use Psr\Log\LoggerAwareTrait;
29 use TYPO3\CMS\Core\Package\PackageManager;
33 
39 class ‪InstallUtility implements LoggerAwareInterface
40 {
41  use LoggerAwareTrait;
42 
44 
45  public function ‪__construct(
46  private readonly EventDispatcherInterface $eventDispatcher,
47  private readonly ‪FileHandlingUtility $fileHandlingUtility,
48  private readonly ‪ListUtility $listUtility,
49  private readonly PackageManager $packageManager,
50  private readonly ‪CacheManager $cacheManager,
51  private readonly ‪OpcodeCacheService $opcodeCacheService,
52  private readonly ‪PackageActivationService $packageActivationService,
53  ‪LanguageServiceFactory $languageServiceFactory,
54  ) {
55  $this->languageService = $languageServiceFactory->‪createFromUserPreferences(‪$GLOBALS['BE_USER'] ?? null);
56  }
57 
65  public function ‪install(string ...$extensionKeys): void
66  {
67  $this->packageActivationService->activate($extensionKeys, $this);
68  }
69 
75  public function ‪uninstall(string $extensionKey): void
76  {
77  $dependentExtensions = $this->‪findInstalledExtensionsThatDependOnExtension($extensionKey);
78  if (!empty($dependentExtensions)) {
80  sprintf(
81  $this->languageService->sL(
82  'LLL:EXT:extensionmanager/Resources/Private/Language/locallang.xlf:extensionList.uninstall.dependencyError'
83  ),
84  $extensionKey,
85  implode(', ', $dependentExtensions)
86  ),
87  1342554622
88  );
89  }
90  $this->packageManager->deactivatePackage($extensionKey);
91  $this->eventDispatcher->dispatch(new ‪AfterPackageDeactivationEvent($extensionKey, 'typo3-cms-extension', $this));
92  $this->cacheManager->flushCachesInGroup('system');
93  }
94 
98  public function ‪reloadAvailableExtensions(): void
99  {
100  $this->listUtility->reloadAvailableExtensions();
101  }
102 
106  public function ‪isAvailable(string $extensionKey): bool
107  {
108  return $this->packageManager->isPackageAvailable($extensionKey);
109  }
110 
116  public function ‪reloadPackageInformation(string $extensionKey): void
117  {
118  if ($this->packageManager->isPackageAvailable($extensionKey)) {
119  $this->opcodeCacheService->clearAllActive();
120  $this->packageManager->reloadPackageInformation($extensionKey);
121  }
122  }
123 
129  public function ‪enrichExtensionWithDetails(string $extensionKey, bool $loadTerInformation = true): array
130  {
131  $extension = $this->‪getExtensionArray($extensionKey);
132  if (!$loadTerInformation) {
133  $availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfInformation([$extensionKey => $extension]);
134  } else {
135  $availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfAndTerInformation([$extensionKey => $extension]);
136  }
137  if (!isset($availableAndInstalledExtensions[$extensionKey])) {
139  'Please check your uploaded extension "' . $extensionKey . '". The configuration file "ext_emconf.php" seems to be invalid.',
140  1391432222
141  );
142  }
143  return $availableAndInstalledExtensions[$extensionKey];
144  }
145 
149  public function ‪removeExtension(string $extension): void
150  {
151  $absolutePath = $this->‪enrichExtensionWithDetails($extension)['packagePath'];
152  if ($this->‪isValidExtensionPath($absolutePath)) {
153  if ($this->packageManager->isPackageAvailable($extension)) {
154  // Package manager deletes the extension and removes the entry from PackageStates.php
155  $this->packageManager->deletePackage($extension);
156  } else {
157  // The extension is not listed in PackageStates.php, we can safely remove it
158  $this->fileHandlingUtility->removeDirectory($absolutePath);
159  }
160  } else {
161  throw new ‪ExtensionManagerException('No valid extension path given.', 1342875724);
162  }
163  }
164 
170  protected function ‪findInstalledExtensionsThatDependOnExtension(string $extensionKey): array
171  {
172  $availableAndInstalledExtensions = $this->listUtility->getAvailableAndInstalledExtensionsWithAdditionalInformation();
173  $dependentExtensions = [];
174  foreach ($availableAndInstalledExtensions as $availableAndInstalledExtensionKey => $availableAndInstalledExtension) {
175  if (isset($availableAndInstalledExtension['installed']) && $availableAndInstalledExtension['installed'] === true) {
176  if (is_array($availableAndInstalledExtension['constraints'] ?? false)
177  && is_array($availableAndInstalledExtension['constraints']['depends'])
178  && array_key_exists($extensionKey, $availableAndInstalledExtension['constraints']['depends'])
179  ) {
180  $dependentExtensions[] = $availableAndInstalledExtensionKey;
181  }
182  }
183  }
184  return $dependentExtensions;
185  }
186 
187  protected function ‪getExtensionArray(string $extensionKey): array
188  {
189  $availableExtensions = $this->listUtility->getAvailableExtensions();
190  if (isset($availableExtensions[$extensionKey])) {
191  return $availableExtensions[$extensionKey];
192  }
193  throw new ‪ExtensionManagerException('Extension ' . $extensionKey . ' is not available', 1342864081);
194  }
195 
201  protected function ‪isValidExtensionPath(string $path): bool
202  {
203  $allowedPaths = ‪Extension::returnInstallPaths();
204  foreach ($allowedPaths as $allowedPath) {
205  if (str_starts_with($path, $allowedPath)) {
206  return true;
207  }
208  }
209  return false;
210  }
211 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\isAvailable
‪isAvailable(string $extensionKey)
Definition: InstallUtility.php:106
‪TYPO3\CMS\Extensionmanager\Utility
Definition: DependencyUtility.php:18
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\findInstalledExtensionsThatDependOnExtension
‪findInstalledExtensionsThatDependOnExtension(string $extensionKey)
Definition: InstallUtility.php:170
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension
Definition: Extension.php:30
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\enrichExtensionWithDetails
‪enrichExtensionWithDetails(string $extensionKey, bool $loadTerInformation=true)
Definition: InstallUtility.php:129
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\getExtensionArray
‪getExtensionArray(string $extensionKey)
Definition: InstallUtility.php:187
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\$languageService
‪LanguageService $languageService
Definition: InstallUtility.php:43
‪TYPO3\CMS\Extensionmanager\Utility\ListUtility
Definition: ListUtility.php:41
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\install
‪install(string ... $extensionKeys)
Definition: InstallUtility.php:65
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
Definition: FileHandlingUtility.php:40
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility
Definition: InstallUtility.php:40
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\reloadPackageInformation
‪reloadPackageInformation(string $extensionKey)
Definition: InstallUtility.php:116
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\uninstall
‪uninstall(string $extensionKey)
Definition: InstallUtility.php:75
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\__construct
‪__construct(private readonly EventDispatcherInterface $eventDispatcher, private readonly FileHandlingUtility $fileHandlingUtility, private readonly ListUtility $listUtility, private readonly PackageManager $packageManager, private readonly CacheManager $cacheManager, private readonly OpcodeCacheService $opcodeCacheService, private readonly PackageActivationService $packageActivationService, LanguageServiceFactory $languageServiceFactory,)
Definition: InstallUtility.php:45
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\reloadAvailableExtensions
‪reloadAvailableExtensions()
Definition: InstallUtility.php:98
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:25
‪TYPO3\CMS\Core\Service\OpcodeCacheService
Definition: OpcodeCacheService.php:27
‪TYPO3\CMS\Core\Package\PackageActivationService
Definition: PackageActivationService.php:41
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension\returnInstallPaths
‪static returnInstallPaths()
Definition: Extension.php:297
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\isValidExtensionPath
‪isValidExtensionPath(string $path)
Definition: InstallUtility.php:201
‪TYPO3\CMS\Core\Package\Event\AfterPackageDeactivationEvent
Definition: AfterPackageDeactivationEvent.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Package\Exception\InvalidPackageStateException
Definition: InvalidPackageStateException.php:23
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory\createFromUserPreferences
‪createFromUserPreferences(?AbstractUserAuthentication $user)
Definition: LanguageServiceFactory.php:44
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility\removeExtension
‪removeExtension(string $extension)
Definition: InstallUtility.php:149