diff --git a/src/Cortex/Router/MatchingResult.php b/src/Cortex/Router/MatchingResult.php index a5ac65d..e23759d 100644 --- a/src/Cortex/Router/MatchingResult.php +++ b/src/Cortex/Router/MatchingResult.php @@ -35,6 +35,7 @@ public function __construct(array $data) 'route' => null, 'path' => null, 'vars' => null, + 'matches' => null, 'handler' => null, 'before' => null, 'after' => null, @@ -68,6 +69,14 @@ public function vars() return is_array($this->data['vars']) ? $this->data['vars'] : []; } + /** + * @return array + */ + public function matches() + { + return is_array($this->data['matches']) ? $this->data['matches'] : []; + } + /** * @return bool */ diff --git a/src/Cortex/Router/ResultHandler.php b/src/Cortex/Router/ResultHandler.php index 838c9b9..6269d80 100644 --- a/src/Cortex/Router/ResultHandler.php +++ b/src/Cortex/Router/ResultHandler.php @@ -28,6 +28,11 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest) $result = apply_filters('cortex.match.done', $result, $wp, $doParseRequest); $handlerResult = $doParseRequest; + if (! $result instanceof MatchingResult) { + return $result; + } + + /** @var \Brain\Cortex\Router\MatchingResult $result */ if ($result->matched()) { $doParseRequest = false; $origHandler = $result->handler(); @@ -37,12 +42,13 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest) $template = $result->template(); (is_string($template)) or $template = ''; $vars = $result->vars(); + $matches = $result->matches(); do_action('cortex.matched', $result, $wp); - is_callable($before) and $before($vars, $wp, $template); - is_callable($handler) and $handlerResult = $handler($vars, $wp, $template); - is_callable($after) and $after($vars, $wp, $template); + is_callable($before) and $before($vars, $wp, $template, $matches); + is_callable($handler) and $handlerResult = $handler($vars, $wp, $template, $matches); + is_callable($after) and $after($vars, $wp, $template, $matches); $template and $this->setTemplate($template); do_action('cortex.matched-after', $result, $wp, $handlerResult); diff --git a/src/Cortex/Router/Router.php b/src/Cortex/Router/Router.php index f053c74..f5c303a 100644 --- a/src/Cortex/Router/Router.php +++ b/src/Cortex/Router/Router.php @@ -224,6 +224,8 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface $merge = filter_var($route['merge_query_string'], FILTER_VALIDATE_BOOLEAN); $uriVars = $uri->vars(); $merge and $vars = array_merge($vars, $uriVars); + // vars is going to be modified if route vars is a callback, lets save this as a backup + $varsOriginal = $vars; $result = null; switch (true) { case (is_callable($route['vars'])) : @@ -255,6 +257,7 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface return new MatchingResult([ 'vars' => (array)$vars, + 'matches' => (array)$varsOriginal, 'route' => $route->id(), 'path' => $route['path'], 'handler' => $route['handler'], diff --git a/tests/src/Unit/Router/ResultHandlerTest.php b/tests/src/Unit/Router/ResultHandlerTest.php index 8194eeb..59c54ca 100644 --- a/tests/src/Unit/Router/ResultHandlerTest.php +++ b/tests/src/Unit/Router/ResultHandlerTest.php @@ -72,6 +72,7 @@ public function testHandleAllCallbacks() $result->shouldReceive('afterHandler')->once()->andReturn($after); $result->shouldReceive('template')->once()->andReturnNull(); $result->shouldReceive('vars')->once()->andReturn(['foo' => 'bar']); + $result->shouldReceive('matches')->once()->andReturn([]); $wp = \Mockery::mock('WP'); @@ -129,6 +130,7 @@ public function testHandleTemplate() $result->shouldReceive('afterHandler')->once()->andReturn(null); $result->shouldReceive('template')->once()->andReturn('foo'); $result->shouldReceive('vars')->once()->andReturn([]); + $result->shouldReceive('matches')->once()->andReturn([]); $handler = new ResultHandler(); @@ -178,6 +180,7 @@ public function testHandleTemplateDoNothingIfNoTemplateFound() $result->shouldReceive('afterHandler')->once()->andReturn(null); $result->shouldReceive('template')->once()->andReturn('foo'); $result->shouldReceive('vars')->once()->andReturn([]); + $result->shouldReceive('matches')->once()->andReturn([]); $handler = new ResultHandler(); diff --git a/tests/src/Unit/Router/RouterTest.php b/tests/src/Unit/Router/RouterTest.php index 785b072..c6e9b84 100644 --- a/tests/src/Unit/Router/RouterTest.php +++ b/tests/src/Unit/Router/RouterTest.php @@ -60,6 +60,7 @@ public function testMatchNothingIfNoRoutes() 'route' => null, 'path' => null, 'vars' => null, + 'matches' => null, 'handler' => null, 'before' => null, 'after' => null, @@ -92,6 +93,7 @@ public function testMatchNothingIfNoFilteredRoutes() 'route' => null, 'path' => null, 'vars' => null, + 'matches' => null, 'handler' => null, 'before' => null, 'after' => null, @@ -128,6 +130,7 @@ public function testMatchNothingIfNoValidatingRoutes() 'route' => null, 'path' => null, 'vars' => null, + 'matches' => null, 'handler' => null, 'before' => null, 'after' => null, @@ -185,6 +188,7 @@ public function testMatchNotMatching() 'route' => null, 'path' => null, 'vars' => null, + 'matches' => null, 'handler' => null, 'before' => null, 'after' => null, @@ -245,6 +249,7 @@ public function testMatchMatchingExactMatch() 'route' => 'r1', 'path' => '/foo', 'vars' => ['d' => 'D', 'c' => 'C'], + 'matches' => ['c' => 'C'], 'handler' => $handler, 'before' => null, 'after' => null, @@ -316,6 +321,7 @@ public function testMatchDynamicMatch() 'route' => 'r1', 'path' => '/foo/{bar}', 'vars' => ['d' => 'D', 'bar' => 'i-am-bar', 'c' => 'C'], + 'matches' => ['bar' => 'i-am-bar', 'c' => 'C'], 'handler' => $handler, 'before' => null, 'after' => null, @@ -380,6 +386,7 @@ public function testMatchMatchingExactMatchNoQueryVars() 'route' => 'r1', 'path' => '/foo', 'vars' => ['d' => 'D'], + 'matches' => [], 'handler' => $handler, 'before' => null, 'after' => null, @@ -453,6 +460,7 @@ public function testMatchMatchingNoQueryVarsMaintainPreviewVar() 'preview_id' => '123', 'preview_nonce' => 'abc', ], + 'matches' => [], 'handler' => $handler, 'before' => null, 'after' => null, @@ -518,6 +526,7 @@ public function testMatchMatchingExactMatchCallableVars() 'route' => 'r1', 'path' => '/foo', 'vars' => ['c'], + 'matches' => ['c' => 'C'], 'handler' => $handler, 'before' => null, 'after' => null,