8000 Merge pull request #86 from jasonlewis/feature/custom-fastroute-dispa… · Hydrane/tmp-laravel-framework@c5ffd82 · GitHub
[go: up one dir, main page]

Skip to content

Commit c5ffd82

Browse files
committed
Merge pull request laravel#86 from jasonlewis/feature/custom-fastroute-dispatcher
Allow developers to register their own FastRoute dispatcher with the app.
2 parents 811bcc1 + a3a0225 commit c5ffd82

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Application.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ class Application extends Container implements ApplicationContract, HttpKernelIn
128128
*/
129129
protected $ranServiceBinders = [];
130130

131+
/**
132+
* The FastRoute dispatcher.
133+
*
134+
* @var \FastRoute\Dispatcher
135+
*/
136+
protected $dispatcher;
137+
131138
/**
132139
* Create a new Lumen application instance.
133140
*
@@ -1098,13 +1105,24 @@ public function dispatch($request = null)
10981105
*/
10991106
protected function createDispatcher()
11001107
{
1101-
return \FastRoute\simpleDispatcher(function ($r) {
1108+
return $this->dispatcher ?: \FastRoute\simpleDispatcher(function ($r) {
11021109
foreach ($this->routes as $route) {
11031110
$r->addRoute($route['method'], $route['uri'], $route['action']);
11041111
}
11051112
});
11061113
}
11071114

1115+
/**
1116+
* Set the FastRoute dispatcher instance.
1117+
*
1118+
* @param \FastRoute\Dispatcher $dispatcher
1119+
* @return void
1120+
*/
1121+
public function setDispatcher(Dispatcher $dispatcher)
1122+
{
1123+
$this->dispatcher = $dispatcher;
1124+
}
1125+
11081126
/**
11091127
* Handle the response from the FastRoute dispatcher.
11101128
*

tests/FullApplicationTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,25 @@ public function testRegisterServiceProvider()
333333
$provider = new LumenTestServiceProvider($app);
334334
$app->register($provider);
335335
}
336+
337+
338+
public function testUsingCustomDispatcher()
339+
{
340+
$routes = new FastRoute\RouteCollector(new FastRoute\RouteParser\Std, new FastRoute\DataGenerator\GroupCountBased);
341+
342+
$routes->addRoute('GET', '/', [function () {
343+
return response('Hello World');
344+
}]);
345+
346+
$app = new Application;
347+
348+
$app->setDispatcher(new FastRoute\Dispatcher\GroupCountBased($routes->getData()));
349+
350+
$response = $app->handle(Request::create('/', 'GET'));
351+
352+
$this->assertEquals(200, $response->getStatusCode());
353+
$this->assertEquals('Hello World', $response->getContent());
354+
}
336355
}
337356

338357
class LumenTestService {}

0 commit comments

Comments
 (0)
0