TYPO3 CMS  TYPO3_8-7
AbstractDownloadExtensionUpdate.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 
24 
29 {
33  protected $title = 'Install an Extension from the Extension Repository';
34 
39  protected $extensionDetails = [];
40 
44  protected $repositoryUrl = 'https://typo3.org/fileadmin/ter/@filename';
45 
54  protected function installExtension($extensionKey, &$customMessage)
55  {
56  $updateSuccessful = true;
58  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
59 
61  $extensionListUtility = $objectManager->get(ListUtility::class);
62  $availableExtensions = $extensionListUtility->getAvailableExtensions();
63  $extensionDetails = $this->getExtensionDetails($extensionKey);
64 
65  $isExtensionAvailable = !empty($availableExtensions[$extensionKey]);
66  $isComposerMode = Bootstrap::usesComposerClassLoading();
67 
68  if (!$isComposerMode && !$isExtensionAvailable) {
70  $extensionTerUtility = $objectManager->get(TerUtility::class);
71  if (empty($extensionDetails)) {
72  $updateSuccessful = false;
73  $customMessage .= 'No version information for extension ' . $extensionKey . ' found. Can not install the extension.';
74  }
75  $t3xContent = $this->fetchExtension($extensionKey, $extensionDetails['versionString']);
76  if (empty($t3xContent)) {
77  $updateSuccessful = false;
78  $customMessage .= 'The extension ' . $extensionKey . ' could not be downloaded.';
79  }
80  $t3xExtracted = $extensionTerUtility->decodeExchangeData($t3xContent);
81  if (empty($t3xExtracted) || !is_array($t3xExtracted) || empty($t3xExtracted['extKey'])) {
82  $updateSuccessful = false;
83  $customMessage .= 'The extension ' . $extensionKey . ' could not be extracted.';
84  }
85 
87  $extensionFileHandlingUtility = $objectManager->get(FileHandlingUtility::class);
88  $extensionFileHandlingUtility->unpackExtensionFromExtensionDataArray($t3xExtracted);
89 
90  // The listUtility now needs to have the regenerated list of packages
91  $extensionListUtility->reloadAvailableExtensions();
92  }
93 
94  if ($isComposerMode && !$isExtensionAvailable) {
95  $updateSuccessful = false;
96  $customMessage .= 'The extension ' . $extensionKey . ' can not be downloaded since ' .
97  'Composer is used for package management. Please require this ' .
98  'extension as package via Composer: ' .
99  '"composer require ' . $extensionDetails['composerName'] . ':^' . $extensionDetails['versionString'] . '"';
100  }
101 
102  if ($updateSuccessful) {
104  $extensionInstallUtility = $objectManager->get(InstallUtility::class);
105  $extensionInstallUtility->install($extensionKey);
106  }
107 
108  return $updateSuccessful;
109  }
110 
118  protected function getExtensionDetails($extensionKey)
119  {
120  if (array_key_exists($extensionKey, $this->extensionDetails)) {
121  return $this->extensionDetails[$extensionKey];
122  }
123 
124  return [];
125  }
126 
136  protected function fetchExtension($extensionKey, $version)
137  {
138  if (empty($extensionKey) || empty($version)) {
139  throw new \InvalidArgumentException(
140  'No extension key for fetching an extension was given.',
141  1344687432
142  );
143  }
144 
145  $filename = $extensionKey[0] . '/' . $extensionKey[1] . '/' . $extensionKey . '_' . $version . '.t3x';
146  $url = str_replace('@filename', $filename, $this->repositoryUrl);
147 
148  return $this->fetchUrl($url);
149  }
150 
163  protected function fetchUrl($url)
164  {
165  if (empty($url)) {
166  throw new \InvalidArgumentException(
167  'No URL for downloading an extension given.',
168  1344687436
169  );
170  }
171 
172  $fileContent = GeneralUtility::getUrl($url);
173 
174  // Can not fetch url, throw an exception
175  if ($fileContent === false) {
176  throw new \RuntimeException(
177  'Can not fetch URL "' . $url . '". Possible reasons are network problems or misconfiguration.',
178  1344685036
179  );
180  }
181 
182  return $fileContent;
183  }
184 }
static makeInstance($className,... $constructorArguments)