@@ -22,6 +22,9 @@ public function __construct()
2222 $ this ->targetJson = OB_CACHE . '/routes.json ' ;
2323 $ this ->sourceDirs = [
2424 OB_LOCAL . '/core/controllers/ ' ,
25+ OB_LOCAL . '/core/models/ ' ,
26+ ...glob (OB_LOCAL . '/modules/*/controllers/ ' ),
27+ ...glob (OB_LOCAL . '/modules/*/models/ ' ),
2528 ];
2629 }
2730
@@ -39,6 +42,10 @@ public function needsUpdate(): bool
3942 }
4043
4144 foreach ($ this ->sourceDirs as $ dir ) {
45+ if (! str_ends_with ($ dir , '/controllers/ ' )) {
46+ continue ;
47+ }
48+
4249 foreach (new \FilesystemIterator ($ dir ) as $ file ) {
4350 if ($ file ->isDir ()) {
4451 continue ;
@@ -53,7 +60,7 @@ public function needsUpdate(): bool
5360 continue ;
5461 }
5562
56- if (! $ updated [$ file ->getRealPath ()] || $ updated [$ file ->getRealPath ()] !== $ file ->getMTime ()) {
63+ if (! ( $ updated [$ file ->getRealPath ()] ?? null ) || $ updated [$ file ->getRealPath ()] !== $ file ->getMTime ()) {
5764 return true ;
5865 }
5966 }
@@ -67,6 +74,10 @@ public function genRoutes(): bool
6774 $ docFiles = [];
6875 $ updated = [];
6976 foreach ($ this ->sourceDirs as $ dir ) {
77+ if (! str_ends_with ($ dir , '/controllers/ ' )) {
78+ continue ;
79+ }
80+
7081 foreach (new \FilesystemIterator ($ dir ) as $ file ) {
7182 if ($ file ->isDir ()) {
7283 continue ;
@@ -85,7 +96,7 @@ public function genRoutes(): bool
8596 $ blocks = $ this ->parse_blocks ($ content );
8697 $ updated [$ file ->getRealPath ()] = $ file ->getMTime ();
8798
88- $ docFiles [] = $ this ->generate_tree ($ blocks , $ file ->getFilename (), basename ( $ dir) );
99+ $ docFiles [] = $ this ->generate_tree ($ blocks , $ file ->getFilename (), $ dir );
89100 }
90101 }
91102
@@ -94,6 +105,19 @@ public function genRoutes(): bool
94105 foreach ($ file ->getClass ()->getMethods () as $ method ) {
95106 if (count ($ method ->routes ) > 0 ) {
96107 foreach ($ method ->routes as $ route ) {
108+ // Each route is stored in JSON as an array containing elements in the format
109+ // [
110+ // /api/v2/route/string,
111+ // controller,
112+ // method
113+ // ]
114+ //
115+ // When a route comes from a module, it is instead stored as
116+ // [
117+ // /api/v2/module/ModuleName/route/string,
118+ // controller,
119+ // method
120+ // ]
97121 $ routes [$ route [0 ]][] = [$ route [1 ], strtolower ($ file ->getClass ()->name ), $ method ->name ];
98122 }
99123 }
@@ -159,7 +183,13 @@ public function genDocs(string $targetDir): bool
159183 $ content = $ this ->parse_clean (file_get_contents ($ file ->getPathname ()));
160184 $ blocks = $ this ->parse_blocks ($ content );
161185
162- $ doc_files [] = $ this ->generate_tree ($ blocks , $ file ->getFilename (), basename ($ dir ));
186+ if (! str_ends_with (rtrim ($ dir , '/ ' ), '/core/controllers ' )) {
187+ $ moduleStr = '/modules/ ' . basename (dirname ($ dir )) . '/controllers ' ;
188+ } else {
189+ $ moduleStr = '/core/controllers ' ;
190+ }
191+
192+ $ doc_files [] = $ this ->generate_tree ($ blocks , $ file ->getFilename (), $ moduleStr );
163193 }
164194 }
165195
@@ -434,6 +464,11 @@ private function generate_tree(array $blocks, string $filename, string $dir): Ro
434464 break ;
435465 }
436466 }
467+
468+ if (! str_ends_with (rtrim ($ dir , '/ ' ), '/core/controllers ' )) {
469+ $ moduleStr = basename (dirname ($ dir ));
470+ $ doc_class ->module = $ moduleStr ;
471+ }
437472 break ;
438473 case 'method ' :
439474 $ method = new RouteMethod ($ decl ['name ' ], $ doc ['description ' ], $ decl ['visibility ' ], $ decl ['args ' ]);
@@ -455,7 +490,12 @@ private function generate_tree(array $blocks, string $filename, string $dir): Ro
455490 case 'route ' :
456491 $ route_method = substr ($ tag [1 ], 0 , strpos ($ tag [1 ], " " ));
457492 $ route_url = substr ($ tag [1 ], strpos ($ tag [1 ], " " ) + 1 );
458- $ route_url = '/api/ ' . trim ($ route_url , '/ ' );
493+
494+ if ($ doc_class !== null && $ doc_class ->module !== null ) {
495+ $ route_url = '/api/v2/module/ ' . $ doc_class ->module . '/ ' . trim ($ route_url , '/ ' );
496+ } else {
497+ $ route_url = '/api/ ' . trim ($ route_url , '/ ' );
498+ }
459499
460500 if (!in_array ($ route_method , ['GET ' , 'POST ' , 'PUT ' , 'PATCH ' , 'DELETE ' ])) {
461501 error_log ('[E] Unsupported HTTP method used in @route tag: ' . $ tag [1 ] . ". \n" );
0 commit comments