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