stub2ref
[mdref/mdref] / mdref / Generator.php
1 <?php
2
3 namespace mdref;
4
5 use mdref\Generator\{Cls, Func};
6
7 class Generator {
8 /**
9 * @param array<string, array<string, \ReflectionFunctionAbstract>> $functions
10 * @return void
11 */
12 public function generateFunctions(array $functions) : void {
13 foreach ($functions as $ns => $funcs) {
14 $ns_path = strtr($ns, "\\", "/");
15 foreach ($funcs as $fn => $rf) {
16 $fn_file = "$ns_path/$fn.md";
17 fprintf(STDERR, "Generating %s\n", $fn_file);
18 is_dir($ns_path) || mkdir($ns_path, 0770, true);
19 file_put_contents($fn_file, new Func($this, $rf));
20 }
21 }
22 }
23
24 /**
25 * @param array<string, array<string, \ReflectionClass>> $classes
26 * @return void
27 */
28 public function generateClasses(array $classes) : void {
29 foreach ($classes as $ns => $cls) {
30 $ns_path = strtr($ns, "\\", "/");
31 foreach ($cls as $cn => $rc) {
32 $cn_path = "$ns_path/$cn";
33 $cn_file = "$cn_path.md";
34 fprintf(STDERR, "Generating %s\n", $cn_file);
35 is_dir($ns_path) || mkdir($ns_path, 0770, true);
36 file_put_contents($cn_file, new Cls($this, $rc));
37 $this->generateMethods($rc);
38 }
39 }
40 }
41
42 private function generateMethods(\ReflectionClass $rc) : void {
43 $funcs = [];
44 foreach ($rc->getMethods(\ReflectionMethod::IS_PUBLIC) as $rm) {
45 if ($rm->getDeclaringClass()->getName() === $rc->getName()) {
46 foreach ($rc->getInterfaces() as $ri) {
47 if ($ri->hasMethod($rm->getName())) {
48 continue 2;
49 }
50 }
51 $funcs[$rc->getName()][$rm->getName()] = $rm;
52 }
53 }
54 $this->generateFunctions($funcs);
55 }
56 }