Skip to content

Commit 0f33ad8

Browse files
committed
Make original matched vars accessible to handlers
they can be get using MatchingResult::matches()
1 parent 96022c9 commit 0f33ad8

5 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/Cortex/Router/MatchingResult.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function __construct(array $data)
3535
'route' => null,
3636
'path' => null,
3737
'vars' => null,
38+
'matches' => null,
3839
'handler' => null,
3940
'before' => null,
4041
'after' => null,
@@ -68,6 +69,14 @@ public function vars()
6869
return is_array($this->data['vars']) ? $this->data['vars'] : [];
6970
}
7071

72+
/**
73+
* @return array
74+
*/
75+
public function matches()
76+
{
77+
return is_array($this->data['matches']) ? $this->data['matches'] : [];
78+
}
79+
7180
/**
7281
* @return bool
7382
*/

src/Cortex/Router/ResultHandler.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
2828
$result = apply_filters('cortex.match.done', $result, $wp, $doParseRequest);
2929
$handlerResult = $doParseRequest;
3030

31+
if (! $result instanceof MatchingResult) {
32+
return $result;
33+
}
34+
35+
/** @var \Brain\Cortex\Router\MatchingResult $result */
3136
if ($result->matched()) {
3237
$doParseRequest = false;
3338
$origHandler = $result->handler();
@@ -37,12 +42,13 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
3742
$template = $result->template();
3843
(is_string($template)) or $template = '';
3944
$vars = $result->vars();
45+
$matches = $result->matches();
4046

4147
do_action('cortex.matched', $result, $wp);
4248

43-
is_callable($before) and $before($vars, $wp, $template);
44-
is_callable($handler) and $handlerResult = $handler($vars, $wp, $template);
45-
is_callable($after) and $after($vars, $wp, $template);
49+
is_callable($before) and $before($vars, $wp, $template, $matches);
50+
is_callable($handler) and $handlerResult = $handler($vars, $wp, $template, $matches);
51+
is_callable($after) and $after($vars, $wp, $template, $matches);
4652
$template and $this->setTemplate($template);
4753

4854
do_action('cortex.matched-after', $result, $wp, $handlerResult);

src/Cortex/Router/Router.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface
224224
$merge = filter_var($route['merge_query_string'], FILTER_VALIDATE_BOOLEAN);
225225
$uriVars = $uri->vars();
226226
$merge and $vars = array_merge($vars, $uriVars);
227+
// vars is going to be modified if route vars is a callback, lets save this as a backup
228+
$varsOriginal = $vars;
227229
$result = null;
228230
switch (true) {
229231
case (is_callable($route['vars'])) :
@@ -255,6 +257,7 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface
255257

256258
return new MatchingResult([
257259
'vars' => (array)$vars,
260+
'matches' => (array)$varsOriginal,
258261
'route' => $route->id(),
259262
'path' => $route['path'],
260263
'handler' => $route['handler'],

tests/src/Unit/Router/ResultHandlerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function testHandleAllCallbacks()
7272
$result->shouldReceive('afterHandler')->once()->andReturn($after);
7373
$result->shouldReceive('template')->once()->andReturnNull();
7474
$result->shouldReceive('vars')->once()->andReturn(['foo' => 'bar']);
75+
$result->shouldReceive('matches')->once()->andReturn([]);
7576

7677
$wp = \Mockery::mock('WP');
7778

@@ -129,6 +130,7 @@ public function testHandleTemplate()
129130
$result->shouldReceive('afterHandler')->once()->andReturn(null);
130131
$result->shouldReceive('template')->once()->andReturn('foo');
131132
$result->shouldReceive('vars')->once()->andReturn([]);
133+
$result->shouldReceive('matches')->once()->andReturn([]);
132134

133135
$handler = new ResultHandler();
134136

@@ -178,6 +180,7 @@ public function testHandleTemplateDoNothingIfNoTemplateFound()
178180
$result->shouldReceive('afterHandler')->once()->andReturn(null);
179181
$result->shouldReceive('template')->once()->andReturn('foo');
180182
$result->shouldReceive('vars')->once()->andReturn([]);
183+
$result->shouldReceive('matches')->once()->andReturn([]);
181184

182185
$handler = new ResultHandler();
183186

tests/src/Unit/Router/RouterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testMatchNothingIfNoRoutes()
6060
'route' => null,
6161
'path' => null,
6262
'vars' => null,
63+
'matches' => null,
6364
'handler' => null,
6465
'before' => null,
6566
'after' => null,
@@ -92,6 +93,7 @@ public function testMatchNothingIfNoFilteredRoutes()
9293
'route' => null,
9394
'path' => null,
9495
'vars' => null,
96+
'matches' => null,
9597
'handler' => null,
9698
'before' => null,
9799
'after' => null,
@@ -128,6 +130,7 @@ public function testMatchNothingIfNoValidatingRoutes()
128130
'route' => null,
129131
'path' => null,
130132
'vars' => null,
133+
'matches' => null,
131134
'handler' => null,
132135
'before' => null,
133136
'after' => null,
@@ -185,6 +188,7 @@ public function testMatchNotMatching()
185188
'route' => null,
186189
'path' => null,
187190
'vars' => null,
191+
'matches' => null,
188192
'handler' => null,
189193
'before' => null,
190194
'after' => null,
@@ -245,6 +249,7 @@ public function testMatchMatchingExactMatch()
245249
'route' => 'r1',
246250
'path' => '/foo',
247251
'vars' => ['d' => 'D', 'c' => 'C'],
252+
'matches' => ['c' => 'C'],
248253
'handler' => $handler,
249254
'before' => null,
250255
'after' => null,
@@ -316,6 +321,7 @@ public function testMatchDynamicMatch()
316321
'route' => 'r1',
317322
'path' => '/foo/{bar}',
318323
'vars' => ['d' => 'D', 'bar' => 'i-am-bar', 'c' => 'C'],
324+
'matches' => ['bar' => 'i-am-bar', 'c' => 'C'],
319325
'handler' => $handler,
320326
'before' => null,
321327
'after' => null,
@@ -380,6 +386,7 @@ public function testMatchMatchingExactMatchNoQueryVars()
380386
'route' => 'r1',
381387
'path' => '/foo',
382388
'vars' => ['d' => 'D'],
389+
'matches' => [],
383390
'handler' => $handler,
384391
'before' => null,
385392
'after' => null,
@@ -453,6 +460,7 @@ public function testMatchMatchingNoQueryVarsMaintainPreviewVar()
453460
'preview_id' => '123',
454461
'preview_nonce' => 'abc',
455462
],
463+
'matches' => [],
456464
'handler' => $handler,
457465
'before' => null,
458466
'after' => null,
@@ -518,6 +526,7 @@ public function testMatchMatchingExactMatchCallableVars()
518526
'route' => 'r1',
519527
'path' => '/foo',
520528
'vars' => ['c'],
529+
'matches' => ['c' => 'C'],
521530
'handler' => $handler,
522531
'before' => null,
523532
'after' => null,

0 commit comments

Comments
 (0)