Automattic\WooCommerce\Blocks\Domain\Services
Hydration::match_route_to_handler
Inspired from WP core's match_request_to_handler, this matches a given path from available route regexes. Extracts URL parameters from regex named groups and query string parameters.
Method of the class: Hydration{}
No Hooks.
Returns
Array|null. Array with 'controller', 'url_params', and 'query_params' keys, or null if no match.
Usage
// private - for code of main (parent) class only $result = $this->match_route_to_handler( $path, $available_routes );
- $path(string) (required)
- The path to match (may include query string).
- $available_routes(array) (required)
- Available routes in {
$regex1=>$contoller_class1, ... } format.
Hydration::match_route_to_handler() Hydration::match route to handler code WC 10.5.0
private function match_route_to_handler( $path, $available_routes ) {
// Parse query string if present.
$query_params = array();
$parsed_url = wp_parse_url( $path );
$clean_path = $parsed_url['path'] ?? $path;
if ( isset( $parsed_url['query'] ) ) {
parse_str( $parsed_url['query'], $query_params );
}
// Match route and extract URL parameters.
foreach ( $available_routes as $route_path => $controller ) {
if ( preg_match( '@^' . $route_path . '$@i', $clean_path, $matches ) ) {
// Extract named groups (URL parameters like 'id').
$url_params = array_intersect_key(
$matches,
array_flip( array_filter( array_keys( $matches ), 'is_string' ) )
);
return array(
'controller' => $controller,
'url_params' => $url_params,
'query_params' => $query_params,
);
}
}
return null;
}