route
route copied to clipboard
New method, getData
With it's method allow get info the route.
Allow creating to DataSource to Clockwork
BEFORE
AFTER
LeagueRouterDataSource
<?php
namespace App\DataSources;
use Clockwork\DataSource\DataSourceInterface;
use Clockwork\Request\Request;
use League\Route\Dispatcher;
use League\Route\Router;
class LeagueRouterDataSource implements DataSourceInterface
{
public function __construct(
protected Router $router,
protected ServerRequestInterface $request
) {
}
public function resolve(Request $request)
{
$request->controller = $this->getController($request);
}
protected function getController(Request $request)
{
// using getData()
$data = $this->router->getData();
$dispatcher = new Dispatcher($data);
$dispatcher->setStrategy($this->router->getStrategy());
$match = $dispatcher->dispatch($request->method, $request->uri);
$callable = $match[1]->getCallable();
// if use magic method __invoke
if (is_object($callable)) {
return get_class($callable);
}
// if use syntax [controller, method] or "controller::method"
if (is_array($callable)) {
[$controller, $method] = $callable;
return get_class($controller) . "@{$method}";
}
// if use anonymous function
return "Closure";
}
}