Skip to content

Commit

Permalink
Allow a callback as template
Browse files Browse the repository at this point in the history
See #22
  • Loading branch information
gmazzap committed Dec 24, 2017
1 parent 76e0433 commit 4c9cf21
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/Cortex/Router/MatchingResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ public function matched()
}

/**
* @return string
* @return string|bool|callable
*/
public function template()
{
$template = $this->data['template'];

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

/**
Expand Down
27 changes: 16 additions & 11 deletions src/Cortex/Router/ResultHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
$result = apply_filters('cortex.match.done', $result, $wp, $doParseRequest);
$handlerResult = $doParseRequest;

if (! $result instanceof MatchingResult) {
if (!$result instanceof MatchingResult) {
return $result;
}

Expand All @@ -40,23 +40,28 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
$before = $this->buildCallback($result->beforeHandler());
$after = $this->buildCallback($result->afterHandler());
$template = $result->template();
(is_string($template)) or $template = '';
$vars = $result->vars();
$matches = $result->matches();

if (is_callable($template)) {
$template = $template($vars, $wp, $matches);
}

(is_string($template) || $template === 'false') or $template = '';

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

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);
$this->setTemplate($template);

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

is_bool($handlerResult) and $doParseRequest = $handlerResult;
$doParseRequest = apply_filters('cortex.do-parse-request', $doParseRequest);

if (! $doParseRequest) {
if (!$doParseRequest) {
remove_filter('template_redirect', 'redirect_canonical');

return false;
Expand All @@ -79,7 +84,7 @@ private function buildCallback($handler)
$built = $handler;
}

if (! $built && $handler instanceof ControllerInterface) {
if (!$built && $handler instanceof ControllerInterface) {
$built = function (array $vars, \WP $wp, $template) use ($handler) {
return $handler->run($vars, $wp, $template);
};
Expand All @@ -93,14 +98,14 @@ private function buildCallback($handler)
*/
private function setTemplate($template)
{
if (is_string($template)) {
if (is_string($template) && $template) {
$ext = apply_filters('cortex.default-template-extension', 'php');
pathinfo($template, PATHINFO_EXTENSION) or $template .= '.'.ltrim($ext, '.');
pathinfo($template, PATHINFO_EXTENSION) or $template .= '.' . ltrim($ext, '.');
$template = is_file($template) ? $template : locate_template([$template], false);
$template or $template = null;
$template or $template = '';
}
if (is_null($template)) {

if ($template === '' || !(is_string($template) || $template === false)) {
return;
}

Expand All @@ -124,7 +129,7 @@ private function setTemplate($template)
];

$returnTemplate = function () use ($template) {
current_filter('template_include') and remove_all_filters('template_include');
current_filter() === 'template_include' and remove_all_filters('template_include');

return $template;
};
Expand Down

0 comments on commit 4c9cf21

Please sign in to comment.