Passed
Push — master ( a9da14...7de28b )
by Pol
02:46
created

Config   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 80.85%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 130
ccs 38
cts 47
cp 0.8085
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocalConfigurationFilepath() 0 15 4
A resolveImports() 0 4 1
B findFilesToIncludeInConfiguration() 0 75 8
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
        // Get the vendor-bin property from the composer.json.
23 1
        $composer = Taskman::createJsonConfiguration([$cwd . '/composer.json']);
24
25 1
        if (null === $composer) {
26
            return [];
27
        }
28
29 1
        $vendorDir = $composer->get('vendor-dir') ?? $cwd . '/vendor';
30
31
        // Keep a reference of the default filename that we need to load from
32
        // each packages.
33
        $filesToLoad = [
34 1
            'taskman.yml.dist',
35
            'taskman.yml',
36
        ];
37
38
        // Load default paths.
39
        $filesystemPaths = [
40 1
            __DIR__ . '/../config/default.yml',
41
            __DIR__ . '/../default.yml',
42 1
            static::getLocalConfigurationFilepath(),
43
        ];
44
45
        // Check if composer.lock exists.
46 1
        $composerLockPath = \realpath($cwd . '/composer.lock');
47
48 1
        if (false === $composerLockPath) {
49
            return [];
50
        }
51
52 1
        $composerLockConfig = Taskman::createJsonConfiguration([$composerLockPath]);
53
54
        // Get the dependencies packages directories.
55 1
        $packageDirectories = \array_filter(
56 1
            \array_map(
57
                static function ($package) use ($vendorDir) {
58 1
                    return \realpath($vendorDir . '/' . $package['name']);
59 1
                },
60 1
                \array_merge(
61 1
                    $composerLockConfig->get('packages', []),
62 1
                    $composerLockConfig->get('packages-dev', [])
63
                )
64
            )
65
        );
66
67 1
        $packageDirectories[] = $cwd;
68
69
        // Loop over each composer.json, deduct the package directory and probe for files to include.
70 1
        foreach ($packageDirectories as $packageDirectory) {
71 1
            foreach ($filesToLoad as $taskmanFile) {
72 1
                foreach ([$packageDirectory, $cwd] as $directory) {
73 1
                    $candidateFile = $directory . '/' . $taskmanFile;
74 1
                    $filesystemPaths[$candidateFile] = $candidateFile;
75
                }
76
            }
77
78 1
            $composer = Taskman::createJsonConfiguration([$packageDirectory . '/composer.json']);
79 1
            $extraFiles = $composer->get('extra.taskman.files', []);
80
81 1
            if (empty($extraFiles)) {
82 1
                continue;
83
            }
84
85
            foreach ($extraFiles as $commandFile) {
86
                $filesystemPaths[$commandFile] = $commandFile;
87
                $commandFile = $packageDirectory . '/' . $commandFile;
88
                $filesystemPaths[$commandFile] = $commandFile;
89
            }
90
        }
91
92 1
        return \array_filter(
93 1
            static::resolveImports(...\array_values($filesystemPaths)),
94 1
            'file_exists'
95
        );
96
    }
97
98
    /**
99
     * Get the local configuration filepath.
100
     *
101
     * @param string $configuration_file
102
     *   The default filepath.
103
     *
104
     * @return null|string
105
     *   The local configuration file path, or null if it doesn't exist.
106
     */
107 1
    public static function getLocalConfigurationFilepath($configuration_file = 'phptaskman/taskman.yml')
108
    {
109 1
        if ($config = \getenv('PHPTASKMAN_CONFIG')) {
110
            return $config;
111
        }
112
113 1
        if ($config = \getenv('XDG_CONFIG_HOME')) {
114
            return $config . '/' . $configuration_file;
115
        }
116
117 1
        if ($home = \getenv('HOME')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $home is dead and can be removed.
Loading history...
118 1
            return \getenv('HOME') . '/.config/' . $configuration_file;
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * Resolve YAML configurations files containing imports.
126
     *
127
     * Handles circular dependencies by ignoring them.
128
     *
129
     * @param string[] ...$filepaths
130
     *   A list of YML filepath to parse.
131
     *
132
     * @return string[]
133
     *   The list of all the YAML files to include.
134
     */
135 1
    public static function resolveImports(...$filepaths)
136
    {
137 1
        return (new YamlRecursivePathsFinder($filepaths))
138 1
            ->getAllPaths();
139
    }
140
}
141