Skip to content

Commit

Permalink
Enable disabling templates from routes
Browse files Browse the repository at this point in the history
Setting route option `'template'` to false, means that WordPress will
load no template. Moreover, setting `'no_template'` route option to
`true` do same thing, and takes precedence on any `'template'` option
value.
  • Loading branch information
gmazzap committed Jul 6, 2016
1 parent 4ad6e3a commit 4e64f15
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Cortex/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class Route implements RouteInterface
'before' => null,
'after' => null,
'template' => null,
'no_template' => null,
'path' => null,
'handler' => null,
];
Expand Down
4 changes: 3 additions & 1 deletion src/Cortex/Router/MatchingResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public function matched()
*/
public function template()
{
return is_string($this->data['template']) ? $this->data['template'] : '';
$template = $this->data['template'];

return (is_string($template) || $template === false) ? $template : '';
}

/**
Expand Down
31 changes: 21 additions & 10 deletions src/Cortex/Router/ResultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class ResultHandler implements ResultHandlerInterface
*/
public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
{
/** @var \Brain\Cortex\Router\MatchingResult $result */
$result = apply_filters('cortex.match.done', $result, $wp, $doParseRequest);
$handlerResult = $doParseRequest;

Expand Down Expand Up @@ -62,7 +63,7 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
}

/**
* @param mixed $handler
* @param mixed $handler
* @return callable|null
*/
private function buildCallback($handler)
Expand All @@ -86,16 +87,26 @@ private function buildCallback($handler)
*/
private function setTemplate($template)
{
$ext = apply_filters('cortex.default-template-extension', 'php');
pathinfo($template, PATHINFO_EXTENSION) or $template .= '.'.ltrim($ext, '.');
$template = is_file($template) ? $template : locate_template([$template], false);
if (! $template) {
if (is_string($template)) {
$ext = apply_filters('cortex.default-template-extension', 'php');
pathinfo($template, PATHINFO_EXTENSION) or $template .= '.'.ltrim($ext, '.');
$template = is_file($template) ? $template : locate_template([$template], false);
$template or $template = null;
}

// Allow template to be a file path or `false`, which will actually disable template
if (! is_file($template) && $template !== false) {
return;
}

$setter = function () use ($template) {
return $template;
};
// If template is `false`, we return `true` on `"{$type}_template"`
// to speed up `template-loader.php`
$template_setter = $template
? function () use ($template) {
return $template;
}
: '__return_true';


$types = [
'404',
Expand All @@ -116,8 +127,8 @@ private function setTemplate($template)
'index',
];

array_walk($types, function ($type) use ($setter) {
add_filter("{$type}_template", $setter);
array_walk($types, function ($type) use ($template_setter) {
add_filter("{$type}_template", $template_setter);
});

add_filter('template_include', function () use ($template) {
Expand Down
19 changes: 10 additions & 9 deletions src/Cortex/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct(
) {
$this->groups = $groups;
$this->routes = $routes;
$this->collector = $collector ?: new RouteCollector(new Std(), new DefDataGenerator());
$this->collector = $collector ? : new RouteCollector(new Std(), new DefDataGenerator());
$this->dispatcherFactory = $dispatcherFactory;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ public function match(UriInterface $uri, $httpMethod)
unset($this->collector);

$uriPath = '/'.trim($uri->path(), '/');
$routeInfo = $dispatcher->dispatch($httpMethod, $uriPath ?: '/');
$routeInfo = $dispatcher->dispatch($httpMethod, $uriPath ? : '/');
if ($routeInfo[0] === Dispatcher::FOUND) {
$route = $this->parsedRoutes[$routeInfo[1]];
$vars = $routeInfo[2];
Expand Down Expand Up @@ -189,7 +189,7 @@ private function validateRoute(RouteInterface $route, UriInterface $uri, $httpMe
if (count($uri->chunks()) !== (substr_count($path, '/') + 1)) {
return false;
}
$method = (array) $route['method'];
$method = (array)$route['method'];
$handler = $route['handler'];

return
Expand All @@ -201,7 +201,7 @@ private function validateRoute(RouteInterface $route, UriInterface $uri, $httpMe
}

/**
* @param array $data
* @param array $data
* @return \FastRoute\Dispatcher
*/
private function buildDispatcher(array $data)
Expand All @@ -218,9 +218,9 @@ private function buildDispatcher(array $data)
}

/**
* @param \Brain\Cortex\Route\RouteInterface $route
* @param array $vars
* @param \Brain\Cortex\Uri\UriInterface $uri
* @param \Brain\Cortex\Route\RouteInterface $route
* @param array $vars
* @param \Brain\Cortex\Uri\UriInterface $uri
* @return \Brain\Cortex\Router\MatchingResult
*/
private function finalizeRoute(RouteInterface $route, array $vars, UriInterface $uri)
Expand Down Expand Up @@ -256,15 +256,16 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface

$vars = $this->ensurePreviewVars($vars, $uriVars);
$vars = apply_filters('cortex.matched-vars', $vars, $route, $uri);
$noTemplate = filter_var($route['no_template'], FILTER_VALIDATE_BOOLEAN);

return new MatchingResult([
'vars' => (array) $vars,
'vars' => (array)$vars,
'route' => $route->id(),
'path' => $route['path'],
'handler' => $route['handler'],
'before' => $route['before'],
'after' => $route['after'],
'template' => $route['template'],
'template' => $noTemplate ? false : $route['template'],
]);
}

Expand Down

0 comments on commit 4e64f15

Please sign in to comment.