1 | <?php |
||
5 | abstract class BaseMiddleware { |
||
6 | |||
7 | const ALL_METHODS = '*'; |
||
8 | |||
9 | public $scope = array(); |
||
10 | public static $context = array(); |
||
11 | public static $routes = array(); |
||
12 | |||
13 | |||
14 | /** |
||
15 | * |
||
16 | * Restrict a Middleware hook to certain paths and HTTP methods. |
||
17 | * |
||
18 | * No actual restriction takes place in this method. |
||
19 | * We simply place the $methods array into $this->scope, keyed by its $hook. |
||
20 | * |
||
21 | * @param $methods |
||
22 | * An array of HTTP methods that are allowed, or an '*' to match all methods. |
||
23 | * @param $route |
||
24 | * Route to restrict the middleware to. Please note: this must be a route you added with addRoute(), not |
||
25 | * just any path. |
||
26 | * @return $this |
||
27 | */ |
||
28 | public function restrict($methods, $route) { |
||
32 | |||
33 | /** |
||
34 | * Determine whether the current route has any route restrictions for this BaseMiddleware. |
||
35 | * |
||
36 | * BaseMiddleware must have self::$context['pattern'] and self::$context['http_method'] set. |
||
37 | * Furthermore $context['http_method'] can be an array (preflight uses that). |
||
38 | * |
||
39 | * @return bool |
||
40 | * Whether the current route should run $hook. |
||
41 | */ |
||
42 | public function shouldRun() { |
||
64 | |||
65 | /** Preprocess. This is where you'd add new routes **/ |
||
66 | public function preprocess(&$router) {} |
||
67 | /** Preflight. This is where do things after routes are finalized but before processing starts **/ |
||
68 | public function preflight() {} |
||
69 | /** Preroute. This is where you would alter request, or implement things like: security etc. **/ |
||
70 | public function preroute(&$req, &$res) {} |
||
71 | /** This is your chance to override output. It can be called multiple times for each ->flush() invocation! **/ |
||
72 | public function prerender(&$buffer) {} |
||
73 | |||
74 | } // end Middleware |