Coder Social home page Coder Social logo

Dispatcher: handle and find about route HOT 1 OPEN

vanodevium avatar vanodevium commented on July 30, 2024 1
Dispatcher: handle and find

from route.

Comments (1)

ericktucto avatar ericktucto commented on July 30, 2024

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

from route.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.