1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace PhpTaskman\Core\Config; |
||
6 | |||
7 | use PhpTaskman\Core\Taskman; |
||
8 | |||
9 | final class Config |
||
10 | { |
||
11 | /** |
||
12 | * Find the files to include in the configuration. |
||
13 | * |
||
14 | * @param string $cwd |
||
15 | * The current working directory. |
||
16 | * |
||
17 | * @return string[] |
||
18 | * The list of all the YAML files to include. |
||
19 | */ |
||
20 | 1 | public static function findFilesToIncludeInConfiguration($cwd) |
|
21 | { |
||
22 | // Check if composer.json exists. |
||
23 | 1 | $composerPath = realpath($cwd . '/composer.json'); |
|
24 | |||
25 | 1 | if (false === $composerPath) { |
|
26 | return []; |
||
27 | } |
||
28 | |||
29 | // Get the vendor-bin property from the composer.json. |
||
30 | 1 | $composerConfig = Taskman::createJsonConfiguration([$composerPath]); |
|
31 | 1 | $vendorDir = $composerConfig->get('vendor-dir', $cwd . '/vendor'); |
|
32 | |||
33 | // Keep a reference of the default filename that we need to load from |
||
34 | // each packages. |
||
35 | $configFilesToLoad = [ |
||
36 | 1 | 'defaultConfig' => 'config/default.yml', |
|
37 | 'defaultCommand' => 'default.yml', |
||
38 | ]; |
||
39 | |||
40 | // Keep a reference of the default filename that we need to load from |
||
41 | // each packages. |
||
42 | $commandsFilesToLoad = [ |
||
43 | 1 | 'default' => 'taskman.yml.dist', |
|
44 | 'defaultOverride' => 'taskman.yml', |
||
45 | ]; |
||
46 | |||
47 | // Check if composer.lock exists. |
||
48 | 1 | $composerLockPath = realpath($cwd . '/composer.lock'); |
|
49 | |||
50 | 1 | if (false === $composerLockPath) { |
|
51 | return []; |
||
52 | } |
||
53 | |||
54 | 1 | $composerLockConfig = Taskman::createJsonConfiguration( |
|
55 | 1 | [$composerLockPath] |
|
56 | ); |
||
57 | |||
58 | // Get the dependencies packages directories. |
||
59 | 1 | $packageDirectories = array_filter( |
|
60 | 1 | array_map( |
|
61 | static function ($package) use ($vendorDir) { |
||
62 | 1 | return realpath($vendorDir . '/' . $package['name']); |
|
63 | 1 | }, |
|
64 | 1 | array_merge( |
|
65 | 1 | $composerLockConfig->get('packages', []), |
|
66 | 1 | $composerLockConfig->get('packages-dev', []) |
|
67 | ) |
||
68 | ) |
||
69 | ); |
||
70 | |||
71 | 1 | $packageDirectories[] = $cwd; |
|
72 | |||
73 | 1 | $configs = []; |
|
74 | 1 | $commands = []; |
|
75 | |||
76 | // Loop over each composer.json, deduct the package directory and probe for files to include. |
||
77 | 1 | foreach ($packageDirectories as $packageDirectory) { |
|
78 | 1 | foreach ($configFilesToLoad as $taskmanFile) { |
|
79 | 1 | $candidateFile = $packageDirectory . '/' . $taskmanFile; |
|
80 | 1 | $configs[] = $candidateFile; |
|
81 | } |
||
82 | |||
83 | 1 | $composerConfig = Taskman::createJsonConfiguration( |
|
84 | 1 | [$packageDirectory . '/composer.json'] |
|
85 | ); |
||
86 | |||
87 | 1 | foreach ($composerConfig->get('extra.taskman.files', []) as $commandFile) { |
|
88 | $commandFile = $packageDirectory . '/' . $commandFile; |
||
89 | $configs[] = $commandFile; |
||
90 | } |
||
91 | |||
92 | 1 | foreach ($commandsFilesToLoad as $taskmanFile) { |
|
93 | 1 | $candidateFile = $packageDirectory . '/' . $taskmanFile; |
|
94 | 1 | $commands[] = $candidateFile; |
|
95 | } |
||
96 | } |
||
97 | |||
98 | $localConfigFiles = [ |
||
99 | 1 | __DIR__ . '/../../config/default.yml', |
|
100 | __DIR__ . '/../../default.yml', |
||
101 | 1 | static::getLocalConfigurationFilepath(), |
|
102 | ]; |
||
103 | |||
104 | 1 | $configs = array_filter( |
|
105 | 1 | array_filter( |
|
106 | 1 | array_merge( |
|
107 | 1 | $configs, |
|
108 | 1 | $localConfigFiles, |
|
109 | 1 | $commands |
|
110 | ), |
||
111 | 1 | 'file_exists' |
|
112 | ) |
||
113 | ); |
||
114 | |||
115 | 1 | return array_values($configs); |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get the local configuration filepath. |
||
120 | * |
||
121 | * @param string $configuration_file |
||
122 | * The default filepath. |
||
123 | * |
||
124 | * @return null|string |
||
125 | * The local configuration file path, or null if it doesn't exist. |
||
126 | */ |
||
127 | 1 | public static function getLocalConfigurationFilepath($configuration_file = 'phptaskman/taskman.yml') |
|
128 | { |
||
129 | 1 | if ($config = getenv('PHPTASKMAN_CONFIG')) { |
|
130 | return $config; |
||
131 | } |
||
132 | |||
133 | 1 | if ($config = getenv('XDG_CONFIG_HOME')) { |
|
134 | return $config . '/' . $configuration_file; |
||
135 | } |
||
136 | |||
137 | 1 | if ($home = getenv('HOME')) { |
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
138 | 1 | return getenv('HOME') . '/.config/' . $configuration_file; |
|
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Resolve YAML configurations files containing imports. |
||
144 | * |
||
145 | * Handles circular dependencies by ignoring them. |
||
146 | * |
||
147 | * @param string[] ...$filepaths |
||
148 | * A list of YML filepath to parse. |
||
149 | * |
||
150 | * @return string[] |
||
151 | * The list of all the YAML files to include. |
||
152 | */ |
||
153 | public static function resolveImports(...$filepaths) |
||
154 | { |
||
155 | return (new YamlRecursivePathsFinder($filepaths)) |
||
156 | ->getAllPaths(); |
||
157 | } |
||
158 | } |
||
159 |