Skip to content

Commit 6ccbbd8

Browse files
committed
Use the regular routes for the UrlGenerator and implemented an iterable routes method so we can get a normalized, iterable array of routes from each adapter.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 437db85 commit 6ccbbd8

File tree

6 files changed

+53
-8
lines changed

6 files changed

+53
-8
lines changed

src/Contract/Routing/Adapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public function addRoute(array $methods, array $versions, $uri, $action);
4747
*/
4848
public function getRoutes($version = null);
4949

50+
/**
51+
* Get a normalized iterable set of routes. Top level key must be a version with each
52+
* version containing iterable routes that can be consumed by the adapter.
53+
*
54+
* @param string $version
55+
*
56+
* @return mixed
57+
*/
58+
public function getIterableRoutes($version = null);
59+
5060
/**
5161
* Set the routes on the adapter.
5262
*

src/Provider/ApiServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ protected function registerUrlGenerator()
205205
$this->app->singleton('api.url', function ($app) {
206206
$url = new UrlGenerator($app['request']);
207207

208-
$url->setRouteCollections($app['api.router']->getAdapterRoutes());
208+
$url->setRouteCollections($app['api.router']->getRoutes());
209209

210210
return $url;
211211
});

src/Routing/Adapter/Laravel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public function getRoutes($version = null)
151151
return $this->routes;
152152
}
153153

154+
public function getIterableRoutes($version = null)
155+
{
156+
return $this->getRoutes($version);
157+
}
158+
154159
/**
155160
* Set the routes on the adapter.
156161
*

src/Routing/Adapter/Lumen.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dingo\Api\Routing\Adapter;
44

5+
use ArrayIterator;
56
use ReflectionClass;
67
use FastRoute\Dispatcher;
78
use FastRoute\RouteParser;
@@ -102,11 +103,11 @@ public function dispatch(Request $request, $version)
102103
*/
103104
public function getRouteProperties($route, Request $request)
104105
{
105-
$uri = ltrim($request->getRequestUri(), '/');
106-
$methods = (array) $request->getMethod();
107-
$action = $route[1];
106+
$uri = ltrim(isset($route['uri']) ? $route['uri'] : $request->getRequestUri(), '/');
107+
$methods = isset($route['methods']) ? $route['methods'] : (array) $request->getMethod();
108+
$action = (isset($route[1]) && is_array($route[1])) ? $route[1] : $route;
108109

109-
if ($request->getMethod() === 'GET') {
110+
if ($request->getMethod() === 'GET' && ! in_array('HEAD', $methods)) {
110111
$methods[] = 'HEAD';
111112
}
112113

@@ -219,6 +220,29 @@ public function getRoutes($version = null)
219220
return $this->routes;
220221
}
221222

223+
/**
224+
* Get routes in an iterable form.
225+
*
226+
* @param string $version
227+
*
228+
* @return \ArrayIterator
229+
*/
230+
public function getIterableRoutes($version = null)
231+
{
232+
$itrerable = [];
233+
234+
foreach ($this->getRoutes($version) as $version => $routes) {
235+
foreach ($routes->getData()[0] as $uri => $route) {
236+
$methods = array_keys($route);
237+
$route = array_shift($route);
238+
239+
$iterable[$version][$uri] = array_merge($route, compact('uri', 'methods'));
240+
}
241+
}
242+
243+
return new ArrayIterator($iterable);
244+
}
245+
222246
/**
223247
* Set the routes on the adapter.
224248
*

src/Routing/Router.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ public function getLastGroupPrefix()
721721
*/
722722
public function getRoutes($version = null)
723723
{
724-
$routes = $this->adapter->getRoutes($version);
724+
$routes = $this->adapter->getIterableRoutes($version);
725725

726726
if (! is_null($version)) {
727727
$routes = [$version => $routes];
@@ -761,9 +761,9 @@ public function getAdapterRoutes()
761761
*/
762762
public function setAdapterRoutes(array $routes)
763763
{
764-
$this->container->instance('api.routes', $routes);
765-
766764
$this->adapter->setRoutes($routes);
765+
766+
$this->container->instance('api.routes', $this->getRoutes());
767767
}
768768

769769
/**

tests/Stubs/RoutingAdapterStub.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Dingo\Api\Tests\Stubs;
44

55
use Closure;
6+
use ArrayIterator;
67
use Illuminate\Http\Request;
78
use Dingo\Api\Http\Response;
89
use Dingo\Api\Contract\Routing\Adapter;
@@ -107,6 +108,11 @@ public function getRoutes($version = null)
107108
return $this->routes;
108109
}
109110

111+
public function getIterableRoutes($version = null)
112+
{
113+
return new ArrayIterator($this->getRoutes($version));
114+
}
115+
110116
public function setRoutes(array $routes)
111117
{
112118
//

0 commit comments

Comments
 (0)