Repository navigation
Expand file tree
/
Copy pathLaravelServiceProvider.php
More file actions
202 lines (170 loc) · 5.44 KB
/
Copy pathLaravelServiceProvider.php
File metadata and controls
202 lines (170 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Dingo\Api\Provider;
use ReflectionClass;
use Dingo\Api\Http\FormRequest;
use Illuminate\Routing\Redirector;
use Dingo\Api\Http\Middleware\Auth;
use Illuminate\Contracts\Http\Kernel;
use Dingo\Api\Event\RequestWasMatched;
use Dingo\Api\Http\Middleware\Request;
use Illuminate\Foundation\Application;
use Dingo\Api\Http\Middleware\RateLimit;
use Illuminate\Routing\ControllerDispatcher;
use Dingo\Api\Http\Middleware\PrepareController;
use Illuminate\Http\Request as IlluminateRequest;
use Dingo\Api\Routing\Adapter\Laravel as LaravelAdapter;
class LaravelServiceProvider extends DingoServiceProvider
{
/**
* Boot the service provider.
*
* @return void
*/
public function boot()
{
parent::boot();
$this->publishes([realpath(__DIR__.'/../../config/api.php') => config_path('api.php')]);
$kernel = $this->app->make(Kernel::class);
$this->app[Request::class]->mergeMiddlewares(
$this->gatherAppMiddleware($kernel)
);
$this->addRequestMiddlewareToBeginning($kernel);
$this->app['events']->listen(RequestWasMatched::class, function (RequestWasMatched $event) {
$this->replaceRouteDispatcher();
$this->updateRouterBindings();
});
$this->app->resolving(FormRequest::class, function (FormRequest $request, Application $app) {
$this->initializeRequest($request, $app['request']);
$request->setContainer($app)->setRedirector($app->make(Redirector::class));
});
$this->addMiddlewareAlias('api.auth', Auth::class);
$this->addMiddlewareAlias('api.throttle', RateLimit::class);
$this->addMiddlewareAlias('api.controllers', PrepareController::class);
}
/**
* Replace the route dispatcher.
*
* @return void
*/
protected function replaceRouteDispatcher()
{
$this->app->singleton('illuminate.route.dispatcher', function ($app) {
return new ControllerDispatcher($app['api.router.adapter']->getRouter(), $app);
});
}
/**
* Grab the bindings from the Laravel router and set them on the adapters
* router.
*
* @return void
*/
protected function updateRouterBindings()
{
foreach ($this->getRouterBindings() as $key => $binding) {
$this->app['api.router.adapter']->getRouter()->bind($key, $binding);
}
}
/**
* Get the Laravel routers bindings.
*
* @return array
*/
protected function getRouterBindings()
{
$property = (new ReflectionClass($this->app['router']))->getProperty('binders');
$property->setAccessible(true);
return $property->getValue($this->app['router']);
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::register();
$this->registerRouterAdapter();
}
/**
* Register the router adapter.
*
* @return void
*/
protected function registerRouterAdapter()
{
$this->app->singleton('api.router.adapter', function ($app) {
return new LaravelAdapter($app['router']);
});
}
/**
* Add the request middleware to the beginning of the kernel.
*
* @param \Illuminate\Contracts\Http\Kernel $kernel
*
* @return void
*/
protected function addRequestMiddlewareToBeginning(Kernel $kernel)
{
$kernel->prependMiddleware(Request::class);
}
/**
* Register a short-hand name for a middleware. For compatibility
* with Laravel < 5.4 check if aliasMiddleware exists since this
* method has been renamed.
*
* @param string $name
* @param string $class
*
* @return void
*/
protected function addMiddlewareAlias($name, $class)
{
$router = $this->app['router'];
if (method_exists($router, 'aliasMiddleware')) {
return $router->aliasMiddleware($name, $class);
}
return $router->middleware($name, $class);
}
/**
* Gather the application middleware besides this one so that we can send
* our request through them, exactly how the developer wanted.
*
* @param \Illuminate\Contracts\Http\Kernel $kernel
*
* @return array
*/
protected function gatherAppMiddleware(Kernel $kernel)
{
$property = (new ReflectionClass($kernel))->getProperty('middleware');
$property->setAccessible(true);
return $property->getValue($kernel);
}
/**
* Initialize the form request with data from the given request.
*
* @param FormRequest $form
* @param IlluminateRequest $current
*
* @return void
*/
protected function initializeRequest(FormRequest $form, IlluminateRequest $current)
{
$files = $current->files->all();
$files = is_array($files) ? array_filter($files) : $files;
$form->initialize(
$current->query->all(),
$current->request->all(),
$current->attributes->all(),
$current->cookies->all(),
$files,
$current->server->all(),
$current->getContent()
);
$form->setJson($current->json());
if ($session = $current->getSession()) {
$form->setLaravelSession($session);
}
$form->setUserResolver($current->getUserResolver());
$form->setRouteResolver($current->getRouteResolver());
}
}