forked from box-project/box
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComposerProcessFactory.php
More file actions
175 lines (146 loc) · 4.49 KB
/
ComposerProcessFactory.php
File metadata and controls
175 lines (146 loc) · 4.49 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace KevinGH\Box\Composer;
use Closure;
use Fidry\Console\IO;
use KevinGH\Box\Constants;
use RuntimeException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
use const PHP_OS_FAMILY;
/**
* @final
* @private
*/
class ComposerProcessFactory
{
private string $composerExecutable;
public static function create(
?string $composerExecutable = null,
?IO $io = null,
): self {
$io ??= IO::createNull();
return new self(
null === $composerExecutable
? self::retrieveComposerExecutable(...)
: static fn () => $composerExecutable,
self::retrieveSubProcessVerbosity($io),
$io->isDecorated(),
self::getDefaultEnvVars(),
);
}
/**
* @param Closure():string $composerExecutableFactory
*/
public function __construct(
private readonly Closure $composerExecutableFactory,
private readonly ?string $verbosity,
private readonly bool $ansi,
private readonly array $defaultEnvironmentVariables,
) {
}
public function getVersionProcess(): Process
{
return $this->createProcess(
[
$this->getComposerExecutable(),
'--version',
// Never use ANSI support here as we want to parse the raw output.
'--no-ansi',
],
// Ensure that even if this command gets executed within the app with --quiet it still
// works.
['SHELL_VERBOSITY' => 0],
);
}
public function getDumpAutoloaderProcess(bool $noDev): Process
{
$composerCommand = [$this->getComposerExecutable(), 'dump-autoload', '--classmap-authoritative'];
if (true === $noDev) {
$composerCommand[] = '--no-dev';
}
if (null !== $this->verbosity) {
$composerCommand[] = $this->verbosity;
}
if ($this->ansi) {
$composerCommand[] = '--ansi';
}
return $this->createProcess($composerCommand);
}
public function getVendorDirProcess(): Process
{
return $this->createProcess(
[
$this->getComposerExecutable(),
'config',
'vendor-dir',
// Never use ANSI support here as we want to parse the raw output.
'--no-ansi',
],
// Ensure that even if this command gets executed within the app with --quiet it still
// works.
['SHELL_VERBOSITY' => 0],
);
}
private function createProcess(array $command, array $environmentVariables = []): Process
{
return new Process(
$command,
env: [
...$this->defaultEnvironmentVariables,
...$environmentVariables,
],
);
}
private function getComposerExecutable(): string
{
if (!isset($this->composerExecutable)) {
$this->composerExecutable = ($this->composerExecutableFactory)();
}
return $this->composerExecutable;
}
private static function retrieveSubProcessVerbosity(IO $io): ?string
{
if ($io->isDebug()) {
return '-vvv';
}
if ($io->isVeryVerbose()) {
return '-v';
}
return null;
}
private static function getDefaultEnvVars(): array
{
$vars = ['COMPOSER_ORIGINAL_INIS' => ''];
if ('1' === (string) getenv(Constants::ALLOW_XDEBUG)) {
$vars['COMPOSER_ALLOW_XDEBUG'] = '1';
}
return $vars;
}
private static function retrieveComposerExecutable(): string
{
$executableFinder = new ExecutableFinder();
if (self::isWindows()) {
$executableFinder->setSuffixes(['.exe', '.bat', '.cmd', '.com']);
} else {
$executableFinder->addSuffix('.phar');
}
if (null === $composer = $executableFinder->find('composer')) {
throw new RuntimeException('Could not find a Composer executable.');
}
return $composer;
}
private static function isWindows(): bool
{
return PHP_OS_FAMILY === 'Windows';
}
}