diff --git a/src/Cortex/Route/Route.php b/src/Cortex/Route/Route.php index 0b6e17f..a3b963e 100644 --- a/src/Cortex/Route/Route.php +++ b/src/Cortex/Route/Route.php @@ -33,6 +33,7 @@ final class Route implements RouteInterface 'before' => null, 'after' => null, 'template' => null, + 'no_template' => null, 'path' => null, 'handler' => null, ]; diff --git a/src/Cortex/Router/MatchingResult.php b/src/Cortex/Router/MatchingResult.php index d924fce..a5ac65d 100644 --- a/src/Cortex/Router/MatchingResult.php +++ b/src/Cortex/Router/MatchingResult.php @@ -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 : ''; } /** diff --git a/src/Cortex/Router/ResultHandler.php b/src/Cortex/Router/ResultHandler.php index c8f0e21..a20b046 100644 --- a/src/Cortex/Router/ResultHandler.php +++ b/src/Cortex/Router/ResultHandler.php @@ -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; @@ -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) @@ -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', @@ -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) { diff --git a/src/Cortex/Router/Router.php b/src/Cortex/Router/Router.php index b7f1a90..f1e686a 100644 --- a/src/Cortex/Router/Router.php +++ b/src/Cortex/Router/Router.php @@ -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; } @@ -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]; @@ -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 @@ -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) @@ -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) @@ -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'], ]); }