Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatcher: handle and find #328

Open
vanodevium opened this issue Jan 8, 2023 · 6 comments
Open

Dispatcher: handle and find #328

vanodevium opened this issue Jan 8, 2023 · 6 comments
Assignees
Labels

Comments

@vanodevium
Copy link

Is there any way to only find the specific handler, but not handle it?

In simple way, this is original dispatchRequest()

    public function dispatchRequest(ServerRequestInterface $request): ResponseInterface
    {
        $method = $request->getMethod();
        $uri    = $request->getUri()->getPath();
        $match  = $this->dispatch($method, $uri);

        switch ($match[0]) {
            case FastRoute::NOT_FOUND:
                $this->setNotFoundDecoratorMiddleware();
                break;
            case FastRoute::METHOD_NOT_ALLOWED:
                $allowed = (array) $match[1];
                $this->setMethodNotAllowedDecoratorMiddleware($allowed);
                break;
            case FastRoute::FOUND:
                $route = $this->ensureHandlerIsRoute($match[1], $method, $uri)->setVars($match[2]);

                if ($this->isExtraConditionMatch($route, $request)) {
                    $this->setFoundMiddleware($route);
                    $request = $this->requestWithRouteAttributes($request, $route);
                    break;
                }

                $this->setNotFoundDecoratorMiddleware();
                break;
        }

        return $this->handle($request);
    }

But I wanna method which return only $match array, without processing. Something like:

    public function dispatchAndReturn(ServerRequestInterface $request): array
    {
        $method = $request->getMethod();
        $uri    = $request->getUri()->getPath();
        return $this->dispatch($method, $uri);
    }
@ericktucto
Copy link

ericktucto commented May 15, 2023

It's util to Clockwork. I created a DataSource to use on Clockwork

BEFORE

Captura desde 2023-05-14 20-58-06

AFTER

github

LeagueRouterDataSource

<?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";
  }
}

@philipobenito
Copy link
Member

I know this is very late, but what is the intended use case for this?

I'll close the issue for now, but will re-open and consider for 6.1 release if a good use case is provided

@vanodevium
Copy link
Author

The main aim of this function is the ability to get meta data about routing for the dynamic testing purposes

@philipobenito
Copy link
Member

@vanodevium can you expand a little?

For example, would an array of all of the route objects be useful? Or are you looking for something key => value based? Because of how the groups, and pass through to FastRoute works, it'd be handy to have as much info as possible about what you're trying to achieve so I can come up with something that will be useful for this but also maybe in other ways.

@philipobenito philipobenito reopened this Nov 10, 2024
@philipobenito philipobenito self-assigned this Nov 10, 2024
@philipobenito
Copy link
Member

I'm asking mainly because we also have #330 which seems like it might be similar.

@vanodevium
Copy link
Author

Imho, for my case, there would be awesome to take concrete handler for concrete route string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants