stub2ref
[mdref/mdref] / mdref / Generator.php
diff --git a/mdref/Generator.php b/mdref/Generator.php
new file mode 100644 (file)
index 0000000..84a323e
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+namespace mdref;
+
+use mdref\Generator\{Cls, Func};
+
+class Generator {
+       /**
+        * @param array<string, array<string, \ReflectionFunctionAbstract>> $functions
+        * @return void
+        */
+       public function generateFunctions(array $functions) : void {
+               foreach ($functions as $ns => $funcs) {
+                       $ns_path = strtr($ns, "\\", "/");
+                       foreach ($funcs as $fn => $rf) {
+                               $fn_file = "$ns_path/$fn.md";
+                               fprintf(STDERR, "Generating %s\n", $fn_file);
+                               is_dir($ns_path) || mkdir($ns_path, 0770, true);
+                               file_put_contents($fn_file, new Func($this, $rf));
+                       }
+               }
+       }
+
+       /**
+        * @param array<string, array<string, \ReflectionClass>> $classes
+        * @return void
+        */
+       public function generateClasses(array $classes) : void {
+               foreach ($classes as $ns => $cls) {
+                       $ns_path = strtr($ns, "\\", "/");
+                       foreach ($cls as $cn => $rc) {
+                               $cn_path = "$ns_path/$cn";
+                               $cn_file = "$cn_path.md";
+                               fprintf(STDERR, "Generating %s\n", $cn_file);
+                               is_dir($ns_path) || mkdir($ns_path, 0770, true);
+                               file_put_contents($cn_file, new Cls($this, $rc));
+                               $this->generateMethods($rc);
+                       }
+               }
+       }
+
+       private function generateMethods(\ReflectionClass $rc) : void {
+               $funcs = [];
+               foreach ($rc->getMethods(\ReflectionMethod::IS_PUBLIC) as $rm) {
+                       if ($rm->getDeclaringClass()->getName() === $rc->getName()) {
+                               foreach ($rc->getInterfaces() as $ri) {
+                                       if ($ri->hasMethod($rm->getName())) {
+                                               continue 2;
+                                       }
+                               }
+                               $funcs[$rc->getName()][$rm->getName()] = $rm;
+                       }
+               }
+               $this->generateFunctions($funcs);
+       }
+}