|
8 | 8 | */
|
9 | 9 | class ComposerJsonEditor
|
10 | 10 | {
|
| 11 | + private const COMPOSER_FILEPATH = __DIR__.'/../../composer.json'; |
| 12 | + |
| 13 | + public static function editComposerFileForDeprecation(string $moduleName): void |
| 14 | + { |
| 15 | + |
| 16 | + $composerContent = file_get_contents(self::COMPOSER_FILEPATH); |
| 17 | + if ($composerContent === false) { |
| 18 | + throw new \RuntimeException('Error while loading composer.json file for edition.'); |
| 19 | + } |
| 20 | + $composerJson = \json_decode($composerContent, true); |
| 21 | + $composerJson['autoload']['files'] = self::editFileListForDeprecation($composerJson['autoload']['files'], $moduleName); |
| 22 | + |
| 23 | + $newContent = \json_encode($composerJson, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES); |
| 24 | + \file_put_contents(self::COMPOSER_FILEPATH, $newContent); |
| 25 | + } |
| 26 | + |
11 | 27 | /**
|
12 | 28 | * @param string[] $modules A list of modules
|
13 | 29 | */
|
14 |
| - public static function editFiles(array $modules): void |
| 30 | + public static function editComposerFileForGeneration(array $modules): void |
15 | 31 | {
|
16 |
| - $files = \array_map(function (string $module) { |
17 |
| - return 'generated/'.lcfirst($module).'.php'; |
18 |
| - }, $modules); |
19 |
| - $files[] = 'lib/special_cases.php'; |
20 |
| - $composerContent = file_get_contents(__DIR__.'/../../composer.json'); |
| 32 | + |
| 33 | + $composerContent = file_get_contents(self::COMPOSER_FILEPATH); |
21 | 34 | if ($composerContent === false) {
|
22 | 35 | throw new \RuntimeException('Error while loading composer.json file for edition.');
|
23 | 36 | }
|
24 | 37 | $composerJson = \json_decode($composerContent, true);
|
25 |
| - $composerJson['autoload']['files'] = $files; |
| 38 | + $composerJson['autoload']['files'] = self::editFilesListForGeneration($composerJson['autoload']['files'], $modules); |
| 39 | + |
| 40 | + $newContent = \json_encode($composerJson, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES); |
| 41 | + \file_put_contents(self::COMPOSER_FILEPATH, $newContent); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * @param string[] $oldFiles |
| 49 | + * @param string[] $modules A list of modules |
| 50 | + * @return string[] |
| 51 | + */ |
| 52 | + public static function editFilesListForGeneration(array $oldFiles, array $modules): array |
| 53 | + { |
| 54 | + $files = array_values(array_filter($oldFiles, function ($file) { |
| 55 | + return strpos($file, 'generated/') === false; |
| 56 | + })); |
| 57 | + foreach ($modules as $module) { |
| 58 | + $files[] = 'generated/'.lcfirst($module).'.php'; |
| 59 | + } |
| 60 | + return $files; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param string[] $fileList |
| 65 | + * @return string[] |
| 66 | + */ |
| 67 | + public static function editFileListForDeprecation(array $fileList, string $moduleName): array |
| 68 | + { |
| 69 | + $newList = []; |
| 70 | + foreach ($fileList as $fileName) { |
| 71 | + if ($fileName === "generated/$moduleName.php") { |
| 72 | + $newList[] = "deprecated/$moduleName.php"; |
| 73 | + } else { |
| 74 | + $newList[] = $fileName; |
| 75 | + } |
| 76 | + } |
26 | 77 |
|
27 |
| - $newContent = json_encode($composerJson, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES); |
28 |
| - \file_put_contents(__DIR__.'/../../composer.json', $newContent); |
| 78 | + return $newList; |
29 | 79 | }
|
30 | 80 | }
|
0 commit comments