<?php
namespace Koselig\Routing;
use Illuminate\Routing\Route;
use Koselig\Http\Request;
use Koselig\Models\Post;
use Koselig\Support\Wordpress;
use ReflectionFunction;
class TemplateRoute extends Route
{
public function __construct($methods, $types, $action)
{
parent::__construct($methods, $types, $action);
$this->uri = 'template/' . parent::uri();
}
public function matches(Request $request, $includingMethod = true)
{
$post = $request->post();
if (!$post) {
return false;
}
$slug = $post->getMeta('_wp_page_template');
if (!empty($this->getAction()['domain']) && !Wordpress::multisite($this->getAction()['domain'])) {
return false;
}
return $this->uri === $slug;
}
protected function runCallable()
{
$function = new ReflectionFunction($this->action['uses']);
$params = $function->getParameters();
foreach ($params as $param) {
if ($param->getClass()
&& ($param->getClass()->isSubclassOf(Post::class) || $param->getClass()->getName() === Post::class)) {
$builder = $param->getClass()->getMethod('query')->invoke(null);
$post = $builder->find(Wordpress::id());
$this->setParameter($param->getName(), $post);
}
}
return parent::runCallable();
}
}