1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright MediaCT. All rights reserved. |
4
|
|
|
* https://www.mediact.nl |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Mediact\Composer\DependencyInstaller; |
8
|
|
|
|
9
|
|
|
use Composer\Command\ConfigCommand; |
10
|
|
|
use Composer\Command\RequireCommand; |
11
|
|
|
use Composer\Console\Application; |
12
|
|
|
use Composer\Factory; |
13
|
|
|
use Composer\Json\JsonFile; |
14
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
class DependencyInstaller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $definition; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $workingDir; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var null|OutputInterface |
31
|
|
|
*/ |
32
|
|
|
private $output; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param string|null $composerFile |
38
|
|
|
* @param OutputInterface|null $output |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
string $composerFile = null, |
42
|
|
|
OutputInterface $output = null |
43
|
|
|
) { |
44
|
|
|
$composerFile = $composerFile ?: Factory::getComposerFile(); |
45
|
|
|
$composerJson = new JsonFile($composerFile); |
46
|
|
|
$this->definition = $composerJson->read(); |
47
|
|
|
$this->workingDir = dirname($composerFile); |
48
|
|
|
$this->output = $output; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Install a repository. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @param string $type |
56
|
|
|
* @param string $url |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function installRepository(string $name, string $type, string $url) |
61
|
|
|
{ |
62
|
|
|
if (array_key_exists('repositories', $this->definition) |
63
|
|
|
&& array_key_exists($name, $this->definition['repositories']) |
64
|
|
|
) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$application = new Application(); |
69
|
|
|
$command = new ConfigCommand(); |
70
|
|
|
|
71
|
|
|
$definition = clone $application->getDefinition(); |
72
|
|
|
$definition->addArguments($command->getDefinition()->getArguments()); |
73
|
|
|
$definition->addOptions($command->getDefinition()->getOptions()); |
74
|
|
|
|
75
|
|
|
$input = new ArrayInput( |
76
|
|
|
[ |
77
|
|
|
'command' => 'config', |
78
|
|
|
'setting-key' => 'repositories.' . $name, |
79
|
|
|
'setting-value' => [ |
80
|
|
|
$type, |
81
|
|
|
$url |
82
|
|
|
], |
83
|
|
|
'--working-dir' => $this->workingDir |
84
|
|
|
], |
85
|
|
|
$definition |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$application->setAutoExit(false); |
89
|
|
|
$application->run($input, $this->output); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Install a composer package. |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
* @param string $version |
97
|
|
|
* @param bool $dev |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
* |
101
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
102
|
|
|
*/ |
103
|
|
|
public function installPackage(string $name, string $version, bool $dev = true) |
104
|
|
|
{ |
105
|
|
|
$node = $dev ? 'require-dev' : 'require'; |
106
|
|
|
|
107
|
|
|
if (array_key_exists($node, $this->definition) |
108
|
|
|
&& array_key_exists($name, $this->definition[$node]) |
109
|
|
|
) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$application = new Application(); |
114
|
|
|
$command = new RequireCommand(); |
115
|
|
|
|
116
|
|
|
$definition = clone $application->getDefinition(); |
117
|
|
|
$definition->addArguments($command->getDefinition()->getArguments()); |
118
|
|
|
$definition->addOptions($command->getDefinition()->getOptions()); |
119
|
|
|
|
120
|
|
|
$input = new ArrayInput( |
121
|
|
|
[ |
122
|
|
|
'command' => 'require', |
123
|
|
|
'packages' => [$name . ':' . $version], |
124
|
|
|
'--dev' => $dev, |
125
|
|
|
'--no-scripts' => true, |
126
|
|
|
'--no-interaction' => true, |
127
|
|
|
'--no-plugins' => true, |
128
|
|
|
'--working-dir' => $this->workingDir |
129
|
|
|
], |
130
|
|
|
$definition |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$application->setAutoExit(false); |
134
|
|
|
$application->run($input, $this->output); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|