Skip to content

Commit 9ada1d3

Browse files
committed
Normalize the FastRoute static routes between multiple versions.
There were some changes in FastRoute that resulting in static routes array being slightly different (the method/uri were swapped in the keys). This resulted in some odd results between Lumen 5.1 and 5.2. This fix now normalizes the static routes from 5.1 to make the array the same as 5.2. The routes are then added properly when fetching the iterable routes. Reference issue dingo#868 and dingo#945.
1 parent 98d3d00 commit 9ada1d3

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

src/Routing/Adapter/Lumen.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,14 @@ public function getIterableRoutes($version = null)
263263
$routeData = $collector->getData();
264264

265265
// The first element in the array are the static routes that do not have any parameters.
266-
foreach ($routeData[0] as $uri => $route) {
267-
$iterable[$version][] = array_shift($route);
266+
foreach ($this->normalizeStaticRoutes($routeData[0]) as $method => $routes) {
267+
if ($method === 'HEAD') {
268+
continue;
269+
}
270+
271+
foreach ($routes as $route) {
272+
$iterable[$version][] = $route;
273+
}
268274
}
269275

270276
// The second element is the more complicated regex routes that have parameters.
@@ -284,6 +290,51 @@ public function getIterableRoutes($version = null)
284290
return new ArrayIterator($iterable);
285291
}
286292

293+
/**
294+
* Normalize the FastRoute static routes so they're the same across multiple versions.
295+
*
296+
* @param array $routes
297+
*
298+
* @return array
299+
*/
300+
protected function normalizeStaticRoutes(array $routes)
301+
{
302+
foreach (array_keys($routes) as $key) {
303+
// If any of the keys are an HTTP method then we are running on a newer version of
304+
// Lumen and FastRoute which means we can leave the routes as they are.
305+
if ($this->stringIsHttpMethod($key)) {
306+
return $routes;
307+
}
308+
}
309+
310+
$normalized = [];
311+
312+
// To normalize the routes we'll take the inner array which contains the routes method as the
313+
// key and make that the parent element on the array. We'll then add all routes for a
314+
// particular HTTP method as children of it by keying them to their URI.
315+
foreach ($routes as $uri => $value) {
316+
foreach ($value as $method => $route) {
317+
$normalized[$method][$uri] = $route;
318+
}
319+
}
320+
321+
return $normalized;
322+
}
323+
324+
/**
325+
* Determine if a string is an HTTP method.
326+
*
327+
* @param string $string
328+
*
329+
* @return bool
330+
*/
331+
protected function stringIsHttpMethod($string)
332+
{
333+
$methods = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
334+
335+
return in_array($string, $methods, true);
336+
}
337+
287338
/**
288339
* Set the routes on the adapter.
289340
*

0 commit comments

Comments
 (0)