Completed
Push — master ( ab86c3...b85299 )
by Andrii
01:45
created

Builder::loadFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * Composer plugin for config assembling
5
 *
6
 * @link      https://github.com/hiqdev/composer-config-plugin
7
 * @package   composer-config-plugin
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\composer\config;
13
14
use Composer\IO\IOInterface;
15
16
/**
17
 * Builder assembles config files.
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class Builder
22
{
23
    /**
24
     * @var string path to output assembled configs
25
     */
26
    protected $outputDir;
27
28
    /**
29
     * @var array files to process: config name => list of files
30
     */
31
    protected $files = [];
32
33
    /**
34
     * @var array collected variables
35
     */
36
    protected $vars = [];
37
38
    const BASE_DIR_SAMPLE = '<base-dir>';
39
    const FILES_FILENAME  = '__files';
40
41
    public function __construct($outputDir, array $files = [])
42
    {
43
        $this->files = $files;
44
        $this->outputDir = $outputDir;
45
    }
46
47
    public function loadFiles()
48
    {
49
        $this->files = $this->readConfig(static::FILES_FILENAME);
0 ignored issues
show
Bug introduced by
The method readConfig() does not seem to exist on object<hiqdev\composer\config\Builder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
    }
51
52
    public function saveFiles()
53
    {
54
        $this->writeConfig(static::FILES_FILENAME, $this->files);
55
    }
56
57
    public function buildConfigs($files = null)
58
    {
59
        if (is_null($files)) {
60
            $files = $this->files;
61
        }
62
        foreach ($files as $name => $pathes) {
63
            $configs = [];
64
            foreach ($pathes as $path) {
65
                $configs[] = $this->readFile($path);
66
            }
67
            $this->buildConfig($name, $configs);
68
        }
69
    }
70
71
    /**
72
     * Merges given configs and writes at given name.
73
     * @param mixed $name
74
     * @param array $configs
75
     */
76
    public function buildConfig($name, array $configs)
77
    {
78
        if (!$this->isSpecialConfig($name)) {
79
            array_push($configs, [
80
                'params' => $this->vars['params'],
81
            ]);
82
        }
83
        $this->vars[$name] = call_user_func_array([Helper::className(), 'mergeConfig'], $configs);
84
        $this->writeConfig($name, (array) $this->vars[$name]);
85
    }
86
87
    protected function isSpecialConfig($name)
88
    {
89
        return in_array($name, ['defines', 'params'], true);
90
    }
91
92
    /**
93
     * Writes config file by name.
94
     * @param string $name
95
     * @param array $data
96
     */
97
    public function writeConfig($name, array $data)
98
    {
99
        static::writeFile($this->getOutputPath($name), $data);
100
    }
101
102
    public function getOutputPath($name)
103
    {
104
        return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php';
105
    }
106
107
    /**
108
     * Writes config file by full path.
109
     * @param string $path
110
     * @param array $data
111
     */
112
    public static function writeFile($path, array $data)
113
    {
114
        if (!file_exists(dirname($path))) {
115
            mkdir(dirname($path), 0777, true);
116
        }
117
        $array = str_replace("'" . self::BASE_DIR_SAMPLE, '$baseDir . \'', Helper::exportVar($data));
118
        file_put_contents($path, "<?php\n\n\$baseDir = dirname(dirname(dirname(__DIR__)));\n\nreturn $array;\n");
119
    }
120
121
    /**
122
     * Reads config file.
123
     * @param string $__path
124
     * @return array configuration read from file
125
     */
126
    public function readFile($__path)
127
    {
128
        if (strncmp($__path, '?', 1) === 0) {
129
            $__skippable = true;
130
            $__path = substr($__path, 1);
131
        }
132
133
        if (file_exists($__path)) {
134
            /// Expose variables to be used in configs
135
            extract($this->vars);
136
137
            return (array) require $__path;
138
        }
139
140
        if (empty($__skippable)) {
141
            $this->writeError('<error>Non existent config file</error> ' . $__path);
142
        }
143
144
        return [];
145
    }
146
147
    /**
148
     * @var IOInterface
149
     */
150
    protected $io;
151
152
    public function setIo(IOInterface $io)
153
    {
154
        $this->io = $io;
155
    }
156
157
    protected function writeError($text)
158
    {
159
        if (isset($this->io)) {
160
            $this->io->writeError($text);
161
        } else {
162
            echo $text . "\n";
163
        }
164
    }
165
166
}
167