/
Builder.php
195 lines (175 loc) · 6 KB
/
Builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* This Software is part of aryelgois/yasql-php and is provided "as is".
*
* @see LICENSE
*/
namespace aryelgois\YaSql;
use aryelgois\YaSql\Populator;
use Symfony\Component\Yaml\Yaml;
/**
* Automated generation of database schemas into a directory
*
* @author Aryel Mota Góis
* @license MIT
* @link https://www.github.com/aryelgois/yasql-php
*/
class Builder
{
/**
* Build log
*
* @var string[]
*/
protected $log;
/**
* Path to build output
*
* @var string
*/
protected $output;
/**
* List of configs already read
*
* @var string[]
*/
protected $track = [];
/**
* Path to vendors directory
*
* @var string|false
*/
protected $vendor;
/**
* Creates a new Builder object
*
* @param string $output Where files will be stored
* @param string $vendor Path to vendors directory
*
* @throws \RuntimeException Can not create output directory
*/
public function __construct(string $output = null, string $vendor = null)
{
if ($output === null) {
$output = getcwd() . '/build';
}
if (!is_dir($output) && !mkdir($output, 0775, true)) {
throw new \RuntimeException('Can not create output directory');
}
$this->output = realpath($output);
$this->vendor = realpath($vendor ?? 'vendor');
$this->log = 'Build start: ' . date('c') . "\n"
. "Output: $this->output\n\n";
if ($this->vendor === false) {
$this->log .= "N: Could not find vendor dir\n\n";
}
}
/**
* Builds databases in a config file
*
* @param string $config Path to YAML with build configurations
* @param array $vendors List of additional vendors to include
* (using the default config file location)
*/
public function build(string $config, array $vendors = null)
{
$config_path = realpath($config);
if ($config !== '' && $config_path !== false) {
$config_path = realpath($config);
if (in_array($config_path, $this->track)) {
$this->log .= "Skiping repeated config file '$config_path'\n";
return;
}
$this->track[] = $config_path;
$this->log .= "Load config file '$config_path'\n";
$config = Yaml::parse(file_get_contents($config_path));
$indent = $config['indentation'] ?? null;
} else {
$this->log = trim($this->log) . "\n";
}
if (!empty($config['databases'] ?? [])) {
$generated = '';
foreach ($config['databases'] as $database) {
$path = $database['path'] ?? $database;
if ($path[0] !== '/') {
$path = dirname($config_path) . '/' . $path;
}
$file = realpath($path);
if ($file === false) {
$this->log .= "E: Database '$path' not found\n";
continue;
}
$sql = Controller::generate(
file_get_contents($file),
$database['name'] ?? null,
$indent
);
$post_list = (array) ($database['post'] ?? []);
foreach ($post_list as $post) {
if (is_array($post)) {
$post_name = $class = $post['call'];
if (!is_subclass_of($class, Populator::class)) {
$this->log .= "E: Class '$class' does not extend "
. Populator::class . "\n";
continue;
}
$obj = new $class();
$post_sql = '';
foreach ((array) $post['with'] as $with) {
if ($with[0] !== '/') {
$with = dirname($config_path) . '/' . $with;
}
$obj->load($with);
$post_sql .= "--\n-- With '" . basename($with)
. "'\n--\n\n" . trim($obj->run()) . "\n\n";
}
} else {
if ($post[0] !== '/') {
$post = dirname($config_path) . '/' . $post;
}
$post_file = realpath($post);
if ($post_file === false) {
$this->log .= "W: Post file '$post' not found\n";
continue;
}
$post_name = basename($post);
$post_sql = file_get_contents($post_file);
}
$sql .= "\n--\n-- Post '$post_name'\n--\n\n"
. trim($post_sql) . "\n";
}
$outfile = basename(substr($file, 0, strrpos($file, '.')))
. '.sql';
file_put_contents($this->output . '/' . $outfile, $sql);
$generated .= "- $outfile\n";
}
$this->log .= "Files generated:\n$generated";
}
if ($this->vendor === false) {
return;
}
$vendors = array_merge_recursive(
array_fill_keys($vendors ?? [], null),
$config['vendors'] ?? []
);
foreach ($vendors as $vendor => $vendor_configs) {
$this->log .= "\nSwitch to vendor $vendor\n\n";
if ($vendor_configs === null) {
$vendor_configs = [null];
}
foreach ((array) $vendor_configs as $vendor_config) {
$this->build("$this->vendor/$vendor/"
. ($vendor_config ?? 'config/databases.yml'));
}
}
}
/**
* Returns the build log
*
* @return string
*/
public function getLog()
{
return $this->log;
}
}