AdminRouteRegistrar
in package
Admin routing surface: registers a wp-admin menu tree whose pages all render through one dispatch pipeline — the current page/route resolves against the adapter {@see RouterInterface} route collection (controller-declared `#[Route]` attributes or imperative registrations) and executes through the framework HTTP kernel, inheriting its middleware pipeline and the `#[Auth]`/`#[Middleware]`/`#[Nonce]` attributes.
Generalizes the pattern each MIDDAG plugin used to hand-roll: the menu slug derives from the host component name (no brand literal), unmatched routes flow through the injected fallback (so the lib forces neither Inertia nor a "Dashboard" page), and responses are emitted through the injected ResponseEmitterInterface so the previously untestable header/echo paths stay assertable. All wp-admin calls go through AdminSupport.
Table of Contents
Properties
- $capabilities : array<string, string>
- $context : HostComponentContextInterface
- $defaultCapability : string
- Capability enforced for a page not present in {@see self::$capabilities} (defense-in-depth default; overwritten by the main menu's capability on register()). Fail-closed to WordPress's admin default.
- $emitter : ResponseEmitterInterface
- $fallback : Closure
- $kernel : HttpKernelInterface
- $pageHookSuffixes : array<int, string>
- $pageSlugs : array<int, string>
- $routeBases : array<string, string>
- $router : RouterInterface
Methods
- __construct() : mixed
- dispatch() : void
- Resolve a page/route/method to a route and execute it through the HTTP kernel, falling back to the injected callable when nothing matches.
- handleInertiaRequest() : void
- Early Inertia XHR interceptor (wire on `admin_init`): if this is an Inertia request for one of this registrar's pages, render and terminate before WordPress emits the admin HTML shell.
- menuSlug() : string
- Top-level menu slug: the host component name.
- pageHookSuffixes() : array<int, string>
- Registered page hook suffixes (for the consumer's asset-enqueue guard).
- register() : void
- Register the wp-admin menu tree. Every page renders through renderApp().
- renderApp() : void
- wp-admin page render callback: reads the current page/route/method from the request at this boundary, then dispatches. The response body is written inside the admin shell (no termination).
- submenuSlug() : string
- Submenu slug: `{component}-{suffix}`.
- emit() : void
- Emit a PSR-7 response through the emitter seam (status, headers, body).
- matches() : bool
- Whether the route collection has a route for this path/method. Matching runs here (and again inside the kernel) so the no-match case can flow to the fallback instead of a kernel 404.
- psrRequest() : ServerRequestInterface
- Build the PSR-7 request the kernel dispatches: the current request's query/body/cookies/files/server, re-targeted at the synthetic admin route path (the kernel matches paths, not `admin.php?page=...&route=...`).
- requestString() : string
- Read a sanitized string from a request superglobal, or $default when the key is absent or not a string (defends against array-valued params).
- respondToCurrentRequest() : void
- Shared body of renderApp()/handleInertiaRequest(): capability gate at the WordPress boundary (renderApp() is also reachable through admin_init and admin-ajax.php, where WP's own menu gate never runs — fail-closed), then dispatch; optionally terminate (Inertia XHR path, which must pre-empt the admin shell).
- userCanAccess() : bool
- Whether the current user may access $page, checked against the page's registered capability (or the default when the page is unknown). Reads through {@see UserSupport::currentUserCan()}, which fail-closes to false when WordPress is absent.
Properties
$capabilities
private
array<string, string>
$capabilities
= []
page slug => required capability
$context read-only
private
HostComponentContextInterface
$context
$defaultCapability
Capability enforced for a page not present in {@see self::$capabilities} (defense-in-depth default; overwritten by the main menu's capability on register()). Fail-closed to WordPress's admin default.
private
string
$defaultCapability
= 'manage_options'
$emitter read-only
private
ResponseEmitterInterface
$emitter
$fallback read-only
private
Closure
$fallback
$kernel read-only
private
HttpKernelInterface
$kernel
$pageHookSuffixes
private
array<int, string>
$pageHookSuffixes
= []
$pageSlugs
private
array<int, string>
$pageSlugs
= []
$routeBases
private
array<string, string>
$routeBases
= []
page slug => route base
$router read-only
private
RouterInterface
$router
Methods
__construct()
public
__construct(HostComponentContextInterface $context, RouterInterface $router, HttpKernelInterface $kernel, ResponseEmitterInterface $emitter, callable(string, string): void $fallback) : mixed
Parameters
- $context : HostComponentContextInterface
- $router : RouterInterface
- $kernel : HttpKernelInterface
- $emitter : ResponseEmitterInterface
- $fallback : callable(string, string): void
-
runs when no route matches; receives (page slug, resolved path)
dispatch()
Resolve a page/route/method to a route and execute it through the HTTP kernel, falling back to the injected callable when nothing matches.
public
dispatch(string $page, string $route, string $method) : void
Pure — no superglobals.
Parameters
- $page : string
- $route : string
- $method : string
handleInertiaRequest()
Early Inertia XHR interceptor (wire on `admin_init`): if this is an Inertia request for one of this registrar's pages, render and terminate before WordPress emits the admin HTML shell.
public
handleInertiaRequest() : void
menuSlug()
Top-level menu slug: the host component name.
public
menuSlug() : string
Return values
stringpageHookSuffixes()
Registered page hook suffixes (for the consumer's asset-enqueue guard).
public
pageHookSuffixes() : array<int, string>
Return values
array<int, string>register()
Register the wp-admin menu tree. Every page renders through renderApp().
public
register(MenuPage $main, array<int, SubMenuPage> $subPages) : void
Parameters
- $main : MenuPage
- $subPages : array<int, SubMenuPage>
renderApp()
wp-admin page render callback: reads the current page/route/method from the request at this boundary, then dispatches. The response body is written inside the admin shell (no termination).
public
renderApp() : void
submenuSlug()
Submenu slug: `{component}-{suffix}`.
public
submenuSlug(string $slugSuffix) : string
Parameters
- $slugSuffix : string
Return values
stringemit()
Emit a PSR-7 response through the emitter seam (status, headers, body).
private
emit(ResponseInterface $response) : void
Parameters
- $response : ResponseInterface
matches()
Whether the route collection has a route for this path/method. Matching runs here (and again inside the kernel) so the no-match case can flow to the fallback instead of a kernel 404.
private
matches(string $path, string $method) : bool
Parameters
- $path : string
- $method : string
Return values
boolpsrRequest()
Build the PSR-7 request the kernel dispatches: the current request's query/body/cookies/files/server, re-targeted at the synthetic admin route path (the kernel matches paths, not `admin.php?page=...&route=...`).
private
psrRequest(string $method, string $path) : ServerRequestInterface
Parameters
- $method : string
- $path : string
Return values
ServerRequestInterfacerequestString()
Read a sanitized string from a request superglobal, or $default when the key is absent or not a string (defends against array-valued params).
private
requestString(array<string, mixed> $source, string $key[, string $default = '' ]) : string
Parameters
- $source : array<string, mixed>
- $key : string
- $default : string = ''
Return values
stringrespondToCurrentRequest()
Shared body of renderApp()/handleInertiaRequest(): capability gate at the WordPress boundary (renderApp() is also reachable through admin_init and admin-ajax.php, where WP's own menu gate never runs — fail-closed), then dispatch; optionally terminate (Inertia XHR path, which must pre-empt the admin shell).
private
respondToCurrentRequest(bool $terminate) : void
Parameters
- $terminate : bool
userCanAccess()
Whether the current user may access $page, checked against the page's registered capability (or the default when the page is unknown). Reads through {@see UserSupport::currentUserCan()}, which fail-closes to false when WordPress is absent.
private
userCanAccess(string $page) : bool
Parameters
- $page : string