Passed
Push — master ( dbe497...f8489a )
by Jim
01:40
created

Builder::pathHasDefault()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
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 Closure;
14
use hiqdev\composer\config\configs\ConfigFactory;
15
16
/**
17
 * Builder assembles config files.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 *
21
 * @since php5.5
22
 */
23
class Builder
24
{
25
    /**
26
     * @var string path to output assembled configs
27
     */
28
    protected $outputDir;
29
30
    /**
31
     * @var array collected variables
32
     */
33
    protected $vars = [];
34
35
    /**
36
     * @var array configurations
37
     */
38
    protected $configs = [];
39
40
    const OUTPUT_DIR_SUFFIX = '-output';
41
42 2
    public function __construct($outputDir = null)
43
    {
44 2
        $this->setOutputDir($outputDir);
45 2
    }
46
47 2
    public function setOutputDir($outputDir)
48
    {
49 2
        $this->outputDir = $outputDir ?: static::findOutputDir();
50 2
    }
51
52
    public function getOutputDir()
53
    {
54
        return $this->outputDir;
55
    }
56
57
    public static function rebuild($outputDir = null)
58
    {
59
        $builder = new self($outputDir);
60
        $files = $builder->getConfig('__files')->load();
61
        $builder->buildUserConfigs($files->getValues());
62
    }
63
64
    public function rebuildUserConfigs()
65
    {
66
        $this->getConfig('__files')->load();
67
    }
68
69
    /**
70
     * Returns default output dir.
71
     * @param string $vendor path to vendor dir
72
     * @return string
73
     */
74 2
    public static function findOutputDir($vendor = null)
75
    {
76 2
        if ($vendor) {
77
            $dir = $vendor . '/hiqdev/' . basename(dirname(__DIR__));
78
        } else {
79 2
            $dir = \dirname(__DIR__);
80
        }
81
82 2
        return $dir . static::OUTPUT_DIR_SUFFIX;
83
    }
84
85
    /**
86
     * Returns full path to assembled config file.
87
     * @param string $filename name of config
88
     * @param string $vendor path to vendor dir
89
     * @return string absolute path
90
     */
91
    public static function path($filename, $vendor = null)
92
    {
93
        return static::findOutputDir($vendor) . DIRECTORY_SEPARATOR . $filename . '.php';
94
    }
95
96
    /**
97
     * Returns full path to assembled config file, if not exists, will return the default value.
98
     * @param string $filename
99
     * @param string $default
100
     * @return mixed|string
101
     */
102
    public static function pathHasDefault($filename, $default = '')
103
    {
104
        if (!file_exists($path = self::path($filename))) {
105
            return $default instanceof Closure ? $default() : $default;
0 ignored issues
show
introduced by
$default is never a sub-type of Closure.
Loading history...
106
        }
107
108
        return $path;
109
    }
110
111
    /**
112
     * Builds all (user and system) configs by given files list.
113
     * @param null|array $files files to process: config name => list of files
114
     */
115
    public function buildAllConfigs(array $files)
116
    {
117
        $this->buildUserConfigs($files);
118
        $this->buildSystemConfigs($files);
119
    }
120
121
    /**
122
     * Builds configs by given files list.
123
     * @param null|array $files files to process: config name => list of files
124
     */
125
    public function buildUserConfigs(array $files)
126
    {
127
        $resolver = new Resolver($files);
128
        $files = $resolver->get();
129
        foreach ($files as $name => $paths) {
130
            $this->getConfig($name)->load($paths)->build()->write();
131
        }
132
133
        return $files;
134
    }
135
136
    public function buildSystemConfigs(array $files)
137
    {
138
        $this->getConfig('__files')->setValues($files);
139
        foreach (['__rebuild', '__files', 'aliases', 'packages'] as $name) {
140
            $this->getConfig($name)->build()->write();
141
        }
142
    }
143
144
    public function getOutputPath($name)
145
    {
146
        return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php';
147
    }
148
149
    protected function createConfig($name)
150
    {
151
        $config = ConfigFactory::create($this, $name);
152
        $this->configs[$name] = $config;
153
154
        return $config;
155
    }
156
157
    public function getConfig($name)
158
    {
159
        if (!isset($this->configs[$name])) {
160
            $this->configs[$name] = $this->createConfig($name);
161
        }
162
163
        return $this->configs[$name];
164
    }
165
166
    public function getVars()
167
    {
168
        $vars = [];
169
        foreach ($this->configs as $name => $config) {
170
            $vars[$name] = $config->getValues();
171
        }
172
173
        return $vars;
174
    }
175
176
    public function mergeAliases(array $aliases)
177
    {
178
        $this->getConfig('aliases')->mergeValues($aliases);
179
    }
180
181
    public function setPackage($name, array $data)
182
    {
183
        $this->getConfig('packages')->setValue($name, $data);
184
    }
185
}
186