Completed
Push — master ( e0d1a0...4d9f21 )
by Pol
03:07 queued 22s
created

Config   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 117
ccs 33
cts 42
cp 0.7856
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLocalConfigurationFilepath() 0 15 4
B findFilesToIncludeInConfiguration() 0 62 6
A resolveImports() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Config;
6
7
use PhpTaskman\Core\Taskman;
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
final class Config
12
{
13
    /**
14
     * Find the files to include in the configuration.
15
     *
16
     * @param string $cwd
17
     *   The current working directory.
18
     *
19
     * @return string[]
20
     *   The list of all the YAML files to include.
21
     */
22 1
    public static function findFilesToIncludeInConfiguration($cwd)
23
    {
24
        // Get the vendor-bin property from the composer.json.
25 1
        $composer = Taskman::getComposerFromDirectory($cwd);
26 1
        $vendorDir = $composer->getConfig('vendor-dir') ?? $cwd . '/vendor';
27
28
        // Keep a reference of the default filename that we need to load from
29
        // each packages.
30
        $filesToLoad = [
31 1
            'taskman.yml.dist',
32
            'taskman.yml',
33
        ];
34
35
        // Load default paths.
36
        $filesystemPaths = [
37 1
            __DIR__ . '/../config/default.yml',
38
            __DIR__ . '/../default.yml',
39 1
            static::getLocalConfigurationFilepath(),
40
        ];
41
42
        // Find all the composer.json to parse.
43 1
        $finder = (new Finder())
44 1
            ->files()
45 1
            ->exclude('tests')
46 1
            ->in([\dirname(\realpath($vendorDir)), $cwd])
47 1
            ->name('composer.json');
48
49
        // Loop over each composer.json, deduct the package directory and probe for files to include.
50
        /** @var SplFileInfo $file */
51 1
        foreach ($finder as $file) {
52 1
            $composerPackageDirectory = \dirname($file->getPathname());
53
54 1
            foreach ($filesToLoad as $taskmanFile) {
55 1
                foreach ([$composerPackageDirectory, $cwd] as $directory) {
56 1
                    $candidateFile = $directory . '/' . $taskmanFile;
57 1
                    $filesystemPaths[$candidateFile] = $candidateFile;
58
                }
59
            }
60
61 1
            $composer = Taskman::getComposerFromDirectory(
62 1
                $composerPackageDirectory
63
            );
64
65 1
            $extra = $composer->getExtra();
66
67 1
            if (empty($extra)) {
68 1
                continue;
69
            }
70
71
            $extra += ['taskman' => []];
72
            $extra['taskman'] += ['files' => []];
73
74
            foreach ($extra['taskman']['files'] as $commandFile) {
75
                $filesystemPaths[$commandFile] = $commandFile;
76
                $commandFile = $composerPackageDirectory . '/' . $commandFile;
77
                $filesystemPaths[$commandFile] = $commandFile;
78
            }
79
        }
80
81 1
        return \array_filter(
82 1
            static::resolveImports(...\array_values($filesystemPaths)),
83 1
            'file_exists'
84
        );
85
    }
86
87
    /**
88
     * Get the local configuration filepath.
89
     *
90
     * @param string $configuration_file
91
     *   The default filepath.
92
     *
93
     * @return null|string
94
     *   The local configuration file path, or null if it doesn't exist.
95
     */
96 1
    public static function getLocalConfigurationFilepath($configuration_file = 'phptaskman/taskman.yml')
97
    {
98 1
        if ($config = \getenv('PHPTASKMAN_CONFIG')) {
99
            return $config;
100
        }
101
102 1
        if ($config = \getenv('XDG_CONFIG_HOME')) {
103
            return $config . '/' . $configuration_file;
104
        }
105
106 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...
107 1
            return \getenv('HOME') . '/.config/' . $configuration_file;
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * Resolve YAML configurations files containing imports.
115
     *
116
     * Handles circular dependencies by ignoring them.
117
     *
118
     * @param string[] ...$filepaths
119
     *   A list of YML filepath to parse.
120
     *
121
     * @return string[]
122
     *   The list of all the YAML files to include.
123
     */
124 1
    public static function resolveImports(...$filepaths)
125
    {
126 1
        return (new YamlRecursivePathsFinder($filepaths))
127 1
            ->getAllPaths();
128
    }
129
}
130