1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the humbug/php-scoper package. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
9
|
|
|
* Pádraic Brady <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\Console; |
16
|
|
|
|
17
|
|
|
use Fidry\Console\Command\CommandRegistry; |
18
|
|
|
use Fidry\Console\Input\IO; |
19
|
|
|
use Humbug\PhpScoper\Configuration\Configuration; |
20
|
|
|
use Humbug\PhpScoper\Configuration\ConfigurationFactory; |
21
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
22
|
|
|
use Symfony\Component\Console\Input\StringInput; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
25
|
|
|
use Symfony\Component\Filesystem\Path; |
26
|
|
|
use function assert; |
27
|
|
|
use function count; |
28
|
|
|
use function file_exists; |
29
|
|
|
use function Safe\sprintf; |
30
|
|
|
use function trim; |
31
|
|
|
use const DIRECTORY_SEPARATOR; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @private |
35
|
|
|
*/ |
36
|
|
|
final class ConfigLoader |
37
|
|
|
{ |
38
|
|
|
private CommandRegistry $commandRegistry; |
39
|
|
|
private Filesystem $fileSystem; |
40
|
|
|
private ConfigurationFactory $configFactory; |
41
|
|
|
|
42
|
|
|
public function __construct( |
43
|
|
|
CommandRegistry $commandRegistry, |
44
|
|
|
Filesystem $fileSystem, |
45
|
|
|
ConfigurationFactory $configFactory |
46
|
|
|
) { |
47
|
|
|
$this->commandRegistry = $commandRegistry; |
48
|
|
|
$this->fileSystem = $fileSystem; |
49
|
|
|
$this->configFactory = $configFactory; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param non-empty-string|null $configFilePath Canonical absolute path |
|
|
|
|
54
|
|
|
* @param non-empty-string $defaultConfigFilePath |
55
|
|
|
* @param list<non-empty-string> $paths List of canonical absolute paths |
56
|
|
|
*/ |
57
|
|
|
public function loadConfig( |
58
|
|
|
IO $io, |
59
|
|
|
string $prefix, |
60
|
|
|
bool $noConfig, |
61
|
|
|
?string $configFilePath, |
62
|
|
|
string $defaultConfigFilePath, |
63
|
|
|
bool $isInitCommandExecuted, |
64
|
|
|
array $paths, |
65
|
|
|
string $cwd |
66
|
|
|
): Configuration { |
67
|
|
|
$prefix = trim($prefix); |
68
|
|
|
$defaultConfigFilePath = $this->canonicalizePath($defaultConfigFilePath, $cwd); |
69
|
|
|
|
70
|
|
|
if ($noConfig) { |
71
|
|
|
return $this->loadConfigWithoutConfigFile( |
72
|
|
|
$io, |
73
|
|
|
$prefix, |
74
|
|
|
$paths, |
75
|
|
|
$cwd, |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (null === $configFilePath && !$isInitCommandExecuted) { |
80
|
|
|
$configFilePath = $this->loadDefaultConfig( |
81
|
|
|
$io, |
82
|
|
|
$defaultConfigFilePath, |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
if (null === $configFilePath) { |
86
|
|
|
return $this->loadConfig( |
87
|
|
|
$io, |
88
|
|
|
$prefix, |
89
|
|
|
$noConfig, |
90
|
|
|
$configFilePath, |
91
|
|
|
$defaultConfigFilePath, |
92
|
|
|
true, |
93
|
|
|
$paths, |
94
|
|
|
$cwd, |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
self::logConfigFilePathFound($io, $configFilePath); |
100
|
|
|
|
101
|
|
|
return $this->loadConfiguration($configFilePath, $prefix, $paths, $cwd); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param list<non-empty-string> $paths |
|
|
|
|
106
|
|
|
*/ |
107
|
|
|
private function loadConfigWithoutConfigFile( |
108
|
|
|
IO $io, |
109
|
|
|
string $prefix, |
110
|
|
|
array $paths, |
111
|
|
|
string $cwd |
112
|
|
|
): Configuration { |
113
|
|
|
$io->writeln( |
114
|
|
|
'Loading without configuration file.', |
115
|
|
|
OutputInterface::VERBOSITY_DEBUG, |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
return $this->loadConfiguration(null, $prefix, $paths, $cwd); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param non-empty-string $defaultConfigFilePath |
|
|
|
|
123
|
|
|
* |
124
|
|
|
* @return non-empty-string|null Config file path when found otherwise executes the init command |
|
|
|
|
125
|
|
|
*/ |
126
|
|
|
private function loadDefaultConfig(IO $io, string $defaultConfigFilePath): ?string |
127
|
|
|
{ |
128
|
|
|
$configFilePath = $defaultConfigFilePath; |
129
|
|
|
|
130
|
|
|
if (file_exists($configFilePath)) { |
131
|
|
|
return $configFilePath; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$initInput = new StringInput(''); |
135
|
|
|
$initInput->setInteractive($io->isInteractive()); |
136
|
|
|
|
137
|
|
|
$this->commandRegistry |
138
|
|
|
->getCommand('init') |
139
|
|
|
->execute( |
140
|
|
|
new IO( |
141
|
|
|
$initInput, |
142
|
|
|
$io->getOutput(), |
143
|
|
|
), |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$io->writeln( |
147
|
|
|
sprintf( |
|
|
|
|
148
|
|
|
'Config file "<comment>%s</comment>" not found. Skipping.', |
149
|
|
|
$configFilePath, |
150
|
|
|
), |
151
|
|
|
OutputInterface::VERBOSITY_DEBUG, |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private static function logConfigFilePathFound(IO $io, ?string $configFilePath): void |
158
|
|
|
{ |
159
|
|
|
if (null === $configFilePath) { |
160
|
|
|
$io->writeln( |
161
|
|
|
'Loading without configuration file.', |
162
|
|
|
OutputInterface::VERBOSITY_DEBUG, |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (!file_exists($configFilePath)) { |
169
|
|
|
throw new RuntimeException( |
170
|
|
|
sprintf( |
|
|
|
|
171
|
|
|
'Could not find the configuration file "%s".', |
172
|
|
|
$configFilePath, |
173
|
|
|
), |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$io->writeln( |
178
|
|
|
sprintf( |
|
|
|
|
179
|
|
|
'Using the configuration file "%s".', |
180
|
|
|
$configFilePath, |
181
|
|
|
), |
182
|
|
|
OutputInterface::VERBOSITY_DEBUG, |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param non-empty-string|null $configFilePath |
|
|
|
|
188
|
|
|
* @param list<non-empty-string> $paths |
189
|
|
|
*/ |
190
|
|
|
private function loadConfiguration( |
191
|
|
|
?string $configFilePath, |
192
|
|
|
string $prefix, |
193
|
|
|
array $paths, |
194
|
|
|
string $cwd |
195
|
|
|
): Configuration { |
196
|
|
|
return $this->configurePaths( |
197
|
|
|
$this->configurePrefix( |
198
|
|
|
$this->configFactory->create($configFilePath, $paths), |
199
|
|
|
$prefix, |
200
|
|
|
), |
201
|
|
|
$cwd, |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
private function configurePrefix(Configuration $config, string $prefix): Configuration |
206
|
|
|
{ |
207
|
|
|
if ('' !== $prefix) { |
208
|
|
|
return $this->configFactory->createWithPrefix( |
209
|
|
|
$config, |
210
|
|
|
$prefix, |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $config; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private function configurePaths( |
218
|
|
|
Configuration $config, |
219
|
|
|
string $cwd |
220
|
|
|
): Configuration { |
221
|
|
|
// Use the current working directory as the path if no file has been |
222
|
|
|
// found |
223
|
|
|
if (0 === count($config->getFilesWithContents())) { |
224
|
|
|
return $this->configFactory->createWithPaths( |
225
|
|
|
$config, |
226
|
|
|
[$cwd], |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $config; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param non-empty-string $path |
|
|
|
|
235
|
|
|
* |
236
|
|
|
* @return non-empty-string Absolute canonical path |
|
|
|
|
237
|
|
|
*/ |
238
|
|
|
private function canonicalizePath(string $path, string $cwd): string |
239
|
|
|
{ |
240
|
|
|
$canonicalPath = Path::canonicalize( |
241
|
|
|
$this->fileSystem->isAbsolutePath($path) |
242
|
|
|
? $path |
243
|
|
|
: $cwd.DIRECTORY_SEPARATOR.$path, |
244
|
|
|
); |
245
|
|
|
|
246
|
|
|
assert('' !== $canonicalPath); |
247
|
|
|
|
248
|
|
|
return $canonicalPath; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|