| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Routing;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Contracts\Container\BindingResolutionException;
|
|
|
6 |
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
|
|
|
7 |
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
|
|
|
8 |
use Illuminate\Contracts\View\Factory as ViewFactoryContract;
|
|
|
9 |
use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract;
|
|
|
10 |
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
|
|
|
11 |
use Illuminate\Support\ServiceProvider;
|
|
|
12 |
use Nyholm\Psr7\Factory\Psr17Factory;
|
|
|
13 |
use Nyholm\Psr7\Response as PsrResponse;
|
|
|
14 |
use Psr\Http\Message\ResponseInterface;
|
|
|
15 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
16 |
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
|
|
|
17 |
|
|
|
18 |
class RoutingServiceProvider extends ServiceProvider
|
|
|
19 |
{
|
|
|
20 |
/**
|
|
|
21 |
* Register the service provider.
|
|
|
22 |
*
|
|
|
23 |
* @return void
|
|
|
24 |
*/
|
|
|
25 |
public function register()
|
|
|
26 |
{
|
|
|
27 |
$this->registerRouter();
|
|
|
28 |
$this->registerUrlGenerator();
|
|
|
29 |
$this->registerRedirector();
|
|
|
30 |
$this->registerPsrRequest();
|
|
|
31 |
$this->registerPsrResponse();
|
|
|
32 |
$this->registerResponseFactory();
|
|
|
33 |
$this->registerCallableDispatcher();
|
|
|
34 |
$this->registerControllerDispatcher();
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Register the router instance.
|
|
|
39 |
*
|
|
|
40 |
* @return void
|
|
|
41 |
*/
|
|
|
42 |
protected function registerRouter()
|
|
|
43 |
{
|
|
|
44 |
$this->app->singleton('router', function ($app) {
|
|
|
45 |
return new Router($app['events'], $app);
|
|
|
46 |
});
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Register the URL generator service.
|
|
|
51 |
*
|
|
|
52 |
* @return void
|
|
|
53 |
*/
|
|
|
54 |
protected function registerUrlGenerator()
|
|
|
55 |
{
|
|
|
56 |
$this->app->singleton('url', function ($app) {
|
|
|
57 |
$routes = $app['router']->getRoutes();
|
|
|
58 |
|
|
|
59 |
// The URL generator needs the route collection that exists on the router.
|
|
|
60 |
// Keep in mind this is an object, so we're passing by references here
|
|
|
61 |
// and all the registered routes will be available to the generator.
|
|
|
62 |
$app->instance('routes', $routes);
|
|
|
63 |
|
|
|
64 |
return new UrlGenerator(
|
|
|
65 |
$routes, $app->rebinding(
|
|
|
66 |
'request', $this->requestRebinder()
|
|
|
67 |
), $app['config']['app.asset_url']
|
|
|
68 |
);
|
|
|
69 |
});
|
|
|
70 |
|
|
|
71 |
$this->app->extend('url', function (UrlGeneratorContract $url, $app) {
|
|
|
72 |
// Next we will set a few service resolvers on the URL generator so it can
|
|
|
73 |
// get the information it needs to function. This just provides some of
|
|
|
74 |
// the convenience features to this URL generator like "signed" URLs.
|
|
|
75 |
$url->setSessionResolver(function () {
|
|
|
76 |
return $this->app['session'] ?? null;
|
|
|
77 |
});
|
|
|
78 |
|
|
|
79 |
$url->setKeyResolver(function () {
|
|
|
80 |
return $this->app->make('config')->get('app.key');
|
|
|
81 |
});
|
|
|
82 |
|
|
|
83 |
// If the route collection is "rebound", for example, when the routes stay
|
|
|
84 |
// cached for the application, we will need to rebind the routes on the
|
|
|
85 |
// URL generator instance so it has the latest version of the routes.
|
|
|
86 |
$app->rebinding('routes', function ($app, $routes) {
|
|
|
87 |
$app['url']->setRoutes($routes);
|
|
|
88 |
});
|
|
|
89 |
|
|
|
90 |
return $url;
|
|
|
91 |
});
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Get the URL generator request rebinder.
|
|
|
96 |
*
|
|
|
97 |
* @return \Closure
|
|
|
98 |
*/
|
|
|
99 |
protected function requestRebinder()
|
|
|
100 |
{
|
|
|
101 |
return function ($app, $request) {
|
|
|
102 |
$app['url']->setRequest($request);
|
|
|
103 |
};
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Register the Redirector service.
|
|
|
108 |
*
|
|
|
109 |
* @return void
|
|
|
110 |
*/
|
|
|
111 |
protected function registerRedirector()
|
|
|
112 |
{
|
|
|
113 |
$this->app->singleton('redirect', function ($app) {
|
|
|
114 |
$redirector = new Redirector($app['url']);
|
|
|
115 |
|
|
|
116 |
// If the session is set on the application instance, we'll inject it into
|
|
|
117 |
// the redirector instance. This allows the redirect responses to allow
|
|
|
118 |
// for the quite convenient "with" methods that flash to the session.
|
|
|
119 |
if (isset($app['session.store'])) {
|
|
|
120 |
$redirector->setSession($app['session.store']);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
return $redirector;
|
|
|
124 |
});
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Register a binding for the PSR-7 request implementation.
|
|
|
129 |
*
|
|
|
130 |
* @return void
|
|
|
131 |
*
|
|
|
132 |
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
133 |
*/
|
|
|
134 |
protected function registerPsrRequest()
|
|
|
135 |
{
|
|
|
136 |
$this->app->bind(ServerRequestInterface::class, function ($app) {
|
|
|
137 |
if (class_exists(Psr17Factory::class) && class_exists(PsrHttpFactory::class)) {
|
|
|
138 |
$psr17Factory = new Psr17Factory;
|
|
|
139 |
|
|
|
140 |
return (new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
|
|
|
141 |
->createRequest($app->make('request'));
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
throw new BindingResolutionException('Unable to resolve PSR request. Please install the symfony/psr-http-message-bridge and nyholm/psr7 packages.');
|
|
|
145 |
});
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Register a binding for the PSR-7 response implementation.
|
|
|
150 |
*
|
|
|
151 |
* @return void
|
|
|
152 |
*
|
|
|
153 |
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
|
|
154 |
*/
|
|
|
155 |
protected function registerPsrResponse()
|
|
|
156 |
{
|
|
|
157 |
$this->app->bind(ResponseInterface::class, function () {
|
|
|
158 |
if (class_exists(PsrResponse::class)) {
|
|
|
159 |
return new PsrResponse;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
throw new BindingResolutionException('Unable to resolve PSR response. Please install the nyholm/psr7 package.');
|
|
|
163 |
});
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Register the response factory implementation.
|
|
|
168 |
*
|
|
|
169 |
* @return void
|
|
|
170 |
*/
|
|
|
171 |
protected function registerResponseFactory()
|
|
|
172 |
{
|
|
|
173 |
$this->app->singleton(ResponseFactoryContract::class, function ($app) {
|
|
|
174 |
return new ResponseFactory($app[ViewFactoryContract::class], $app['redirect']);
|
|
|
175 |
});
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Register the callable dispatcher.
|
|
|
180 |
*
|
|
|
181 |
* @return void
|
|
|
182 |
*/
|
|
|
183 |
protected function registerCallableDispatcher()
|
|
|
184 |
{
|
|
|
185 |
$this->app->singleton(CallableDispatcherContract::class, function ($app) {
|
|
|
186 |
return new CallableDispatcher($app);
|
|
|
187 |
});
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Register the controller dispatcher.
|
|
|
192 |
*
|
|
|
193 |
* @return void
|
|
|
194 |
*/
|
|
|
195 |
protected function registerControllerDispatcher()
|
|
|
196 |
{
|
|
|
197 |
$this->app->singleton(ControllerDispatcherContract::class, function ($app) {
|
|
|
198 |
return new ControllerDispatcher($app);
|
|
|
199 |
});
|
|
|
200 |
}
|
|
|
201 |
}
|