Builder::buildSystemConfigs()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Composer plugin for config assembling
4
 *
5
 * @link      https://github.com/hiqdev/composer-config-plugin
6
 * @package   composer-config-plugin
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\composer\config;
12
13
use hiqdev\composer\config\configs\ConfigFactory;
14
use hiqdev\composer\config\utils\Resolver;
15
16
/**
17
 * Builder assembles config files.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class Builder
22
{
23
    /**
24
     * @var string path to output assembled configs
25
     */
26
    protected $outputDir;
27
28
    /**
29
     * @var array collected variables
30
     */
31
    protected $vars = [];
32
33
    /**
34
     * @var array configurations
35
     */
36
    protected $configs = [];
37
38
    const OUTPUT_DIR_SUFFIX = '-output';
39 2
40
    public function __construct($outputDir = null)
41 2
    {
42 2
        $this->setOutputDir($outputDir);
43
    }
44 2
45
    public function createAlternative($name): Builder
46 2
    {
47 2
        $dir = $this->outputDir . DIRECTORY_SEPARATOR . $name;
48
        $alt = new static($dir);
49
        foreach (['aliases', 'packages'] as $key) {
50
            $alt->configs[$key] = $this->getConfig($key)->clone($alt);
51
        }
52
53
        return $alt;
54
    }
55
56
    public function setOutputDir($outputDir)
57
    {
58
        $this->outputDir = $outputDir
59
            ? static::buildAbsPath($this->getBaseDir(), $outputDir)
60
            : static::findOutputDir();
61
    }
62
63
    public function getBaseDir(): string
64
    {
65 2
        return dirname(__DIR__, 4);
66
    }
67 2
68
    public function getOutputDir(): string
69
    {
70 2
        return $this->outputDir;
71
    }
72
73 2
    public static function rebuild($outputDir = null)
74
    {
75
        $builder = new self($outputDir);
76
        $files = $builder->getConfig('__files')->load();
77
        $builder->buildUserConfigs($files->getValues());
78
    }
79
80
    public function rebuildUserConfigs()
81
    {
82
        $this->getConfig('__files')->load();
83
    }
84
85
    /**
86
     * Returns default output dir.
87
     * @param string $baseDir path to project base dir
88
     * @return string
89
     */
90
    public static function findOutputDir(string $baseDir = null): string
91
    {
92
        $baseDir = $baseDir ?: static::findBaseDir();
93
        $path = $baseDir . DIRECTORY_SEPARATOR . 'composer.json';
94
        $data = @json_decode(file_get_contents($path), true);
95
        $dir = $data['extra'][Package::EXTRA_OUTPUT_DIR_OPTION_NAME] ?? null;
96
97
        return $dir ? static::buildAbsPath($baseDir, $dir) : static::defaultOutputDir($baseDir);
98
    }
99
100
    public static function findBaseDir(): string
101
    {
102
        return dirname(__DIR__, 4);
103
    }
104
105
    /**
106
     * Returns default output dir.
107
     * @param string $vendor path to vendor dir
108
     * @return string
109
     */
110
    public static function defaultOutputDir($baseDir = null): string
111
    {
112
        if ($baseDir) {
113
            $dir = $baseDir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'hiqdev' . DIRECTORY_SEPARATOR . basename(dirname(__DIR__));
114
        } else {
115
            $dir = \dirname(__DIR__);
116
        }
117
118
        return $dir . static::OUTPUT_DIR_SUFFIX;
119
    }
120
121
    /**
122
     * Returns full path to assembled config file.
123
     * @param string $filename name of config
124
     * @param string $baseDir path to base dir
125
     * @return string absolute path
126
     */
127
    public static function path($filename, $baseDir = null)
128
    {
129
        return static::buildAbsPath(static::findOutputDir($baseDir), $filename . '.php');
130
    }
131
132
    public static function buildAbsPath(string $dir, string $file): string
133
    {
134
        return strncmp($file, DIRECTORY_SEPARATOR, 1) === 0 ? $file : $dir . DIRECTORY_SEPARATOR . $file;
135
    }
136
137
    /**
138
     * Builds all (user and system) configs by given files list.
139
     * @param null|array $files files to process: config name => list of files
140
     */
141
    public function buildAllConfigs(array $files)
142
    {
143
        $this->buildUserConfigs($files);
144
        $this->buildSystemConfigs($files);
145
    }
146
147
    /**
148
     * Builds configs by given files list.
149
     * @param null|array $files files to process: config name => list of files
150
     */
151
    public function buildUserConfigs(array $files): array
152
    {
153
        $resolver = new Resolver($files);
154
        $files = $resolver->get();
155
        foreach ($files as $name => $paths) {
156
            $this->getConfig($name)->load($paths)->build()->write();
157
        }
158
159
        return $files;
160
    }
161
162
    public function buildSystemConfigs(array $files)
163
    {
164
        $this->getConfig('__files')->setValues($files);
165
        foreach (['__rebuild', '__files', 'aliases', 'packages'] as $name) {
166
            $this->getConfig($name)->build()->write();
167
        }
168
    }
169
170
    public function getOutputPath($name)
171
    {
172
        return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php';
173
    }
174
175
    protected function createConfig($name)
176
    {
177
        $config = ConfigFactory::create($this, $name);
178
        $this->configs[$name] = $config;
179
180
        return $config;
181
    }
182
183
    public function getConfig(string $name)
184
    {
185
        if (!isset($this->configs[$name])) {
186
            $this->configs[$name] = $this->createConfig($name);
187
        }
188
189
        return $this->configs[$name];
190
    }
191
192
    public function getVar($name, $key)
193
    {
194
        $config = $this->configs[$name] ?? null;
195
        if (empty($config)) {
196
            return null;
197
        }
198
199
        return $config->getValues()[$key] ?? null;
200
    }
201
202
    public function getVars()
203
    {
204
        $vars = [];
205
        foreach ($this->configs as $name => $config) {
206
            $vars[$name] = $config->getValues();
207
        }
208
209
        return $vars;
210
    }
211
212
    public function mergeAliases(array $aliases)
213
    {
214
        $this->getConfig('aliases')->mergeValues($aliases);
215
    }
216
217
    public function setPackage(string $name, array $data)
218
    {
219
        $this->getConfig('packages')->setValue($name, $data);
220
    }
221
}
222