‪TYPO3CMS  ‪main
checkIntegrityComposer.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 
4 declare(strict_types=1);
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 require __DIR__ . '/../../vendor/autoload.php';
20 
21 if (PHP_SAPI !== 'cli') {
22  die('Script must be called from command line.' . chr(10));
23 }
24 
33 {
37  private ‪$rootComposerJson = [];
38 
39  private ‪$testResults = [];
40 
47  public function ‪execute(): int
48  {
49  ‪$rootComposerJson = __DIR__ . '/../../composer.json';
50  $this->rootComposerJson = json_decode(file_get_contents(‪$rootComposerJson), true);
51  $filesToProcess = $this->‪findExtensionComposerJson();
52  $output = new \Symfony\Component\Console\Output\ConsoleOutput();
53 
54  $resultAcrossAllFiles = 0;
56  foreach ($filesToProcess as $composerJsonFile) {
57  $fullFilePath = $composerJsonFile->getRealPath();
58  $this->‪validateComposerJson($fullFilePath);
59  }
60  if (!empty($this->testResults)) {
61  $table = new \Symfony\Component\Console\Helper\Table($output);
62  $table->setHeaders([
63  'EXT',
64  'type',
65  'Dependency',
66  'should be',
67  'actually is',
68  ]);
69  foreach ($this->testResults as $extKey => $results) {
70  foreach ($results as $result) {
71  $table->addRow([
72  $extKey,
73  $result['type'],
74  $result['dependency'],
75  $result['shouldBe'],
76  $result['actuallyIs'],
77  ]);
78  }
79  }
80  $table->render();
81  $resultAcrossAllFiles = 1;
82  }
83  return $resultAcrossAllFiles;
84  }
85 
91  private function ‪findExtensionComposerJson(): \Symfony\Component\Finder\Finder
92  {
93  ‪$finder = new Symfony\Component\Finder\Finder();
94  $composerFiles = ‪$finder
95  ->files()
96  ->in(__DIR__ . '/../../typo3/sysext/*')
97  ->name('composer.json');
98  return $composerFiles;
99  }
100 
107  private function ‪validateComposerJson(string $composerJsonFile)
108  {
109  $extensionKey = $this->‪extractExtensionKey($composerJsonFile);
110  $extensionComposerJson = json_decode(file_get_contents($composerJsonFile), true);
111  // Check require section
112  foreach ($this->rootComposerJson['require'] as $requireKey => $requireItem) {
113  if (isset($extensionComposerJson['require'][$requireKey]) && $extensionComposerJson['require'][$requireKey] !== $requireItem) {
114  // log inconsistency
115  $this->testResults[$extensionKey][] = [
116  'type' => 'require',
117  'dependency' => $requireKey,
118  'shouldBe' => $requireItem,
119  'actuallyIs' => $extensionComposerJson['require'][$requireKey],
120  ];
121  }
122  }
123  // Check require-dev section
124  foreach ($this->rootComposerJson['require-dev'] as $requireDevKey => $requireDevItem) {
125  if (isset($extensionComposerJson['require-dev'][$requireDevKey]) && $extensionComposerJson['require-dev'][$requireDevKey] !== $requireDevItem) {
126  // log inconsistency
127  $this->testResults[$extensionKey][] = [
128  'type' => 'require-dev',
129  'dependency' => $requireDevKey,
130  'shouldBe' => $requireDevItem,
131  'actuallyIs' => $extensionComposerJson['require-dev'][$requireDevKey],
132  ];
133  }
134  }
135  // Check root composer.json for missing non-sysext required packages
136  foreach ($extensionComposerJson['require'] ?? [] as $requireKey => $requireItem) {
137  if (!str_starts_with($requireKey, 'typo3/cms-') && !isset($this->rootComposerJson['require'][$requireKey])) {
138  $this->testResults['repo-root'][] = [
139  'type' => 'require',
140  'dependency' => $requireKey . ' by ' . $extensionKey,
141  'shouldBe' => $requireItem,
142  'actuallyIs' => 'missing',
143  ];
144  }
145  }
146  }
147 
154  private function ‪extractExtensionKey(string $filename): string
155  {
156  $pattern = '/typo3\/sysext\/(?<extName>[a-z].+?)\//';
157  preg_match_all($pattern, $filename, $matches, PREG_SET_ORDER, 0);
158  return $matches[0]['extName'];
159  }
160 }
161 
163 exit(‪$composerIntegrityChecker->execute());
‪checkIntegrityComposer\findExtensionComposerJson
‪Symfony Component Finder Finder findExtensionComposerJson()
Definition: checkIntegrityComposer.php:90
‪$finder
‪if(PHP_SAPI !=='cli') $finder
Definition: header-comment.php:22
‪checkIntegrityComposer\execute
‪int execute()
Definition: checkIntegrityComposer.php:46
‪checkIntegrityComposer\$testResults
‪$testResults
Definition: checkIntegrityComposer.php:38
‪checkIntegrityComposer\extractExtensionKey
‪string extractExtensionKey(string $filename)
Definition: checkIntegrityComposer.php:153
‪checkIntegrityComposer\$rootComposerJson
‪array $rootComposerJson
Definition: checkIntegrityComposer.php:36
‪$composerIntegrityChecker
‪$composerIntegrityChecker
Definition: checkIntegrityComposer.php:161
‪checkIntegrityComposer
Definition: checkIntegrityComposer.php:33
‪checkIntegrityComposer\validateComposerJson
‪validateComposerJson(string $composerJsonFile)
Definition: checkIntegrityComposer.php:106