‪TYPO3CMS  10.4
ComposerManifestProposalGenerator.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 GuzzleHttp\Exception\TransferException;
24 use TYPO3\CMS\Extensionmanager\Utility\EmConfUtility;
25 
30 {
31  private const ‪TER_COMPOSER_ENDPOINT = 'https://extensions.typo3.org/composerize';
32 
36  protected ‪$requestFactory;
37 
41  protected ‪$emConfUtility;
42 
44  {
45  $this->requestFactory = ‪$requestFactory;
46  $this->emConfUtility = ‪$emConfUtility;
47  }
48 
55  public function ‪getComposerManifestProposal(string $extensionKey): string
56  {
57  if (!$this->‪isValidExtensionKey($extensionKey)) {
58  throw new \InvalidArgumentException('Extension key ' . $extensionKey . ' is not valid.', 1619446379);
59  }
60 
61  $composerManifestPath = ‪Environment::getExtensionsPath() . '/' . $extensionKey . '/composer.json';
62 
63  if (file_exists($composerManifestPath)) {
64  $composerManifest = json_decode((file_get_contents($composerManifestPath) ?: ''), true) ?? [];
65  if (!is_array($composerManifest) || $composerManifest === []) {
66  return '';
67  }
68  $composerManifest['extra']['typo3/cms']['extension-key'] = $extensionKey;
69  } else {
70  try {
71  $composerManifest = $this->‪getComposerManifestProposalFromTer($extensionKey);
72  } catch (TransferException $e) {
73  return '';
74  }
75  }
76 
77  return json_encode($composerManifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
78  }
79 
84  protected function ‪getComposerManifestProposalFromTer(string $extensionKey): array
85  {
86  $emConf = $this->emConfUtility->includeEmConf(
87  $extensionKey,
88  ['packagePath' => ‪Environment::getExtensionsPath() . '/' . $extensionKey . '/']
89  );
90 
91  if (!is_array($emConf) || $emConf === []) {
92  return [];
93  }
94 
95  $response = $this->requestFactory->request(
96  self::TER_COMPOSER_ENDPOINT . '/' . $extensionKey,
97  'post',
98  ['json' => [$emConf]]
99  );
100 
101  if ($response->getStatusCode() !== 200 || ($responseContent = $response->getBody()->getContents()) === '') {
102  return [];
103  }
104 
105  $composerManifest = json_decode($responseContent, true) ?? [];
106  return is_array($composerManifest) ? $composerManifest : [];
107  }
108 
109  protected function ‪isValidExtensionKey(string $extensionKey): bool
110  {
111  return preg_match('/^[0-9a-z._\-]+$/i', $extensionKey)
112  && GeneralUtility::isAllowedAbsPath(‪Environment::getExtensionsPath() . '/' . $extensionKey);
113  }
114 }
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\__construct
‪__construct(RequestFactory $requestFactory, EmConfUtility $emConfUtility)
Definition: ComposerManifestProposalGenerator.php:41
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator
Definition: ComposerManifestProposalGenerator.php:30
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\$emConfUtility
‪EmConfUtility $emConfUtility
Definition: ComposerManifestProposalGenerator.php:39
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\$requestFactory
‪RequestFactory $requestFactory
Definition: ComposerManifestProposalGenerator.php:35
‪TYPO3\CMS\Extensionmanager\Service
Definition: ComposerManifestProposalGenerator.php:18
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\TER_COMPOSER_ENDPOINT
‪const TER_COMPOSER_ENDPOINT
Definition: ComposerManifestProposalGenerator.php:31
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\getComposerManifestProposal
‪string getComposerManifestProposal(string $extensionKey)
Definition: ComposerManifestProposalGenerator.php:53
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:31
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\isValidExtensionKey
‪isValidExtensionKey(string $extensionKey)
Definition: ComposerManifestProposalGenerator.php:107
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\getComposerManifestProposalFromTer
‪getComposerManifestProposalFromTer(string $extensionKey)
Definition: ComposerManifestProposalGenerator.php:82
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Core\Environment\getExtensionsPath
‪static string getExtensionsPath()
Definition: Environment.php:271