/
RouteServiceProvider.php
161 lines (142 loc) · 3.66 KB
/
RouteServiceProvider.php
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
<?php
namespace Illuminate\Foundation\Support\Providers;
use Closure;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Traits\ForwardsCalls;
/**
* @mixin \Illuminate\Routing\Router
*/
class RouteServiceProvider extends ServiceProvider
{
use ForwardsCalls;
/**
* The controller namespace for the application.
*
* @var string|null
*/
protected $namespace;
/**
* The callback that should be used to load the application's routes.
*
* @var \Closure|null
*/
protected $loadRoutesUsing;
/**
* The global callback that should be used to load the application's routes.
*
* @var \Closure|null
*/
protected static $alwaysLoadRoutesUsing;
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->booted(function () {
$this->setRootControllerNamespace();
if ($this->routesAreCached()) {
$this->loadCachedRoutes();
} else {
$this->loadRoutes();
$this->app->booted(function () {
$this->app['router']->getRoutes()->refreshNameLookups();
$this->app['router']->getRoutes()->refreshActionLookups();
});
}
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the callback that will be used to load the application's routes.
*
* @param \Closure $routesCallback
* @return $this
*/
protected function routes(Closure $routesCallback)
{
$this->loadRoutesUsing = $routesCallback;
return $this;
}
/**
* Register the callback that will be used to load the application's routes.
*
* @param \Closure|null $routesCallback
* @return void
*/
public static function loadRoutesUsing(?Closure $routesCallback)
{
self::$alwaysLoadRoutesUsing = $routesCallback;
}
/**
* Set the root controller namespace for the application.
*
* @return void
*/
protected function setRootControllerNamespace()
{
if (! is_null($this->namespace)) {
$this->app[UrlGenerator::class]->setRootControllerNamespace($this->namespace);
}
}
/**
* Determine if the application routes are cached.
*
* @return bool
*/
protected function routesAreCached()
{
return $this->app->routesAreCached();
}
/**
* Load the cached routes for the application.
*
* @return void
*/
protected function loadCachedRoutes()
{
$this->app->booted(function () {
require $this->app->getCachedRoutesPath();
});
}
/**
* Load the application routes.
*
* @return void
*/
protected function loadRoutes()
{
if (! is_null(self::$alwaysLoadRoutesUsing)) {
$this->app->call(self::$alwaysLoadRoutesUsing);
}
if (! is_null($this->loadRoutesUsing)) {
$this->app->call($this->loadRoutesUsing);
} elseif (method_exists($this, 'map')) {
$this->app->call([$this, 'map']);
}
}
/**
* Pass dynamic methods onto the router instance.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->forwardCallTo(
$this->app->make(Router::class), $method, $parameters
);
}
}