generated from yiisoft/package-template
/
PackageFilesProcess.php
118 lines (97 loc) · 3.45 KB
/
PackageFilesProcess.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
<?php
declare(strict_types=1);
namespace Yiisoft\Config\Composer;
use Composer\Composer;
use Yiisoft\Config\ConfigPaths;
use function glob;
use function in_array;
use function is_file;
use function substr;
/**
* @internal
*/
final class PackageFilesProcess
{
private ProcessHelper $helper;
/**
* @var PackageFile[]
*/
private array $packageFiles = [];
/**
* @param Composer $composer The composer instance.
* @param string[] $packageNames The pretty package names to build.
* If the array is empty, the files of all packages will be build.
*/
public function __construct(Composer $composer, array $packageNames = [])
{
$this->helper = new ProcessHelper($composer);
$this->process($packageNames);
}
/**
* Returns the processed package configuration files.
*
* @return PackageFile[] The processed package configuration files.
*/
public function files(): array
{
return $this->packageFiles;
}
/**
* Returns the config paths instance.
*
* @return ConfigPaths The config paths instance.
*/
public function paths(): ConfigPaths
{
return $this->helper->getPaths();
}
/**
* @param string[] $packageNames The pretty package names to build.
* If the array is empty, the files of all packages will be build.
*/
private function process(array $packageNames): void
{
foreach ($this->helper->getPackages() as $package) {
$options = new Options($package->getExtra());
foreach ($this->helper->getPackageConfig($package) as $files) {
$files = (array) $files;
foreach ($files as $file) {
$isOptional = false;
if (Options::isOptional($file)) {
$isOptional = true;
$file = substr($file, 1);
}
if (
Options::isVariable($file) ||
(!empty($packageNames) && !in_array($package->getPrettyName(), $packageNames, true))
) {
continue;
}
$absoluteFilePath = $this->helper->getAbsolutePackageFilePath($package, $options, $file);
if (Options::containsWildcard($file)) {
$matches = glob($absoluteFilePath);
if (empty($matches)) {
continue;
}
foreach ($matches as $match) {
$this->packageFiles[] = new PackageFile(
$this->helper->getPackageFilename($package, $options, $match),
$this->helper->getRelativePackageFilePath($package, $match),
$match,
);
}
continue;
}
if ($isOptional && !is_file($absoluteFilePath)) {
continue;
}
$this->packageFiles[] = new PackageFile(
$this->helper->getPackageFilename($package, $options, $absoluteFilePath),
$this->helper->getRelativePackageFilePath($package, $absoluteFilePath),
$absoluteFilePath,
);
}
}
}
}
}