Skip to content

Commit 1cd3bf9

Browse files
committed
Remove all global middleware as its run at the request middleware level. This was causing some global middlewares to be executed twice. Cheers to @kylestev for the debugging.
Signed-off-by: Jason Lewis <[email protected]>
1 parent f081392 commit 1cd3bf9

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/Routing/Adapter/Lumen.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ class Lumen implements Adapter
5050
*/
5151
protected $routes = [];
5252

53+
/**
54+
* Indicates if the middleware has been removed from the application instance.
55+
*
56+
* @var bool
57+
*/
58+
protected $middlewareRemoved = false;
59+
5360
/**
5461
* Create a new lumen adapter instance.
5562
*
@@ -82,7 +89,7 @@ public function dispatch(Request $request, $version)
8289
throw new UnknownVersionException;
8390
}
8491

85-
$this->removeRequestMiddlewareFromApp();
92+
$this->removeMiddlewareFromApp();
8693

8794
$routes = $this->routes[$version];
8895

@@ -201,24 +208,26 @@ protected function createRouteCollections(array $versions)
201208
}
202209

203210
/**
204-
* Remove the request middleware from the application instance so we don't
205-
* end up in a continuous loop.
211+
* Remove the global application middleware as it's run from this packages
212+
* Request middleware. Lumen runs middleware later in its life cycle
213+
* which results in some middleware being executed twice.
206214
*
207215
* @return void
208216
*/
209-
protected function removeRequestMiddlewareFromApp()
217+
protected function removeMiddlewareFromApp()
210218
{
219+
if ($this->middlewareRemoved) {
220+
return;
221+
}
222+
223+
$this->middlewareRemoved = true;
224+
211225
$reflection = new ReflectionClass($this->app);
212226
$property = $reflection->getProperty('middleware');
213227
$property->setAccessible(true);
214228

215-
$middleware = $property->getValue($this->app);
216-
217-
if (($key = array_search('Dingo\Api\Http\Middleware\Request', $middleware)) !== false) {
218-
unset($middleware[$key]);
219-
}
229+
$property->setValue($this->app, []);
220230

221-
$property->setValue($this->app, $middleware);
222231
$property->setAccessible(false);
223232
}
224233

0 commit comments

Comments
 (0)