Open vanodevium opened 1 year ago
It's util to Clockwork. I created a DataSource to use on Clockwork
<?php
namespace App\DataSources;
use Clockwork\DataSource\DataSourceInterface;
use Clockwork\Request\Request;
use League\Route\Dispatcher;
use League\Route\Router;
// it is also necessary to obtain the array of routes
class AppRouter extends Router
{
public function getData(): array
{
return $this->routeCollector->getData();
}
}
class LeagueRouterDataSource implements DataSourceInterface
{
public function __construct(
protected AppRouter $router,
protected ServerRequestInterface $request
) {
}
public function resolve(Request $request)
{
$request->controller = $this->getController();
}
protected function getController()
{
$data = $this->router->getData();
$dispatcher = new Dispatcher($data);
$dispatcher->setStrategy($this->router->getStrategy());
$match = $dispatcher->dispatchAndReturn($this->request);
// or
// $match = $dispatcher->dispatch($method, $uri);
// dispatch method is public and not be necessary create the dispatchAndReturn method
$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";
}
}
Is there any way to only find the specific handler, but not handle it?
In simple way, this is original dispatchRequest()
But I wanna method which return only
$match
array, without processing. Something like: