/
generate.php
359 lines (304 loc) · 9.95 KB
/
generate.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
* Generates the necessary includes from the given file root.
*/
require(__DIR__ . "/functions.php");
if (count($argv) < 2) {
throw new Exception("Needs file root parameter");
}
$root = $argv[1];
$discovery = $root . "/discovery.json";
if (isset($argv[2])) {
$discovery = $argv[2];
}
if (!file_exists($discovery)) {
throw new Exception("No $discovery found");
}
$json = json_decode(file_get_contents($discovery), true /* assoc */);
if (!$json) {
throw new Exception("Could not load JSON from $discovery");
}
// add default parameters
$json += array(
'namespace' => 'DiscoveredComponents',
'src' => 'vendor/*/*',
'dest' => 'generated/components',
'depth' => 3,
'composer_depth' => 3,
'autoload' => 'vendor/autoload.php'
);
if (!is_array($json['src'])) {
$json['src'] = array($json['src']);
}
echo "Loaded " . count($json['components']) . " component discovery patterns\n";
// load autoloader if necessary
if ($json["autoload"]) {
require($json["autoload"]);
}
// make target directories as necessary
make_target_directories(array($json['dest'] . "/"));
// now load all of the components
$all_dirs = get_all_directories($root, $json['depth']);
echo "Found " . count($all_dirs) . " potential subdirectories\n";
$selected_dirs = array();
foreach ($json['src'] as $pattern) {
$selected_dirs = array_merge($selected_dirs, get_directories_to_search($all_dirs, $pattern));
}
echo "Filtered to " . count($selected_dirs) . " matching paths\n";
$component_discoverers = array();
$namespace = $json['namespace'];
if ($selected_dirs) {
foreach ($json['components'] as $key => $component_value) {
if (!is_array($component_value)) {
$component_value = array(
"file" => $component_value,
);
}
// default values
$component_value += array(
"maps" => array(),
"masks" => array(),
"lists" => array(),
"instances" => array(),
);
$filename = $component_value["file"];
$check_instance_of = isset($component_value["instanceof"]) ? $component_value["instanceof"] : false;
echo "Processing '$key' components...\n";
$full_name = ucfirst($key);
$component_discoverers[] = " \"$key\" => new $namespace\\$full_name()";
$keys = array();
$instances = array();
$all_instances = array();
$packages = array();
$maps = array();
$masks = array();
$lists = array();
$mask_instances = array();
// initialise maps so we don't generate missing methods if there are no results
foreach ($component_value['maps'] as $key => $method) {
$maps[$key] = array();
}
foreach ($component_value['masks'] as $key => $method) {
$masks[$key] = array();
}
foreach ($component_value['lists'] as $key => $method) {
$lists[$key] = array();
}
foreach ($component_value['instances'] as $key => $method) {
$mask_instances[$key] = array();
}
$count = 0;
foreach ($selected_dirs as $dir) {
if (file_exists($dir . "/" . $filename)) {
$component = json_decode(file_get_contents($dir . "/" . $filename), true);
if (!$component) {
throw new Exception("Could not load JSON from '$dir/$filename'");
}
// try and find a composer.json for this component
$path = $dir;
$composer_json = false;
for ($i = 1; $i < $json['composer_depth']; $i++) {
if (file_exists($path . "/composer.json")) {
$composer_json = json_decode(file_get_contents($path . "/composer.json"), true /* assoc */);
break;
}
$path .= "/..";
}
foreach ($component as $component_key => $classname) {
// we can assume class names are unique
if (is_numeric($component_key)) {
$component_key = $classname;
}
$keys[] = "\"$component_key\"";
$instances[] = " case \"$component_key\": return new $classname(\$config);";
$all_instances[] = "\"$component_key\" => new $classname(\$config)";
if ($composer_json) {
$packages[] = " case \"$component_key\": return \"" . $composer_json['name'] . "\";";
}
if ($check_instance_of || $component_value["maps"] || $component_value["lists"]) {
// instantiate object (using the autoloader as necessary)
$object = new $classname;
// do we want to check that this classname is a valid instanceof?
if ($check_instance_of) {
if (!is_a($object, $check_instance_of)) {
throw new Exception("Classname '$classname' is not an instance of '$check_instance_of'");
}
}
// do we want to make any lists or maps?
foreach ($component_value["maps"] as $key => $method) {
$maps[$key][] = " case \"" . $object->$method() . "\": return \"$component_key\";";
}
foreach ($component_value["masks"] as $key => $method) {
if ($object->$method()) {
$masks[$key][] = "\"$component_key\"";
}
}
foreach ($component_value["lists"] as $key => $method) {
if ($temp = $object->$method()) {
$lists[$key][] = "\"$component_key\" => " . (is_numeric($temp) ? "$temp" : "\"$temp\"");
}
}
foreach ($component_value["instances"] as $key => $instanceof_name) {
if (is_a($object, $instanceof_name)) {
$mask_instances[$key][] = "\"$component_key\"";
}
}
}
}
$count++;
}
}
// now sort
sort($keys);
sort($instances);
sort($packages);
echo "Found $count '$full_name' components\n";
$keys = implode(", ", $keys);
$instances = implode("\n", $instances);
$packages = implode("\n", $packages);
$output_maps = array();
foreach ($maps as $key => $values) {
$method_name = $component_value["maps"][$key];
$output_maps[] = "
/**
* A reverse map for objects based on the result of their {@code $method_name()}.
* @return the string key based on the return value of their {@code $method_name()} method
*/
static function $key(\$input) {
switch (\$input) {
" . implode("\n", $values) . "
default:
throw new \\ComponentDiscovery\\DiscoveryException(\"Could not find any matching $key for '\$input'\");
}
}
";
}
$output_maps = implode($output_maps, "");
$output_masks = array();
foreach ($masks as $key => $values) {
$method_name = $component_value["masks"][$key];
$output_masks[] = "
/**
* @return an array of all classes that return `true` with their {@code $method_name()} method
*/
static function $key() {
return array(
" . implode(",\n ", $values) . "
);
}
";
}
$output_masks = implode($output_masks, "");
$output_lists = array();
foreach ($lists as $key => $values) {
$method_name = $component_value["lists"][$key];
$output_lists[] = "
/**
* @return an array of all {@code $method_name()} values across all components, associated with their unique key
*/
static function $key() {
return array(
" . implode(",\n ", $values) . "
);
}
";
}
$output_lists = implode($output_lists, "");
$output_mask_instances = array();
foreach ($mask_instances as $key => $values) {
$method_name = $component_value["instances"][$key];
$output_mask_instances[] = "
/**
* @return an array of all keys which classes are instances of $method_name
*/
static function $key() {
return array(
" . implode(",\n ", $values) . "
);
}
";
}
$output_mask_instances = implode($output_mask_instances, "");
$fp = fopen($json['dest'] . "/$full_name.php", "w");
if (!$fp) {
throw new Exception("Could not open destination file '" . $json['dest'] . "/$key.php' for writing");
}
fwrite($fp, "<?php
/**
* @generated by component-discovery DO NOT EDIT
*/
namespace $namespace;
/**
* Runtime component $full_name discovery,
* generated from '$filename' component definition files.
*
* Each component has a unique key (available through {@link #getKeys()})
* and a runtime instance object (available through {@link #getInstance()}).
*
* @generated by component-discovery DO NOT EDIT
*/
class $full_name extends \\ComponentDiscovery\\Base {
/**
* @return an array of all unique $full_name string keys
*/
static function getKeys() {
return array($keys);
}
/**
* @return true if this key is a valid key according to {@link #getKeys()}, false otherwise
*/
static function hasKey(\$key) {
return in_array(\$key, self::getKeys());
}
/**
* @return the corresponding runtime instance object for the given string \$key
*/
static function getInstance(\$key, \$config = false) {
switch (\$key) {
$instances
default:
throw new \\ComponentDiscovery\\DiscoveryException(\"Could not find any $full_name with key '\$key'\");
}
}
/**
* @return the corresponsing composer package name that defined this object \$key,
* or {@code null} if none is defined or none could be found.
*/
static function getDefiningPackage(\$key) {
switch (\$key) {
$packages
default: return null;
}
}
/**
* @return an array of all $full_name instances, each with their unique string \$key
*/
static function getAllInstances(\$config = false) {
return array(
" . implode(",\n ", $all_instances) . "
);
}
$output_maps$output_masks$output_lists$output_mask_instances
}
");
fclose($fp);
}
}
// write a all.php
$component_discoverers = implode(",\n", $component_discoverers);
$fp = fopen($json['dest'] . "/All.php", "w");
fwrite($fp, "<?php
/**
* @generated by component-discovery DO NOT EDIT
*/
namespace $namespace;
class All {
static function getAllComponentTypes() {
return array(
$component_discoverers
);
}
}
");
echo "Complete.";
// TODO generate a development version that does not require a rebuild?