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

New method, getData #332

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open

Conversation

ericktucto
Copy link

With it's method allow get info the route.

Allow creating to DataSource to 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;

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

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

Successfully merging this pull request may close these issues.

None yet

1 participant