Passed
Pull Request — master (#54)
by Alexander
12:39
created

Builder::getBaseDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 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): void
68
    {
69
        $builder = new self(new ConfigFactory(), $baseDir);
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
79
     * @return string
80
     */
81
    private static function findOutputDir(string $baseDir): string
82
    {
83
        $path = $baseDir . DIRECTORY_SEPARATOR . 'composer.json';
84
        $data = @json_decode(file_get_contents($path), true);
85
        $dir = $data['extra'][Package::EXTRA_OUTPUT_DIR_OPTION_NAME] ?? null;
86
87
        return $dir ? static::buildAbsPath($baseDir, $dir) : static::defaultOutputDir($baseDir);
88
    }
89
90
    /**
91
     * Returns default output dir.
92
     *
93
     * @param string $baseDir path to base directory
94
     * @return string
95
     */
96
    private static function defaultOutputDir(string $baseDir = null): string
97
    {
98
        if ($baseDir) {
99
            $dir = $baseDir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'yiisoft' . DIRECTORY_SEPARATOR . basename(dirname(__DIR__));
100
        } else {
101
            $dir = dirname(__DIR__);
102
        }
103
104
        return $dir . static::OUTPUT_DIR_SUFFIX;
105
    }
106
107
    /**
108
     * Returns full path to assembled config file.
109
     *
110
     * @param string $filename name of config
111
     * @param string $baseDir path to base dir
112
     * @return string absolute path
113
     */
114
    public static function path(string $filename, string $baseDir): string
115
    {
116
        return static::buildAbsPath(static::findOutputDir($baseDir), $filename . '.php');
117
    }
118
119
    private static function buildAbsPath(string $dir, string $file): string
120
    {
121
        return self::isAbsolutePath($file) ? $file : $dir . DIRECTORY_SEPARATOR . $file;
122
    }
123
124
    private static function isAbsolutePath(string $path): string
125
    {
126
        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...
127
    }
128
129
    /**
130
     * Builds all (user and system) configs by given files list.
131
     *
132
     * @param null|array $files files to process: config name => list of files
133
     */
134
    public function buildAllConfigs(array $files): void
135
    {
136
        $this->buildUserConfigs($files);
137
        $this->buildSystemConfigs($files);
138
    }
139
140
    /**
141
     * Builds configs by given files list.
142
     *
143
     * @param null|array $files files to process: config name => list of files
144
     * @return array
145
     */
146
    private function buildUserConfigs(array $files): array
147
    {
148
        $resolver = new Resolver($files);
149
        $files = $resolver->get();
150
        foreach ($files as $name => $paths) {
151
            $this->getConfig($name)->load($paths)->build()->write();
152
        }
153
154
        return $files;
155
    }
156
157
    private function buildSystemConfigs(array $files): void
158
    {
159
        $this->getConfig('__files')->setValues($files);
160
        foreach (['__files', 'aliases', 'packages'] as $name) {
161
            $this->getConfig($name)->build()->write();
162
        }
163
    }
164
165
    public function getOutputPath(string $name): string
166
    {
167
        return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php';
168
    }
169
170
    public function getConfig(string $name)
171
    {
172
        if (!array_key_exists($name, $this->configs)) {
173
            $this->configs[$name] = $this->configFactory->create($this, $name);
174
        }
175
176
        return $this->configs[$name];
177
    }
178
179
    public function getVars(): array
180
    {
181
        $vars = [];
182
        foreach ($this->configs as $name => $config) {
183
            $vars[$name] = $config->getValues();
184
        }
185
186
        return $vars;
187
    }
188
189
    public function mergeAliases(array $aliases): void
190
    {
191
        $this->getConfig('aliases')->mergeValues($aliases);
192
    }
193
194
    public function setPackage(string $name, array $data): void
195
    {
196
        $this->getConfig('packages')->setValue($name, $data);
197
    }
198
199
    /**
200
     * @return string a full path to the project root
201
     */
202
    public function getBaseDir(): string
203
    {
204
        return $this->baseDir;
205
    }
206
}
207