generated from yiisoft/package-template
/
ProcessHelper.php
273 lines (242 loc) · 8.3 KB
/
ProcessHelper.php
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
declare(strict_types=1);
namespace Yiisoft\Config\Composer;
use Composer\Composer;
use Composer\Package\BasePackage;
use Composer\Package\PackageInterface;
use Yiisoft\Config\ConfigPaths;
use Yiisoft\Strings\WildcardPattern;
use function is_string;
use function str_replace;
/**
* @internal
* @psalm-import-type PackageConfigurationType from RootConfiguration
* @psalm-import-type EnvironmentsConfigurationType from RootConfiguration
*/
final class ProcessHelper
{
private Composer $composer;
private ConfigPaths $paths;
private RootConfiguration $rootConfiguration;
/**
* @psalm-var array<string, BasePackage>
*/
private array $packages;
/**
* @param Composer $composer The composer instance.
*/
public function __construct(Composer $composer)
{
/** @psalm-suppress UnresolvableInclude, MixedOperand */
require_once $composer->getConfig()->get('vendor-dir') . '/autoload.php';
$this->rootConfiguration = RootConfiguration::fromComposerInstance($composer);
$this->composer = $composer;
$this->paths = new ConfigPaths(
$this->rootConfiguration->path(),
$this->rootConfiguration->options()->sourceDirectory(),
);
$this->packages = (new PackagesListBuilder(
$this->composer,
$this->rootConfiguration->options()->packageTypes()
))->build();
}
/**
* Returns all vendor packages.
*
* @psalm-return array<string, BasePackage>
*/
public function getPackages(): array
{
return $this->packages;
}
/**
* Returns vendor packages without packages from the vendor override sublayer.
*
* @psalm-return array<string, BasePackage>
*/
public function getVendorPackages(): array
{
$vendorPackages = [];
foreach ($this->packages as $name => $package) {
if (!$this->isVendorOverridePackage($name)) {
$vendorPackages[$name] = $package;
}
}
return $vendorPackages;
}
/**
* Returns vendor packages only from the vendor override sublayer.
*
* @psalm-return array<string, BasePackage>
*/
public function getVendorOverridePackages(): array
{
$vendorOverridePackages = [];
foreach ($this->packages as $name => $package) {
if ($this->isVendorOverridePackage($name)) {
$vendorOverridePackages[$name] = $package;
}
}
return $vendorOverridePackages;
}
/**
* Returns the absolute path to the package file.
*
* @param PackageInterface $package The package instance.
* @param Options $options The options instance.
* @param string $filename The package configuration filename.
*
* @return string The absolute path to the package file.
*/
public function getAbsolutePackageFilePath(PackageInterface $package, Options $options, string $filename): string
{
return "{$this->getPackageSourceDirectoryPath($package, $options)}/$filename";
}
/**
* Returns the relative path to the package file including the source directory {@see Options::sourceDirectory()}.
*
* @param PackageInterface $package The package instance.
* @param string $filePath The absolute path to the package file.
*
* @return string The relative path to the package file including the source directory.
*/
public function getRelativePackageFilePath(PackageInterface $package, string $filePath): string
{
return str_replace("{$this->getPackageRootDirectoryPath($package)}/", '', $filePath);
}
/**
* Returns the relative path to the package file including the package name.
*
* @param PackageInterface $package The package instance.
* @param string $filePath The absolute path to the package file.
*
* @return string The relative path to the package file including the package name.
*/
public function getRelativePackageFilePathWithPackageName(PackageInterface $package, string $filePath): string
{
return "{$package->getPrettyName()}/{$this->getRelativePackageFilePath($package, $filePath)}";
}
/**
* Returns the package filename excluding the source directory {@see Options::sourceDirectory()}.
*
* @param PackageInterface $package The package instance.
* @param Options $options The options instance.
* @param string $filePath The absolute path to the package file.
*
* @return string The package filename excluding the source directory.
*/
public function getPackageFilename(PackageInterface $package, Options $options, string $filePath): string
{
return str_replace("{$this->getPackageSourceDirectoryPath($package, $options)}/", '', $filePath);
}
/**
* Returns the package configuration.
*
* @param PackageInterface $package The package instance.
*
* @return array The package configuration.
*
* @psalm-return PackageConfigurationType
* @psalm-suppress MixedReturnTypeCoercion
*/
public function getPackageConfig(PackageInterface $package): array
{
return (array) ($package->getExtra()['config-plugin'] ?? []);
}
/**
* Returns the root package configuration.
*
* @return array The root package configuration.
*
* @psalm-return PackageConfigurationType
*/
public function getRootPackageConfig(): array
{
return $this->rootConfiguration->packageConfiguration();
}
/**
* Returns the environment configuration.
*
* @return array The environment configuration.
*
* @psalm-return EnvironmentsConfigurationType
*/
public function getEnvironmentConfig(): array
{
return $this->rootConfiguration->environmentsConfiguration();
}
/**
* Returns the config paths instance.
*
* @return ConfigPaths The config paths instance.
*/
public function getPaths(): ConfigPaths
{
return $this->paths;
}
/**
* Checks whether to build a merge plan.
*
* @return bool Whether to build a merge plan.
*/
public function shouldBuildMergePlan(): bool
{
return $this->rootConfiguration->options()->buildMergePlan();
}
/**
* @return string The merge plan filepath.
*/
public function getMergePlanFile(): string
{
return $this->rootConfiguration->options()->mergePlanFile();
}
/**
* Returns the absolute path to the package source directory {@see Options::sourceDirectory()}.
*
* @param PackageInterface $package The package instance.
* @param Options $options The options instance.
*
* @return string The absolute path to the package config directory.
*/
private function getPackageSourceDirectoryPath(PackageInterface $package, Options $options): string
{
$packageConfigDirectory = $options->sourceDirectory() === '' ? '' : "/{$options->sourceDirectory()}";
return $this->getPackageRootDirectoryPath($package) . $packageConfigDirectory;
}
/**
* Returns the absolute path to the package root directory.
*
* @param PackageInterface $package The package instance.
*
* @return string The absolute path to the package root directory.
*/
private function getPackageRootDirectoryPath(PackageInterface $package): string
{
/**
* @var string Because we use library and composer-plugins only ({@see PackagesListBuilder::getAllPackages()}),
* which always has installation path.
*/
return $this->composer
->getInstallationManager()
->getInstallPath($package);
}
/**
* Checks whether the package is in the vendor override sublayer.
*
* @param string $package The package name.
*
* @return bool Whether the package is in the vendor override sublayer.
*/
private function isVendorOverridePackage(string $package): bool
{
foreach ($this->rootConfiguration->options()->vendorOverrideLayerPackages() as $pattern) {
if (!is_string($pattern)) {
continue;
}
if ($package === $pattern || (new WildcardPattern($pattern))->match($package)) {
return true;
}
}
return false;
}
}