-
Notifications
You must be signed in to change notification settings - Fork 1
/
Router_Factory.php
95 lines (82 loc) · 2.06 KB
/
Router_Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php declare( strict_types=1 );
/**
* Class that is responsible for registering routes.
*
* @package Tribe\Project\Routes
*/
namespace Tribe\Libs\Routes;
use Tribe\Libs\Routes\Abstract_Route;
/**
* Class to register routers for normal and REST API endpoints.
*/
class Router_Factory {
/**
* Currently matched route.
*
* @var Abstract_Route|string
*/
public $matched_route;
/**
* List of Route Instances.
*
* @var array
*/
public $routes;
/**
* List of router vars - tied to query vars.
*
* @var array
*/
public $router_vars;
/**
* Instance of the rewrite rule manager.
*
* @var Router_Rule_Manager
*/
public $manager = null;
/**
* Class constructor.
*
* @return void
*/
public function __construct( Router_Rule_Manager $manager ) {
$this->manager = $manager;
}
/**
* Looks up the custom route for the pattern matched. Returns false
* if not found.
*
* @param string $pattern The regex pattern to lookup.
* @param array $registered_routes Routes registered.
* @return Abstract_Route|null The route or null on failure.
*/
public function find_route( $pattern, $registered_routes ): ?Abstract_Route {
// Load routes if not already loaded.
if ( empty( $this->routes ) ) {
$this->routes = $this->manager->get_route_objects( $registered_routes );
}
// Bail early if the route pattern doesn't exist.
if ( empty( $this->routes[ $pattern ] ) ) {
return null;
}
return $this->routes[ $pattern ];
}
/**
* Checks if the current route is a custom route and activates it if matched.
*
* @hook parse_request
*
* @param \WP $wp The global wp object.
* @param array $registered_routes Routes registered.
* @return Abstract_Route|null The matched route on success, null on failure.
*/
public function did_parse_request( \WP $wp, $registered_routes ): ?Abstract_Route {
$pattern = $wp->matched_rule;
$matched_route = $this->find_route( $pattern, $registered_routes );
// Bail early if no matched route.
if ( empty( $matched_route ) ) {
return null;
}
return $matched_route;
}
}