|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is licensed under MIT License. |
| 5 | + * |
| 6 | + * Copyright (c) 2026-present WebFiori Framework |
| 7 | + * |
| 8 | + * For more information on the license, please visit: |
| 9 | + * https://github.com/WebFiori/.github/blob/main/LICENSE |
| 10 | + * |
| 11 | + */ |
| 12 | +namespace WebFiori\Framework\Router; |
| 13 | + |
| 14 | +use ReflectionClass; |
| 15 | +use WebFiori\Container\ContainerFacade; |
| 16 | +use WebFiori\Framework\App; |
| 17 | +use WebFiori\Http\Annotations\RestController; |
| 18 | +use WebFiori\Http\RequestProcessor; |
| 19 | +use WebFiori\Http\WebService; |
| 20 | +use WebFiori\Http\WebServicesManager; |
| 21 | + |
| 22 | +/** |
| 23 | + * Discovers and registers API routes from a namespace. |
| 24 | + * |
| 25 | + * Supports three types of classes: |
| 26 | + * - Classes with #[RestController] attribute (uses attribute name or derived name) |
| 27 | + * - WebService subclasses without attribute (uses getName()) |
| 28 | + * - WebServicesManager subclasses (registered as manager routes) |
| 29 | + * |
| 30 | + * @author Ibrahim |
| 31 | + */ |
| 32 | +class ServiceRouter { |
| 33 | + /** |
| 34 | + * @var array Discovered service map for introspection. |
| 35 | + */ |
| 36 | + private static array $discovered = []; |
| 37 | + |
| 38 | + /** |
| 39 | + * Discover and register routes for all services in a namespace. |
| 40 | + * |
| 41 | + * @param string $namespace Fully qualified namespace (e.g. 'App\\Apis'). |
| 42 | + * @param string $basePath URL prefix (e.g. '/apis'). |
| 43 | + * @param array $routeOptions Shared route options applied to all routes. |
| 44 | + * @param string|null $directory Directory to scan. If null, derived from namespace relative to ROOT_PATH. |
| 45 | + * |
| 46 | + * @return int Number of routes registered. |
| 47 | + */ |
| 48 | + public static function discover(string $namespace, string $basePath, array $routeOptions = [], ?string $directory = null): int { |
| 49 | + $dir = $directory ?? self::namespaceToPath($namespace); |
| 50 | + $map = self::scanNamespace($namespace, $dir); |
| 51 | + $count = 0; |
| 52 | + $basePath = rtrim($basePath, '/'); |
| 53 | + |
| 54 | + foreach ($map as $name => $entry) { |
| 55 | + $class = $entry['class']; |
| 56 | + $type = $entry['type']; |
| 57 | + $path = $basePath . '/' . $name; |
| 58 | + |
| 59 | + $options = array_merge($routeOptions, [ |
| 60 | + RouteOption::PATH => $path, |
| 61 | + ]); |
| 62 | + |
| 63 | + if ($type === 'manager') { |
| 64 | + $options[RouteOption::TO] = $class; |
| 65 | + } else { |
| 66 | + $options[RouteOption::TO] = self::createServiceClosure($class); |
| 67 | + } |
| 68 | + |
| 69 | + Router::api($options); |
| 70 | + self::$discovered[$name] = [ |
| 71 | + 'class' => $class, |
| 72 | + 'type' => $type, |
| 73 | + 'path' => $path, |
| 74 | + ]; |
| 75 | + $count++; |
| 76 | + } |
| 77 | + |
| 78 | + return $count; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns all discovered services. |
| 83 | + * |
| 84 | + * @return array Associative array keyed by name with class, type, and path. |
| 85 | + */ |
| 86 | + public static function getDiscovered(): array { |
| 87 | + return self::$discovered; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Reset discovered services. |
| 92 | + */ |
| 93 | + public static function reset(): void { |
| 94 | + self::$discovered = []; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Scan a namespace directory for routable classes. |
| 99 | + * |
| 100 | + * @param string $namespace The namespace to scan. |
| 101 | + * @param string $dir The directory to scan. |
| 102 | + * |
| 103 | + * @return array Map of name => ['class' => FQCN, 'type' => 'service'|'manager'] |
| 104 | + */ |
| 105 | + private static function scanNamespace(string $namespace, string $dir): array { |
| 106 | + $map = []; |
| 107 | + |
| 108 | + if (!is_dir($dir)) { |
| 109 | + return $map; |
| 110 | + } |
| 111 | + |
| 112 | + $files = glob($dir . DIRECTORY_SEPARATOR . '*.php'); |
| 113 | + |
| 114 | + if ($files === false) { |
| 115 | + return $map; |
| 116 | + } |
| 117 | + |
| 118 | + foreach ($files as $file) { |
| 119 | + $className = basename($file, '.php'); |
| 120 | + $fqcn = $namespace . '\\' . $className; |
| 121 | + |
| 122 | + if (!class_exists($fqcn)) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + $ref = new ReflectionClass($fqcn); |
| 127 | + |
| 128 | + if ($ref->isAbstract() || $ref->isInterface()) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + // Priority 1: #[RestController] attribute |
| 133 | + $attrs = $ref->getAttributes(RestController::class); |
| 134 | + |
| 135 | + if (!empty($attrs)) { |
| 136 | + $attr = $attrs[0]->newInstance(); |
| 137 | + $name = !empty($attr->name) ? $attr->name : self::deriveNameFromClass($className); |
| 138 | + $map[$name] = ['class' => $fqcn, 'type' => 'service']; |
| 139 | + |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + // Priority 2: WebServicesManager subclass |
| 144 | + if ($ref->isSubclassOf(WebServicesManager::class)) { |
| 145 | + $name = self::deriveNameFromClass($className); |
| 146 | + $map[$name] = ['class' => $fqcn, 'type' => 'manager']; |
| 147 | + |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + // Priority 3: WebService subclass without attribute |
| 152 | + if ($ref->isSubclassOf(WebService::class)) { |
| 153 | + $instance = new $fqcn(); |
| 154 | + $name = $instance->getName(); |
| 155 | + |
| 156 | + if (!empty($name)) { |
| 157 | + $map[$name] = ['class' => $fqcn, 'type' => 'service']; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return $map; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Convert namespace to filesystem path. |
| 167 | + */ |
| 168 | + private static function namespaceToPath(string $namespace): string { |
| 169 | + return ROOT_PATH . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $namespace); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Derive route name from class name. |
| 174 | + * OrderService → orders, ProductManager → product |
| 175 | + */ |
| 176 | + private static function deriveNameFromClass(string $className): string { |
| 177 | + $name = preg_replace('/(Service|Manager|Controller)$/', '', $className); |
| 178 | + |
| 179 | + return strtolower($name); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Create a closure that instantiates and processes a service. |
| 184 | + */ |
| 185 | + private static function createServiceClosure(string $class): callable { |
| 186 | + return function () use ($class) { |
| 187 | + $service = ContainerFacade::has($class) |
| 188 | + ? ContainerFacade::make($class) |
| 189 | + : new $class(); |
| 190 | + (new RequestProcessor())->process($service, App::getRequest()); |
| 191 | + }; |
| 192 | + } |
| 193 | +} |
0 commit comments