Passed
Push — master ( a86af8...342d16 )
by Alexander
21:19 queued 06:12
created

Builder::defaultOutputDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
     * @param ConfigFactory $configFactory
40
     * @param string $baseDir path to the Composer project root
41
     */
42
    public function __construct(ConfigFactory $configFactory, string $baseDir)
43
    {
44
        $this->configFactory = $configFactory;
45
        $this->baseDir = $baseDir;
46
        $this->outputDir = self::findOutputDir($baseDir);
47
    }
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
    }
66
67
    public static function rebuild(string $baseDir): void
68
    {
69
        $builder = new self(new ConfigFactory(), $baseDir);
70
        $files = $builder->getConfig('__files')->load();
71
72
        $builder->buildUserConfigs($files->getValues());
73
    }
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
        $baseDir = dirname(__DIR__, 4);
97
        if (file_exists($baseDir . DIRECTORY_SEPARATOR . 'composer.json')) {
98
            return $baseDir;
99
        }
100
101
        $baseDir = getcwd();
102
        if (file_exists($baseDir . DIRECTORY_SEPARATOR . 'composer.json')) {
103
            return $baseDir;
104
        }
105
106
        throw new \RuntimeException('Cannot find directory that contains composer.json');
107
    }
108
109
    /**
110
     * Returns default output dir.
111
     *
112
     * @param string $baseDir path to base directory
113
     * @return string
114
     */
115
    private static function defaultOutputDir(string $baseDir = null): string
116
    {
117
        if ($baseDir) {
118
            $dir = $baseDir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'yiisoft' . DIRECTORY_SEPARATOR . basename(dirname(__DIR__));
119
        } else {
120
            $dir = dirname(__DIR__);
121
        }
122
123
        return $dir . static::OUTPUT_DIR_SUFFIX;
124
    }
125
126
    /**
127
     * Returns full path to assembled config file.
128
     *
129
     * @param string $filename name of config
130
     * @param string $baseDir path to base dir
131
     * @return string absolute path
132
     * @throws JsonException
133
     */
134
    public static function path(string $filename, string $baseDir = null): string
135
    {
136
        return static::buildAbsPath(static::findOutputDir($baseDir), $filename . '.php');
137
    }
138
139
    private static function buildAbsPath(string $dir, string $file): string
140
    {
141
        return self::isAbsolutePath($file) ? $file : $dir . DIRECTORY_SEPARATOR . $file;
142
    }
143
144
    private static function isAbsolutePath(string $path): string
145
    {
146
        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...
147
    }
148
149
    /**
150
     * Builds all (user and system) configs by given files list.
151
     *
152
     * @param null|array $files files to process: config name => list of files
153
     */
154
    public function buildAllConfigs(array $files): void
155
    {
156
        $this->buildUserConfigs($files);
157
        $this->buildSystemConfigs($files);
158
    }
159
160
    /**
161
     * Builds configs by given files list.
162
     *
163
     * @param null|array $files files to process: config name => list of files
164
     * @return array
165
     */
166
    private function buildUserConfigs(array $files): array
167
    {
168
        $resolver = new Resolver($files);
169
        $files = $resolver->get();
170
        foreach ($files as $name => $paths) {
171
            $this->getConfig($name)->load($paths)->build()->write();
172
        }
173
174
        return $files;
175
    }
176
177
    private function buildSystemConfigs(array $files): void
178
    {
179
        $this->getConfig('__files')->setValues($files);
180
        foreach (['__files', 'aliases', 'packages'] as $name) {
181
            $this->getConfig($name)->build()->write();
182
        }
183
    }
184
185
    public function getOutputPath(string $name): string
186
    {
187
        return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php';
188
    }
189
190
    public function getConfig(string $name)
191
    {
192
        if (!array_key_exists($name, $this->configs)) {
193
            $this->configs[$name] = $this->configFactory->create($this, $name);
194
        }
195
196
        return $this->configs[$name];
197
    }
198
199
    public function getVars(): array
200
    {
201
        $vars = [];
202
        foreach ($this->configs as $name => $config) {
203
            $vars[$name] = $config->getValues();
204
        }
205
206
        return $vars;
207
    }
208
209
    public function mergeAliases(array $aliases): void
210
    {
211
        $this->getConfig('aliases')->mergeValues($aliases);
212
    }
213
214
    public function setPackage(string $name, array $data): void
215
    {
216
        $this->getConfig('packages')->setValue($name, $data);
217
    }
218
219
    /**
220
     * @return string a full path to the project root
221
     */
222
    public function getBaseDir(): string
223
    {
224
        return $this->baseDir;
225
    }
226
}
227