Skip to content

Commit

Permalink
Make original matched vars accessible to handlers
Browse files Browse the repository at this point in the history
they can be get using MatchingResult::matches()
  • Loading branch information
gmazzap committed Jul 14, 2016
1 parent 96022c9 commit 0f33ad8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/Cortex/Router/MatchingResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(array $data)
'route' => null,
'path' => null,
'vars' => null,
'matches' => null,
'handler' => null,
'before' => null,
'after' => null,
Expand Down Expand Up @@ -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
*/
Expand Down
12 changes: 9 additions & 3 deletions src/Cortex/Router/ResultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/Cortex/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) :
Expand Down Expand Up @@ -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'],
Expand Down
3 changes: 3 additions & 0 deletions tests/src/Unit/Router/ResultHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
9 changes: 9 additions & 0 deletions tests/src/Unit/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function testMatchNothingIfNoRoutes()
'route' => null,
'path' => null,
'vars' => null,
'matches' => null,
'handler' => null,
'before' => null,
'after' => null,
Expand Down Expand Up @@ -92,6 +93,7 @@ public function testMatchNothingIfNoFilteredRoutes()
'route' => null,
'path' => null,
'vars' => null,
'matches' => null,
'handler' => null,
'before' => null,
'after' => null,
Expand Down Expand Up @@ -128,6 +130,7 @@ public function testMatchNothingIfNoValidatingRoutes()
'route' => null,
'path' => null,
'vars' => null,
'matches' => null,
'handler' => null,
'before' => null,
'after' => null,
Expand Down Expand Up @@ -185,6 +188,7 @@ public function testMatchNotMatching()
'route' => null,
'path' => null,
'vars' => null,
'matches' => null,
'handler' => null,
'before' => null,
'after' => null,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -380,6 +386,7 @@ public function testMatchMatchingExactMatchNoQueryVars()
'route' => 'r1',
'path' => '/foo',
'vars' => ['d' => 'D'],
'matches' => [],
'handler' => $handler,
'before' => null,
'after' => null,
Expand Down Expand Up @@ -453,6 +460,7 @@ public function testMatchMatchingNoQueryVarsMaintainPreviewVar()
'preview_id' => '123',
'preview_nonce' => 'abc',
],
'matches' => [],
'handler' => $handler,
'before' => null,
'after' => null,
Expand Down Expand Up @@ -518,6 +526,7 @@ public function testMatchMatchingExactMatchCallableVars()
'route' => 'r1',
'path' => '/foo',
'vars' => ['c'],
'matches' => ['c' => 'C'],
'handler' => $handler,
'before' => null,
'after' => null,
Expand Down

0 comments on commit 0f33ad8

Please sign in to comment.