‪TYPO3CMS  ‪main
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 
52  public function ‪getComposerManifestProposal(string $extensionKey): string
53  {
54  if (!$this->‪isValidExtensionKey($extensionKey)) {
55  throw new \InvalidArgumentException('Extension key ' . $extensionKey . ' is not valid.', 1619446379);
56  }
57 
58  $composerManifestPath = ‪Environment::getExtensionsPath() . '/' . $extensionKey . '/composer.json';
59 
60  if (file_exists($composerManifestPath)) {
61  $composerManifest = json_decode((file_get_contents($composerManifestPath) ?: ''), true) ?? [];
62  if (!is_array($composerManifest) || $composerManifest === []) {
63  return '';
64  }
65  $composerManifest['extra']['typo3/cms']['extension-key'] = $extensionKey;
66  } else {
67  try {
68  $composerManifest = $this->‪getComposerManifestProposalFromTer($extensionKey);
69  } catch (TransferException $e) {
70  return '';
71  }
72  }
73 
74  return (string)json_encode($composerManifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
75  }
76 
81  protected function ‪getComposerManifestProposalFromTer(string $extensionKey): array
82  {
83  $emConf = $this->emConfUtility->includeEmConf($extensionKey, ‪Environment::getExtensionsPath() . '/' . $extensionKey);
84 
85  if (!is_array($emConf) || $emConf === []) {
86  return [];
87  }
88 
89  $response = $this->requestFactory->request(
90  self::TER_COMPOSER_ENDPOINT . '/' . $extensionKey,
91  'post',
92  ['json' => [$emConf]]
93  );
94 
95  if ($response->getStatusCode() !== 200 || ($responseContent = $response->getBody()->getContents()) === '') {
96  return [];
97  }
98 
99  $composerManifest = json_decode($responseContent, true) ?? [];
100  return is_array($composerManifest) ? $composerManifest : [];
101  }
102 
103  protected function ‪isValidExtensionKey(string $extensionKey): bool
104  {
105  return preg_match('/^[0-9a-z._\-]+$/i', $extensionKey)
106  && GeneralUtility::isAllowedAbsPath(‪Environment::getExtensionsPath() . '/' . $extensionKey);
107  }
108 }
‪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\Core\Core\Environment\getExtensionsPath
‪static getExtensionsPath()
Definition: Environment.php:253
‪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\getComposerManifestProposal
‪getComposerManifestProposal(string $extensionKey)
Definition: ComposerManifestProposalGenerator.php:50
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\TER_COMPOSER_ENDPOINT
‪const TER_COMPOSER_ENDPOINT
Definition: ComposerManifestProposalGenerator.php:31
‪TYPO3\CMS\Core\Http\RequestFactory
Definition: RequestFactory.php:30
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\isValidExtensionKey
‪isValidExtensionKey(string $extensionKey)
Definition: ComposerManifestProposalGenerator.php:101
‪TYPO3\CMS\Extensionmanager\Service\ComposerManifestProposalGenerator\getComposerManifestProposalFromTer
‪getComposerManifestProposalFromTer(string $extensionKey)
Definition: ComposerManifestProposalGenerator.php:79
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52