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