Skip to content

Commit b0e945b

Browse files
committed
Revert de7bbb4.
Going to approach this from a different angle as it caused more problems than it solved.
1 parent 27163a8 commit b0e945b

File tree

4 files changed

+57
-39
lines changed

4 files changed

+57
-39
lines changed

src/Provider/LaravelServiceProvider.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class LaravelServiceProvider extends DingoServiceProvider
1818
*/
1919
public function boot()
2020
{
21+
parent::boot();
22+
2123
$this->publishes([realpath(__DIR__.'/../../config/api.php') => config_path('api.php')]);
2224

2325
$kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');
@@ -34,7 +36,9 @@ public function boot()
3436
$this->updateRouterBindings();
3537
});
3638

37-
parent::boot();
39+
$this->app['router']->middleware('api.auth', 'Dingo\Api\Http\Middleware\Auth');
40+
$this->app['router']->middleware('api.throttle', 'Dingo\Api\Http\Middleware\RateLimit');
41+
$this->app['router']->middleware('api.controllers', 'Dingo\Api\Http\Middleware\PrepareController');
3842
}
3943

4044
/**
@@ -95,26 +99,10 @@ public function register()
9599
protected function registerRouterAdapter()
96100
{
97101
$this->app->singleton('api.router.adapter', function ($app) {
98-
return new LaravelAdapter($app, $this->cloneLaravelRouter(), $app['router']->getRoutes());
102+
return new LaravelAdapter($app['router']);
99103
});
100104
}
101105

102-
/**
103-
* Clone the Laravel router and set the middleware on the cloned router.
104-
*
105-
* @return \Illuminate\Routing\Router
106-
*/
107-
protected function cloneLaravelRouter()
108-
{
109-
$router = clone $this->app['router'];
110-
111-
$router->middleware('api.auth', 'Dingo\Api\Http\Middleware\Auth');
112-
$router->middleware('api.throttle', 'Dingo\Api\Http\Middleware\RateLimit');
113-
$router->middleware('api.controllers', 'Dingo\Api\Http\Middleware\PrepareController');
114-
115-
return $router;
116-
}
117-
118106
/**
119107
* Add the request middleware to the beginning of the kernel.
120108
*

src/Provider/LumenServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class LumenServiceProvider extends DingoServiceProvider
1616
*/
1717
public function boot()
1818
{
19+
parent::boot();
20+
1921
$this->app->configure('api');
2022

2123
$reflection = new ReflectionClass($this->app);
@@ -45,8 +47,6 @@ public function boot()
4547
'api.throttle' => 'Dingo\Api\Http\Middleware\RateLimit',
4648
'api.controllers' => 'Dingo\Api\Http\Middleware\PrepareController',
4749
]);
48-
49-
parent::boot();
5050
}
5151

5252
/**

src/Routing/Adapter/Laravel.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,29 @@ class Laravel implements Adapter
3434
protected $routes = [];
3535

3636
/**
37-
* Application routes.
37+
* Array of merged old routes and API routes.
38+
*
39+
* @var array
40+
*/
41+
protected $mergedRoutes = [];
42+
43+
/**
44+
* Routes already defined on the router.
3845
*
3946
* @var \Illuminate\Routing\RouteCollection
4047
*/
41-
protected $applicationRoutes = [];
48+
protected $oldRoutes;
4249

4350
/**
4451
* Create a new laravel routing adapter instance.
4552
*
46-
* @param \Illuminate\Contracts\Container\Container $container
47-
* @param \Illuminate\Routing\Router $router
48-
* @param \Illuminate\Routing\RouteCollection $applicationRoutes
53+
* @param \Illuminate\Routing\Router $router
4954
*
5055
* @return void
5156
*/
52-
public function __construct(Container $container, Router $router, RouteCollection $applicationRoutes)
57+
public function __construct(Router $router)
5358
{
54-
$this->container = $container;
5559
$this->router = $router;
56-
$this->applicationRoutes = $applicationRoutes;
5760
}
5861

5962
/**
@@ -70,16 +73,37 @@ public function dispatch(Request $request, $version)
7073
throw new UnknownVersionException;
7174
}
7275

73-
$this->router->setRoutes($this->routes[$version]);
76+
$routes = $this->mergeOldRoutes($version);
7477

75-
// Because the above call will reset the routes defined on the applications
76-
// UrlGenerator we will simply rebind the routes to the application
77-
// container which will trigger the rebinding event.
78-
$this->container->instance('routes', $this->applicationRoutes);
78+
$this->router->setRoutes($routes);
7979

8080
return $this->router->dispatch($request);
8181
}
8282

83+
/**
84+
* Merge the old application routes with the API routes.
85+
*
86+
* @param string $version
87+
*
88+
* @return array
89+
*/
90+
protected function mergeOldRoutes($version)
91+
{
92+
if (! isset($this->oldRoutes)) {
93+
$this->oldRoutes = $this->router->getRoutes();
94+
}
95+
96+
if (! isset($this->mergedRoutes[$version])) {
97+
$this->mergedRoutes[$version] = $this->routes[$version];
98+
99+
foreach ($this->oldRoutes as $route) {
100+
$this->mergedRoutes[$version]->add($route);
101+
}
102+
}
103+
104+
return $this->mergedRoutes[$version];
105+
}
106+
83107
/**
84108
* Get the URI, methods, and action from the route.
85109
*

src/Routing/Route.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ class Route
2525
*/
2626
protected $container;
2727

28-
/**
29-
* Request instance.
30-
*
31-
* @var \Illuminate\Http\Request
32-
*/
33-
protected $request;
28+
protected $route;
3429

3530
/**
3631
* Route URI.
@@ -144,6 +139,7 @@ public function __construct(Adapter $adapter, Container $container, Request $req
144139
{
145140
$this->adapter = $adapter;
146141
$this->container = $container;
142+
$this->route = $route;
147143

148144
$this->setupRouteProperties($request, $route);
149145
}
@@ -628,4 +624,14 @@ public function domain()
628624
{
629625
return Arr::get($this->action, 'domain');
630626
}
627+
628+
/**
629+
* Get the original route.
630+
*
631+
* @return array|\Illuminate\Routing\Route
632+
*/
633+
public function getOriginalRoute()
634+
{
635+
return $this->route;
636+
}
631637
}

0 commit comments

Comments
 (0)