Skip to content

Commit

Permalink
Merge pull request #95 from wdalmut/feature/routeinfo-direct
Browse files Browse the repository at this point in the history
Drop static RouteInfo matched method
  • Loading branch information
Gianluca Arbezzano committed Oct 20, 2015
2 parents be2b8e6 + b636f99 commit d2cf62d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
15 changes: 7 additions & 8 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@
use Exception;
use ReflectionClass;
use FastRoute\Dispatcher as BaseDispatcher;
use FastRoute\Dispatcher as FastRouterDispatcherInterface;
use Penny\Route\FastPsr7RouteInfo;
use Penny\Exception\MethodNotAllowed;
use Penny\Exception\RouteNotFound;
use Psr\Http\Message\RequestInterface;
use Penny\Route\RouteInfo;

class Dispatcher
{
/**
* Inner dispatcher.
*
* @var FastRouterDispatcherInterface
* @var BaseDispatcher
*/
private $router;

/**
* Class constructor with required FastRoute dispatcher implementation.
*
* @param FastRouterDispatcherInterface $router Inner router (based on Nikic FastRouter).
* @param BaseDispatcher $router Inner router (based on Nikic FastRouter).
*/
public function __construct(FastRouterDispatcherInterface $router, ContainerInterface $container)
public function __construct(BaseDispatcher $router, ContainerInterface $container)
{
$this->router = $router;
$this->container = $container;
Expand Down Expand Up @@ -62,10 +61,10 @@ public function __invoke(RequestInterface $request)
case BaseDispatcher::FOUND:
$controller = $this->container->get($routeInfo[1][0]);
$method = $routeInfo[1][1];
$function = (new ReflectionClass($controller))->getShortName();
$function = strtolower((new ReflectionClass($controller))->getShortName());
$eventName = "{$function}.{$method}"; // this improve ~1us

$eventName = sprintf('%s.%s', strtolower($function), $method);
$routeInfo = FastPsr7RouteInfo::matched($eventName, [$controller, $routeInfo[1][1]], $routeInfo[2]);
$routeInfo = new RouteInfo($eventName, [$controller, $routeInfo[1][1]], $routeInfo[2]);
return $routeInfo;
default:
throw new Exception(null, 500);
Expand Down
12 changes: 5 additions & 7 deletions src/Route/FastPsr7RouteInfo.php → src/Route/RouteInfo.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?php
namespace Penny\Route;

class FastPsr7RouteInfo implements RouteInfoInterface
class RouteInfo implements RouteInfoInterface
{
private $name;
private $callable;
private $params;

public static function matched($name, callable $callable, $params = [])
public function __construct($name, callable $callable, $params = [])
{
$obj = new self();
$obj->name = $name;
$obj->callable = $callable;
$obj->params = $params;
return $obj;
$this->name = $name;
$this->callable = $callable;
$this->params = $params;
}

public function getName()
Expand Down
1 change: 0 additions & 1 deletion src/Route/RouteInfoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

interface RouteInfoInterface
{
public static function matched($name, callable $callable, $params);
public function getName();
public function getCallable();
public function getParams();
Expand Down
13 changes: 4 additions & 9 deletions tests/Route/RouteInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@
namespace PennyTest\Route;

use PHPUnit_Framework_TestCase;
use Penny\Route\FastPsr7RouteInfo;
use Penny\Route\RouteInfoInterface;
use TestApp\Controller\IndexController;
use Penny\Route\RouteInfo;

class RouteInfoTest extends PHPUnit_Framework_TestCase
{
public function testRouteInfoImplementInterface()
{
$routeInfo = new FastPsr7RouteInfo();
$routeInfo = new RouteInfo('', function(){}, []);
$this->assertInstanceOf(RouteInfoInterface::class, $routeInfo);
}

public function testMatched()
{
$fastRouteInfo = [
[],
[new IndexController(), 'index'],
["id" => 5]
];
$routeInfo = FastPsr7RouteInfo::matched("index.try", $fastRouteInfo[1], $fastRouteInfo[2]);
$this->assertSame("index.try", $routeInfo->getName());
$routeInfo = new RouteInfo('index.try', [new IndexController(), 'index'], ['id' => 5]);
$this->assertSame('index.try', $routeInfo->getName());
}
}
6 changes: 3 additions & 3 deletions tests/Utils/FastSymfonyDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace PennyTest\Utils;

use Symfony\Component\HttpFoundation\Request;
use Penny\Route\FastPsr7RouteInfo;
use ReflectionClass;
use Penny\Route\RouteInfo;
use Symfony\Component\HttpFoundation\Request;

class FastSymfonyDispatcher
{
Expand Down Expand Up @@ -37,7 +37,7 @@ public function __invoke(Request $request)
$callback = function($controller, $fastRouteInfo) {
return call_user_func([$controller, $fastRouteInfo[1][1]]);
};
$routeInfo = FastPsr7RouteInfo::matched($eventName, $callback($controller, $fastRouteInfo[1][1]), $fastRouteInfo[2]);
$routeInfo = new RouteInfo($eventName, $callback($controller, $fastRouteInfo[1][1]), $fastRouteInfo[2]);
return $routeInfo;
break;
default:
Expand Down

0 comments on commit d2cf62d

Please sign in to comment.