-
-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathComposerJsonEditor.php
More file actions
47 lines (40 loc) · 1.55 KB
/
ComposerJsonEditor.php
File metadata and controls
47 lines (40 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types=1);
namespace Safe\Generator;
use Safe\Templating\Filesystem;
/**
* This class will edit the main composer.json file to add the list of files generated from modules.
*/
class ComposerJsonEditor
{
/**
* @param string[] $modules A list of modules
*/
public static function editComposerFileForGeneration(array $modules): void
{
$composerContent = file_get_contents(Filesystem::projectRootDir() . '/composer.json');
if ($composerContent === false) {
throw new \RuntimeException('Error while loading composer.json file for edition.');
}
/** @var array<string, array<string, string[]>> $composerJson */
$composerJson = \json_decode($composerContent, true);
$composerJson['autoload']['files'] = self::editFilesListForGeneration($composerJson['autoload']['files'], $modules);
$newContent = \json_encode($composerJson, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES) . "\n";
\file_put_contents(Filesystem::projectRootDir() . '/composer.json', $newContent);
}
/**
* @param string[] $oldFiles
* @param string[] $modules A list of modules
* @return string[]
*/
public static function editFilesListForGeneration(array $oldFiles, array $modules): array
{
$files = array_values(array_filter($oldFiles, function ($file) {
return !str_contains($file, 'generated/');
}));
foreach ($modules as $module) {
$files[] = 'generated/'.lcfirst($module).'.php';
}
return $files;
}
}