feat: ServiceRouter — auto-discovery, dynamic routing, and caching#399
Merged
Conversation
added 4 commits
June 14, 2026 12:21
Scans a namespace for routable classes and registers API routes: - #[RestController] attributed classes (uses attribute name or derived) - WebService subclasses without attribute (uses getName()) - WebServicesManager subclasses (registered as manager routes) - Non-service classes are skipped Includes DI container integration for service instantiation. Closes #382 Closes #384
- ServiceRouter::dynamic() registers catch-all route for namespace - ServiceRouter::handle() resolves controller at request time, 404 if not found - RouteOption::NS constant for namespace-based routing - ServicesListCommand lists all discovered services with name, class, type, path Closes #383 Closes #385
- discover() accepts recursive flag for subdirectory scanning - Subdirectory names become kebab-cased URL path segments - Class names converted to kebab-case via CaseConverter - #[RestController] name with '/' is rejected (skipped) - Explicit attribute name is always flat (ignores directory nesting) - Non-recursive remains the default
- RouteCache class: build/load/clear cached service discovery results - Uses app's existing cache backend (file, Redis, etc.) - routes:cache CLI command to build cache - routes:clear CLI command to clear cache - Enabled via ROUTE_CACHE_ENABLED env variable - 100% test coverage (12 tests, 35/35 lines) Closes #386
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Complete ServiceRouter integration: auto-discover API routes from namespaces, dynamic runtime resolution, DI container integration, CLI tooling, and production route caching.
Motivation
Eliminate boilerplate of manually registering every API service. Developers create a
#[RestController]class and it is routable. Supports traditionalWebServiceandWebServicesManagerclasses too.Changes
ServiceRouter::discover()— scans namespace, registers routes for#[RestController],WebService, andWebServicesManagerclassesServiceRouter::dynamic()— registers catch-all route for runtime resolutionServiceRouter::handle()— resolves controller name to class at request time (404 if not found)RouteOption::NSconstant for namespace-based routingRouteCache— caches discovery results using app cache backend (file/Redis)ServicesListCommand(services:list) — lists all discovered servicesRoutesCacheCommand(routes:cache) — builds route cacheRoutesClearCommand(routes:clear) — clears route cache#[RestController]name with/rejected (name is identifier, not path)How to Test / Verify
vendor/bin/phpunit --filter "ServiceRouterTest|RouteCacheTest|ServicesListCommandTest"37 tests covering all features.
RouteCachehas 100% line coverage.Breaking Changes and Migration Steps
None. All existing routing (
Router::api(),WebServicesManager) works unchanged. ServiceRouter is opt-in.Checklist
Related issues
Closes #382
Closes #383
Closes #384
Closes #385
Closes #386