Skip to content

Commit

Permalink
refactor(routing): Allow unknown routes.php formats
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Jan 30, 2024
1 parent 6283132 commit fb82c9e
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ public function __construct(

public static function parseRoutes(string $path): array {
$content = file_get_contents($path);
if (str_contains($content, "return ")) {
if (str_contains($content, "\$this")) {
preg_match("/return ([^;]*);/", $content, $matches);
return self::includeRoutes("<?php\nreturn " . $matches[1] . ";");
if (str_contains($content, 'return ')) {
if (str_contains($content, '$this')) {
preg_match('/return ([^;]*);/', $content, $matches);
return self::includeRoutes("<?php\nreturn ${matches[1]};");
}
return include($path);
} elseif (str_contains($content, "registerRoutes")) {
preg_match_all("/registerRoutes\(.*?\\\$this,.*?(\[[^;]*)\);/s", $content, $matches);
return array_merge(...array_map(fn (string $match) => self::includeRoutes("<?php\nreturn " . $match . ";"), $matches[1]));
} else {
Logger::panic("Routes", "Unknown routes.php format");
} elseif (str_contains($content, 'registerRoutes')) {
preg_match_all('/registerRoutes\(.*?\$this,.*?(\[[^;]*)\);/s', $content, $matches);
return array_merge(...array_map(fn (string $match) => self::includeRoutes("<?php\nreturn $match;"), $matches[1]));
}

Logger::warning('Routes', 'Unknown routes.php format');
return [];
}

private static function includeRoutes(string $code): array {
Expand Down

0 comments on commit fb82c9e

Please sign in to comment.