Skip to content

Commit 65d1b84

Browse files
committed
Ensure preview works by persisting preview url var
Also adding 'cortex.matched-vars' filter to allow customize final vars array
1 parent cb4e856 commit 65d1b84

File tree

2 files changed

+107
-16
lines changed

2 files changed

+107
-16
lines changed

src/Cortex/Router/Router.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function match(UriInterface $uri, $httpMethod)
8787
return $this->results;
8888
}
8989

90-
if (! count($this->routes) || !$this->parseRoutes($uri, $httpMethod)) {
90+
if (! count($this->routes) || ! $this->parseRoutes($uri, $httpMethod)) {
9191
$this->results = new MatchingResult(['route' => null]);
9292

9393
return $this->results;
@@ -129,7 +129,7 @@ private function parseRoutes(UriInterface $uri, $httpMethod)
129129
/** @var \Brain\Cortex\Route\RouteInterface $route */
130130
foreach ($iterator as $route) {
131131
$route = $this->sanitizeRouteMethod($this->groups->mergeGroup($route), $httpMethod);
132-
if (!$this->validateRoute($route, $uri, $httpMethod)) {
132+
if (! $this->validateRoute($route, $uri, $httpMethod)) {
133133
continue;
134134
}
135135

@@ -159,7 +159,7 @@ private function parseRoutes(UriInterface $uri, $httpMethod)
159159
*/
160160
private function sanitizeRouteMethod(RouteInterface $route, $httpMethod)
161161
{
162-
if (empty($route['method']) || !(is_string($route['method']) || is_array($route['method']))) {
162+
if (empty($route['method']) || ! (is_string($route['method']) || is_array($route['method']))) {
163163
$route['method'] = $httpMethod;
164164
}
165165

@@ -227,7 +227,9 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface
227227
{
228228
is_null($route['merge_query_string']) and $route['merge_query_string'] = true;
229229
$merge = filter_var($route['merge_query_string'], FILTER_VALIDATE_BOOLEAN);
230-
$merge and $vars = array_merge($vars, $uri->vars());
230+
$vars = $merge
231+
? array_merge($vars, $uri->vars())
232+
: $this->ensurePreviewVar($vars, $uri, $route);
231233
$result = null;
232234
if (is_callable($route['vars'])) {
233235
$cb = $route['vars'];
@@ -240,14 +242,38 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface
240242
}
241243
}
242244

243-
return $result instanceof MatchingResult ? $result : new MatchingResult([
244-
'vars' => $vars,
245-
'route' => $route->id(),
246-
'path' => $route['path'],
247-
'handler' => $route['handler'],
248-
'before' => $route['before'],
249-
'after' => $route['after'],
250-
'template' => $route['template'],
251-
]);
245+
$vars = $this->ensurePreviewVar($vars, $uri, $route);
246+
$vars = apply_filters('cortex.matched-vars', $vars, $route, $uri);
247+
248+
return $result instanceof MatchingResult
249+
? $result
250+
: new MatchingResult([
251+
'vars' => (array) $vars,
252+
'route' => $route->id(),
253+
'path' => $route['path'],
254+
'handler' => $route['handler'],
255+
'before' => $route['before'],
256+
'after' => $route['after'],
257+
'template' => $route['template'],
258+
]);
259+
}
260+
261+
/**
262+
* We need this to ensure preview works.
263+
*
264+
* @param array $vars
265+
* @param \Brain\Cortex\Uri\UriInterface $uri
266+
* @param \Brain\Cortex\Route\RouteInterface $route
267+
* @return array
268+
*/
269+
private function ensurePreviewVar(array $vars, UriInterface $uri, RouteInterface $route)
270+
{
271+
$uriVars = $uri->vars();
272+
273+
if (! isset($vars['preview']) && isset($uriVars['preview']) && is_user_logged_in()) {
274+
$vars['preview'] = $uriVars['preview'];
275+
}
276+
277+
return $vars;
252278
}
253279
}

tests/src/Unit/Router/RouterTest.php

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Brain\Cortex\Router\Router;
2020
use Brain\Cortex\Tests\TestCase;
2121
use Brain\Cortex\Uri\UriInterface;
22+
use Brain\Monkey\Functions;
2223
use FastRoute\Dispatcher;
2324
use FastRoute\RouteCollector;
2425

@@ -222,7 +223,7 @@ public function testMatchMatchingExactMatch()
222223
$uri->shouldReceive('scheme')->andReturn('http');
223224
$uri->shouldReceive('host')->andReturn('example.com');
224225
$uri->shouldReceive('path')->andReturn('foo');
225-
$uri->shouldReceive('vars')->once()->andReturn(['c' => 'C']);
226+
$uri->shouldReceive('vars')->atLeast()->once()->andReturn(['c' => 'C']);
226227
$uri->shouldReceive('chunks')->andReturn(['foo']);
227228

228229
$collector = \Mockery::mock(RouteCollector::class);
@@ -282,7 +283,7 @@ public function testMatchDynamicMatch()
282283
$uri->shouldReceive('scheme')->andReturn('http');
283284
$uri->shouldReceive('host')->andReturn('example.com');
284285
$uri->shouldReceive('path')->andReturn('foo/i-am-bar');
285-
$uri->shouldReceive('vars')->once()->andReturn(['c' => 'C']);
286+
$uri->shouldReceive('vars')->atLeast()->once()->andReturn(['c' => 'C']);
286287
$uri->shouldReceive('chunks')->andReturn(['foo', 'meh']);
287288

288289
$collector = \Mockery::mock(RouteCollector::class);
@@ -352,6 +353,7 @@ public function testMatchMatchingExactMatchNoQueryVars()
352353
$uri->shouldReceive('host')->andReturn('example.com');
353354
$uri->shouldReceive('path')->andReturn('foo');
354355
$uri->shouldReceive('chunks')->andReturn(['foo']);
356+
$uri->shouldReceive('vars')->andReturn(['foo' => 'no-way']);
355357

356358
$collector = \Mockery::mock(RouteCollector::class);
357359
$collector->shouldReceive('addRoute')->never();
@@ -386,6 +388,69 @@ public function testMatchMatchingExactMatchNoQueryVars()
386388
assertSame($expected, $data);
387389
}
388390

391+
public function testMatchMatchingNoQueryVarsMaintainPreviewVar()
392+
{
393+
Functions::when('is_user_logged_in')->justReturn(true);
394+
395+
$handler = function () {
396+
return func_get_args();
397+
};
398+
399+
$route = new Route([
400+
'id' => 'r1',
401+
'path' => '/foo',
402+
'handler' => $handler,
403+
'vars' => ['d' => 'D'],
404+
'method' => 'POST',
405+
'merge_query_string' => false,
406+
]);
407+
$routes = new PriorityRouteCollection();
408+
409+
$routes->addRoute($route);
410+
411+
$groups = \Mockery::mock(GroupCollectionInterface::class);
412+
$groups->shouldReceive('mergeGroup')->once()->with($route)->andReturn($route);
413+
414+
$uri = \Mockery::mock(UriInterface::class);
415+
$uri->shouldReceive('scheme')->andReturn('http');
416+
$uri->shouldReceive('host')->andReturn('example.com');
417+
$uri->shouldReceive('path')->andReturn('foo');
418+
$uri->shouldReceive('chunks')->andReturn(['foo']);
419+
$uri->shouldReceive('vars')->andReturn(['foo' => 'no-way', 'preview' => 1]);
420+
421+
$collector = \Mockery::mock(RouteCollector::class);
422+
$collector->shouldReceive('addRoute')->never();
423+
424+
$dispatcher = \Mockery::mock(Dispatcher::class);
425+
$dispatcher->shouldReceive('dispatch')->never();
426+
427+
$factory = function (array $args) use ($dispatcher) {
428+
assertSame($args, ['foo' => 'bar']);
429+
430+
return $dispatcher;
431+
};
432+
433+
$router = new Router($routes, $groups, $collector, $factory);
434+
$result = $router->match($uri, 'POST');
435+
436+
$expected = [
437+
'route' => 'r1',
438+
'path' => '/foo',
439+
'vars' => ['preview' => 1, 'd' => 'D'],
440+
'handler' => $handler,
441+
'before' => null,
442+
'after' => null,
443+
'template' => null,
444+
];
445+
446+
$proxy = new Proxy($result);
447+
/** @noinspection PhpUndefinedFieldInspection */
448+
$data = $proxy->data;
449+
450+
assertTrue($result->matched());
451+
assertSame($expected, $data);
452+
}
453+
389454
public function testMatchMatchingExactMatchCallableVars()
390455
{
391456
$handler = function () {
@@ -412,7 +477,7 @@ public function testMatchMatchingExactMatchCallableVars()
412477
$uri->shouldReceive('scheme')->andReturn('http');
413478
$uri->shouldReceive('host')->andReturn('example.com');
414479
$uri->shouldReceive('path')->andReturn('foo');
415-
$uri->shouldReceive('vars')->once()->andReturn(['c' => 'C']);
480+
$uri->shouldReceive('vars')->atLeast()->once()->andReturn(['c' => 'C']);
416481
$uri->shouldReceive('chunks')->andReturn(['foo']);
417482

418483
$collector = \Mockery::mock(RouteCollector::class);

0 commit comments

Comments
 (0)