Passed
Pull Request — master (#55)
by Alexander
12:20
created

Builder   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Test Coverage

Coverage 17.54%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 33
eloc 62
c 4
b 0
f 0
dl 0
loc 215
rs 9.76
ccs 10
cts 57
cp 0.1754

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setOutputDir() 0 5 2
A __construct() 0 5 1
A createAlternative() 0 9 2
A buildUserConfigs() 0 9 2
A buildAbsPath() 0 3 2
A path() 0 3 1
A setPackage() 0 3 1
A getOutputPath() 0 3 1
A buildSystemConfigs() 0 5 2
A getConfig() 0 7 2
A buildAllConfigs() 0 4 1
A getBaseDir() 0 3 1
A mergeAliases() 0 3 1
A findOutputDir() 0 10 3
A defaultOutputDir() 0 9 2
A isAbsolutePath() 0 3 3
A rebuild() 0 6 1
A findBaseDir() 0 18 3
A getVars() 0 8 2
1
<?php
2
3
namespace Yiisoft\Composer\Config;
4
5
use JsonException;
6
use Yiisoft\Composer\Config\Config\Config;
7
use Yiisoft\Composer\Config\Config\ConfigFactory;
8
use Yiisoft\Composer\Config\Util\Resolver;
9
10
use function dirname;
11
12
/**
13
 * Builder assembles config files.
14
 */
15
class Builder
16
{
17
    private const OUTPUT_DIR_SUFFIX = '-output';
18
19
    /**
20
     * @var string path to the Composer project root
21
     */
22
    private string $baseDir;
23
24
    /**
25
     * @var string path to output assembled configs
26
     */
27
    private string $outputDir;
28
29
    /**
30
     * @var Config[] configurations
31
     */
32
    private array $configs = [];
33
34
    private ConfigFactory $configFactory;
35
36
    /**
37
     * Builder constructor.
38
     *
39 2
     * @param ConfigFactory $configFactory
40
     * @param string $baseDir path to the Composer project root
41 2
     */
42 2
    public function __construct(ConfigFactory $configFactory, string $baseDir)
43
    {
44 2
        $this->configFactory = $configFactory;
45
        $this->baseDir = $baseDir;
46 2
        $this->outputDir = self::findOutputDir($baseDir);
47 2
    }
48
49
    public function createAlternative($name): Builder
50
    {
51
        $alt = new static($this->configFactory, $this->baseDir);
52
        $alt->setOutputDir($this->outputDir . DIRECTORY_SEPARATOR . $name);
53
        foreach (['aliases', 'packages'] as $key) {
54
            $alt->configs[$key] = $this->getConfig($key)->clone($alt);
55
        }
56
57
        return $alt;
58
    }
59
60
    public function setOutputDir(?string $outputDir): void
61
    {
62
        $this->outputDir = $outputDir
63
            ? static::buildAbsPath($this->getBaseDir(), $outputDir)
64
            : static::findOutputDir($this->getBaseDir());
65 2
    }
66
67 2
    public static function rebuild(?string $baseDir = null): void
68
    {
69
        $builder = new self(new ConfigFactory(), $baseDir ?? self::findBaseDir());
70 2
        $files = $builder->getConfig('__files')->load();
71
72
        $builder->buildUserConfigs($files->getValues());
73 2
    }
74
75
    /**
76
     * Returns default output dir.
77
     *
78
     * @param string|null $baseDir path to the root Composer package. When `null`,
79
     * @return string
80
     * @throws JsonException
81
     */
82
    private static function findOutputDir(string $baseDir = null): string
83
    {
84
        if ($baseDir === null) {
85
            $baseDir = static::findBaseDir();
86
        }
87
        $path = $baseDir . DIRECTORY_SEPARATOR . 'composer.json';
88
        $data = @json_decode(file_get_contents($path), true);
89
        $dir = $data['extra'][Package::EXTRA_OUTPUT_DIR_OPTION_NAME] ?? null;
90
91
        return $dir ? static::buildAbsPath($baseDir, $dir) : static::defaultOutputDir($baseDir);
92
    }
93
94
    private static function findBaseDir(): string
95
    {
96
        $candidates = [
97
            // normal relative path
98
            dirname(__DIR__, 4),
99
            // console
100
            getcwd(),
101
            // symlinked web
102
            dirname(getcwd())
103
        ];
104
105
        foreach ($candidates as $baseDir) {
106
            if (file_exists($baseDir . DIRECTORY_SEPARATOR . 'composer.json')) {
107
                return $baseDir;
108
            }
109
        }
110
111
        throw new \RuntimeException('Cannot find directory that contains composer.json');
112
    }
113
114
    /**
115
     * Returns default output dir.
116
     *
117
     * @param string $baseDir path to base directory
118
     * @return string
119
     */
120
    private static function defaultOutputDir(string $baseDir = null): string
121
    {
122
        if ($baseDir) {
123
            $dir = $baseDir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'yiisoft' . DIRECTORY_SEPARATOR . basename(dirname(__DIR__));
124
        } else {
125
            $dir = dirname(__DIR__);
126
        }
127
128
        return $dir . static::OUTPUT_DIR_SUFFIX;
129
    }
130
131
    /**
132
     * Returns full path to assembled config file.
133
     *
134
     * @param string $filename name of config
135
     * @param string $baseDir path to base dir
136
     * @return string absolute path
137
     * @throws JsonException
138
     */
139
    public static function path(string $filename, string $baseDir = null): string
140
    {
141
        return static::buildAbsPath(static::findOutputDir($baseDir), $filename . '.php');
142
    }
143
144
    private static function buildAbsPath(string $dir, string $file): string
145
    {
146
        return self::isAbsolutePath($file) ? $file : $dir . DIRECTORY_SEPARATOR . $file;
147
    }
148
149
    private static function isAbsolutePath(string $path): string
150
    {
151
        return strpos($path, '/') === 0 || strpos($path, ':') === 1 || strpos($path, '\\\\') === 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return strpos($path, '/'...rpos($path, '\\') === 0 returns the type boolean which is incompatible with the type-hinted return string.
Loading history...
152
    }
153
154
    /**
155
     * Builds all (user and system) configs by given files list.
156
     *
157
     * @param null|array $files files to process: config name => list of files
158
     */
159
    public function buildAllConfigs(array $files): void
160
    {
161
        $this->buildUserConfigs($files);
162
        $this->buildSystemConfigs($files);
163
    }
164
165
    /**
166
     * Builds configs by given files list.
167
     *
168
     * @param null|array $files files to process: config name => list of files
169
     * @return array
170
     */
171
    private function buildUserConfigs(array $files): array
172
    {
173
        $resolver = new Resolver($files);
174
        $files = $resolver->get();
175
        foreach ($files as $name => $paths) {
176
            $this->getConfig($name)->load($paths)->build()->write();
177
        }
178
179
        return $files;
180
    }
181
182
    private function buildSystemConfigs(array $files): void
183
    {
184
        $this->getConfig('__files')->setValues($files);
185
        foreach (['__files', 'aliases', 'packages'] as $name) {
186
            $this->getConfig($name)->build()->write();
187
        }
188
    }
189
190
    public function getOutputPath(string $name): string
191
    {
192
        return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php';
193
    }
194
195
    public function getConfig(string $name)
196
    {
197
        if (!array_key_exists($name, $this->configs)) {
198
            $this->configs[$name] = $this->configFactory->create($this, $name);
199
        }
200
201
        return $this->configs[$name];
202
    }
203
204
    public function getVars(): array
205
    {
206
        $vars = [];
207
        foreach ($this->configs as $name => $config) {
208
            $vars[$name] = $config->getValues();
209
        }
210
211
        return $vars;
212
    }
213
214
    public function mergeAliases(array $aliases): void
215
    {
216
        $this->getConfig('aliases')->mergeValues($aliases);
217
    }
218
219
    public function setPackage(string $name, array $data): void
220
    {
221
        $this->getConfig('packages')->setValue($name, $data);
222
    }
223
224
    /**
225
     * @return string a full path to the project root
226
     */
227
    public function getBaseDir(): string
228
    {
229
        return $this->baseDir;
230
    }
231
}
232