Passed
Push — master ( f48fb5...dbe497 )
by Jim
01:37
created

Builder   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 17.24%

Importance

Changes 0
Metric Value
wmc 22
eloc 41
dl 0
loc 146
ccs 10
cts 58
cp 0.1724
rs 10
c 0
b 0
f 0

16 Methods

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