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

Fixes #96 , implements ClassName::class prefix for event name #147

Merged
merged 3 commits into from
Nov 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ If a route matches, the corresponding callback is invoked, in this case the call
$r->addRoute('GET', '/', ['PennyApp\Controller\IndexController', 'index']);
```

At this point the system triggers an event called `indexcontroller.index` with zero priority and execute the route callback.
At this point the system triggers an event called `PennyApp\Controller\IndexController.index` with zero priority and execute the route callback.

All listeners attached after and before it will be called correctly until the framework returns response,
if an exception is thrown it will trigger an event named `indexcontroller.index_error`.
if an exception is thrown it will trigger an event named `PennyApp\Controller\IndexController.index_error`.

The most common way to manage all exceptions is:

Expand Down
5 changes: 3 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ private function handleResponse(
EventInterface $event,
RouteInfoInterface $routeInfo
) {
$eventManager->attach($event->getName(), function ($event) use ($routeInfo) {
$eventName = $event->getName();
$eventManager->attach($eventName, function ($event) use ($routeInfo) {
$event->setResponse(call_user_func_array(
$routeInfo->getCallable(),
[$event->getRequest(), $event->getResponse()] + $routeInfo->getParams()
Expand All @@ -172,7 +173,7 @@ private function handleResponse(
try {
$eventManager->trigger($event);
} catch (Exception $exception) {
$this->triggerWithException($eventManager, $event, $routeInfo->getName().'_error', $exception);
$this->triggerWithException($eventManager, $event, $eventName.'_error', $exception);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function processRouteInfo(array $dispatch)
$controller = $this->container->get($dispatch[1][0]);
$method = $dispatch[1][1];
$params = $dispatch[2];
$function = strtolower((new ReflectionClass($controller))->getShortName());
$function = (new ReflectionClass($controller))->name;
$eventName = "{$function}.{$method}"; // this improve ~1us

return new RouteInfo($eventName, [$controller, $method], $params);
Expand Down
19 changes: 10 additions & 9 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Penny\Exception\RouteNotFoundException;
use Penny\Config\Loader;
use PHPUnit_Framework_TestCase;
use TestApp\Controller\IndexController;
use stdClass;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;
Expand All @@ -26,10 +27,10 @@ public function setUp()
{
$config = Loader::load();
$config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', ['TestApp\Controller\IndexController', 'index']);
$r->addRoute('GET', '/{id:\d+}', ['TestApp\Controller\IndexController', 'getSingle']);
$r->addRoute('GET', '/fail', ['TestApp\Controller\IndexController', 'failed']);
$r->addRoute('GET', '/dummy', ['TestApp\Controller\IndexController', 'dummy']);
$r->addRoute('GET', '/', [IndexController::class, 'index']);
$r->addRoute('GET', '/{id:\d+}', [IndexController::class, 'getSingle']);
$r->addRoute('GET', '/fail', [IndexController::class, 'failed']);
$r->addRoute('GET', '/dummy', [IndexController::class, 'dummy']);
});

$this->app = new App(Container\PHPDiFactory::buildContainer($config));
Expand Down Expand Up @@ -101,7 +102,7 @@ public function testEventPostExecuted()
->withMethod('GET');
$response = new Response();

$this->app->getContainer()->get('event_manager')->attach('indexcontroller.index', function ($e) {
$this->app->getContainer()->get('event_manager')->attach(IndexController::class.'.index', function ($e) {
$response = $e->getResponse();
$response->getBody()->write("I'm very happy!");
$e->setResponse($response);
Expand All @@ -119,7 +120,7 @@ public function testEventPreExecuted()
->withMethod('GET');
$response = new Response();

$this->app->getContainer()->get('event_manager')->attach('indexcontroller.index', function ($e) {
$this->app->getContainer()->get('event_manager')->attach(IndexController::class.'.index', function ($e) {
$response = $e->getResponse();
$response->getBody()->write('This is');
$e->setResponse($response);
Expand All @@ -139,7 +140,7 @@ public function testEventPreThrowExceptionIsTrigger()
$response = new Response();
$count = 0;

$this->app->getContainer()->get('event_manager')->attach('indexcontroller.dummy_error', function ($e) use (&$count) {
$this->app->getContainer()->get('event_manager')->attach(IndexController::class.'.dummy_error', function ($e) use (&$count) {
$count = &$count + 1;
throw $e->getException();
}, 10);
Expand Down Expand Up @@ -181,7 +182,7 @@ public function testDispatcherShouldBeCallable()

$config = Loader::load();
$config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', ['TestApp\Controller\IndexController', 'index']);
$r->addRoute('GET', '/', [IndexController::class, 'index']);
});
$config['dispatcher'] = new \StdClass();

Expand Down Expand Up @@ -246,7 +247,7 @@ public function testBootstrapEventTriggered()
{
$config = Loader::load();
$config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', ['TestApp\Controller\IndexController', 'index']);
$r->addRoute('GET', '/', [IndexController::class, 'index']);
});
$this->app = new App(Container\PHPDiFactory::buildContainer($config));
$this->app->getContainer()
Expand Down
7 changes: 4 additions & 3 deletions tests/EventFlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Penny\App;
use Penny\Container;
use PHPUnit_Framework_TestCase;
use TestApp\Controller\IndexController;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;
use Zend\Diactoros\Uri;
Expand All @@ -19,7 +20,7 @@ public function setUp()
{
$config = Loader::load();
$config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', ['TestApp\Controller\IndexController', 'index']);
$r->addRoute('GET', '/', [IndexController::class, 'index']);
});

$this->app = new App(Container\PHPDiFactory::buildContainer($config));
Expand All @@ -32,13 +33,13 @@ public function testStopEventFlow()
->withMethod('GET');
$response = new Response();

$this->app->getContainer()->get('event_manager')->attach('indexcontroller.index', function ($e) {
$this->app->getContainer()->get('event_manager')->attach(IndexController::class.'.index', function ($e) {
$response = $e->getResponse();
$response = $response->withStatus(201);
$e->setResponse($response);
}, 100);

$this->app->getContainer()->get('event_manager')->attach('indexcontroller.index', function ($e) {
$this->app->getContainer()->get('event_manager')->attach(IndexController::class.'.index', function ($e) {
$response = $e->getResponse();
$response = $response->withStatus(205);
$e->setResponse($response);
Expand Down