Skip to content

Commit 250e75e

Browse files
committed
gen routes for module controllers, fix doc generation
1 parent 6c98778 commit 250e75e

4 files changed

Lines changed: 67 additions & 6 deletions

File tree

core/data/routes/templates/header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
$nav_name = substr($nav_name, 0, strlen($nav_name) - 5);
3030
}
3131
?>
32-
<a href="<?=$nav_package . '.' . $nav_item?>.html"><?=($nav_package == 'pages' ? '' : (strtolower($nav_package) . '.')) . $nav_name?></a>
32+
<a href="<?=$nav_package . '.' . $nav_item?>.html"><?=$nav_name?></a>
3333
<?php } ?>
3434
<?php } ?>
3535
</nav>

core/routes/RouteClass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ class RouteClass
77
public $name;
88
public $description;
99
public $package;
10+
public $module;
1011

1112
private $methods;
1213

13-
public function __construct($name, $description = [], $package = "NoPak")
14+
public function __construct($name, $description = [], $package = "NoPak", $module = null)
1415
{
1516
$this->name = $name;
1617
$this->description = $description;
1718
$this->package = $package;
19+
$this->module = $module;
1820

1921
$this->methods = array();
2022
}

core/support/Routes.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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");

modules/Logger/controllers/Logger.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// Copyright 2012-2024 OpenBroadcaster, Inc.
44
// SPDX-License-Identifier: AGPL-3.0-or-later
55

6+
/**
7+
* Logger example module.
8+
*
9+
* @package Controller.Logger
10+
*/
611
namespace OpenBroadcaster\Modules\Logger\Controllers;
712

813
use OpenBroadcaster\Base\Controller;
@@ -20,6 +25,15 @@ public function __construct()
2025

2126
}
2227

28+
/**
29+
* View log.
30+
*
31+
* @param limit
32+
* @param offset
33+
* @return [entries, total]
34+
*
35+
* @route GET /logger
36+
*/
2337
public function viewLog()
2438
{
2539

@@ -32,6 +46,11 @@ public function viewLog()
3246
return array(true,'Log Entries.',array('entries'=>$entries, 'total'=>$total));
3347
}
3448

49+
/**
50+
* Clear log.
51+
*
52+
* @route DELETE /logger
53+
*/
3554
public function clearLog()
3655
{
3756
$this->LoggerModel('logClear');

0 commit comments

Comments
 (0)