Skip to content

Commit

Permalink
Merge pull request #1 from pennyphp/samsonasik-update-composer-patch
Browse files Browse the repository at this point in the history
Fix symfony dispatcher to use the RouteInfo class
  • Loading branch information
samsonasik committed Nov 29, 2015
2 parents 38e8198 + 3616a81 commit a741dcb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions app/Dispatcher/SymfonyDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@

namespace App\Dispatcher;

use Penny\Exception\MethodNotAllowed;
use Penny\Exception\RouteNotFound;
use FastRoute\Dispatcher;
use Penny\Exception\MethodNotAllowedException;
use Penny\Exception\RouteNotFoundException;
use Penny\Route\RouteInfo;
use Symfony\Component\HttpFoundation\Request;

class SymfonyDispatcher
{
private $router;
private $container;

public function __construct($router)
public function __construct($router, $container)
{
$this->router = $router;
$this->container = $container;
}

public function __invoke(Request $request)
{
$routeInfo = $this->router->dispatch($request->getMethod(), $request->getPathInfo());
switch ($routeInfo[0]) {
case \FastRoute\Dispatcher::NOT_FOUND:
throw new RouteNotFound;
case Dispatcher::NOT_FOUND:
throw new RouteNotFoundException;
break;
case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
throw new MethodNotAllowed;
case Dispatcher::METHOD_NOT_ALLOWED:
throw new MethodNotAllowedException;
break;
case \FastRoute\Dispatcher::FOUND:
return $routeInfo;
case Dispatcher::FOUND:
$controller = $this->container->get($routeInfo[1][0]);
$eventName = sprintf('%s.%s', strtolower($routeInfo[1][0]), $routeInfo[1][1]);

return new RouteInfo($eventName, [$controller, $routeInfo[1][1]], $routeInfo[2]);

break;
default:
throw new \Exception(null, 500);
break;
}
}
}
}
2 changes: 1 addition & 1 deletion config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
\App\EventListener\DispatcherExceptionListener::class => \DI\object(\App\EventListener\DispatcherExceptionListener::class)
->constructor(\DI\get('template')),
'dispatcher' => \DI\factory(function (\DI\Container $c) {
$dispatcher = new \App\Dispatcher\SymfonyDispatcher($c->get('router'));
$dispatcher = new \App\Dispatcher\SymfonyDispatcher($c->get('router'), $c);
return $dispatcher;
}),
'session' => \DI\factory(function (\DI\Container $c) {
Expand Down

0 comments on commit a741dcb

Please sign in to comment.