|
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
|
|
|
public function __construct(JsonFile $jsonFile) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->jsonFile = $jsonFile; |
|
24
|
|
|
} |
|
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 SymfonyStyle $style |
|
37
|
|
|
*/ |
|
38
|
|
|
public function operate(array $replacements, SymfonyStyle $style) |
|
39
|
|
|
{ |
|
40
|
|
|
$style->section('Updating composer.json'); |
|
41
|
|
|
$style->text('Reading composer.json'); |
|
42
|
|
|
$composerJson = $this->jsonFile->read(); |
|
43
|
|
|
|
|
44
|
|
|
$style->text('Replacing package name'); |
|
45
|
|
|
$composerJson['name'] = $replacements['package_name']; |
|
46
|
|
|
|
|
47
|
|
|
$style->text('Adding authors'); |
|
48
|
|
|
$composerJson['authors'] = [ |
|
49
|
|
|
[ |
|
50
|
|
|
'name' => $replacements['author_name'], |
|
51
|
|
|
'email' => $replacements['author_email'], |
|
52
|
|
|
], |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
$style->text('Updating autoload'); |
|
56
|
|
|
$composerJson['autoload']['psr-4'][$replacements['ns_vendor'] . '\\' . $replacements['ns_project'] . '\\'] = 'src/'; |
|
57
|
|
|
$composerJson['autoload-dev']['psr-4'][$replacements['ns_tests_vendor'] . '\\' . $replacements['ns_project'] . '\\'] = 'tests/'; |
|
58
|
|
|
|
|
59
|
|
|
$style->text('Removing package needed for installation and post create script'); |
|
60
|
|
|
unset( |
|
61
|
|
|
$composerJson['require']['api-clients/installer'], |
|
62
|
|
|
$composerJson['scripts']['post-create-project-cmd'] |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$style->text('Writing updated composer.json'); |
|
66
|
|
|
$this->jsonFile->write($composerJson); |
|
67
|
|
|
$style->success('Updated composer.json'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|