1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\Installer\Operation; |
4
|
|
|
|
5
|
|
|
use ApiClients\Tools\Installer\OperationInterface; |
6
|
|
|
use Composer\Factory; |
7
|
|
|
use Composer\Json\JsonFile; |
8
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
9
|
|
|
|
10
|
|
|
final class ComposerJson implements OperationInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var JsonFile |
14
|
|
|
*/ |
15
|
|
|
private $jsonFile; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @internal |
19
|
|
|
* @param JsonFile $jsonFile |
20
|
|
|
*/ |
21
|
3 |
|
public function __construct(JsonFile $jsonFile) |
22
|
|
|
{ |
23
|
3 |
|
$this->jsonFile = $jsonFile; |
24
|
3 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return OperationInterface |
28
|
|
|
*/ |
29
|
|
|
public static function create(): OperationInterface |
30
|
|
|
{ |
31
|
|
|
return new self(new JsonFile(Factory::getComposerFile())); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $replacements |
36
|
|
|
* @param array $configuration |
37
|
|
|
* @param SymfonyStyle $style |
38
|
|
|
*/ |
39
|
3 |
|
public function operate(array $replacements, array $configuration, SymfonyStyle $style) |
40
|
|
|
{ |
41
|
3 |
|
$style->section('Updating composer.json'); |
42
|
3 |
|
$style->text('Reading composer.json'); |
43
|
3 |
|
$composerJson = $this->jsonFile->read(); |
44
|
|
|
|
45
|
3 |
|
$style->text('Replacing package name'); |
46
|
3 |
|
$composerJson['name'] = $replacements['package_name']; |
47
|
|
|
|
48
|
3 |
|
$style->text('Adding authors'); |
49
|
3 |
|
$composerJson['authors'] = [ |
50
|
|
|
[ |
51
|
3 |
|
'name' => $replacements['author_name'], |
52
|
3 |
|
'email' => $replacements['author_email'], |
53
|
|
|
], |
54
|
|
|
]; |
55
|
|
|
|
56
|
3 |
|
$style->text('Updating autoload'); |
57
|
3 |
|
$composerJson['autoload']['psr-4'] = [ |
58
|
3 |
|
$replacements['ns_vendor'] . '\\' . $replacements['ns_project'] . '\\' => 'src/', |
59
|
|
|
]; |
60
|
3 |
|
$composerJson['autoload-dev']['psr-4'] = [ |
61
|
3 |
|
$replacements['ns_tests_vendor'] . '\\' . $replacements['ns_project'] . '\\' => 'tests/', |
62
|
|
|
]; |
63
|
|
|
|
64
|
3 |
|
$style->text('Removing package needed for installation and post create script'); |
65
|
|
|
|
66
|
3 |
|
foreach (['require', 'require-dev', 'scripts'] as $index) { |
67
|
3 |
|
if (!isset($configuration[$index])) { |
68
|
3 |
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$itemCount = count($configuration[$index]); |
72
|
2 |
|
$style->text('Removing ' . $itemCount . ' item' . ($itemCount > 1 ? 's' : '') . ' from ' . $index); |
73
|
2 |
|
$composerJson = $this->removeItemFromIndex($composerJson, $configuration, $index); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
$style->text('Writing updated composer.json'); |
77
|
3 |
|
$this->jsonFile->write($composerJson); |
78
|
3 |
|
$style->success('Updated composer.json'); |
79
|
3 |
|
} |
80
|
|
|
|
81
|
2 |
|
private function removeItemFromIndex(array $composerJson, array $configuration, string $index): array |
82
|
|
|
{ |
83
|
2 |
|
foreach ($configuration[$index] as $package) { |
84
|
2 |
|
if (!isset($composerJson[$index][$package])) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
unset($composerJson[$index][$package]); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
return $composerJson; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|