<?php
namespace Koselig\Routing;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Koselig\Models\Post;
use Koselig\Support\Wordpress;
use ReflectionFunction;
class PageRoute extends Route
{
public function __construct($methods, $users, $action)
{
parent::__construct($methods, $users, $action);
$this->uri = 'page/' . $this->uri();
}
public function matches(Request $request, $includingMethod = true)
{
$id = Wordpress::id();
if (!$id) {
return false;
}
if (!empty($this->getAction()['domain']) && !Wordpress::multisite($this->getAction()['domain'])) {
return false;
}
return $this->uri === $id;
}
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();
}
}