|
1
|
|
|
<?php namespace Arcanedev\Support\Providers; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Support\Exceptions\RouteNamespaceUndefinedException; |
|
4
|
|
|
use Illuminate\Contracts\Routing\Registrar as Router; |
|
5
|
|
|
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; |
|
6
|
|
|
use RecursiveDirectoryIterator; |
|
7
|
|
|
use RecursiveIteratorIterator; |
|
8
|
|
|
use SplFileInfo; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class RouteServiceProvider |
|
12
|
|
|
* |
|
13
|
|
|
* @package Arcanedev\Support\Laravel\Providers |
|
14
|
|
|
* @author ARCANEDEV <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* @method Router middleware(\string $name, \string $class) Register a short-hand name for a middleware. |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class RouteServiceProvider extends ServiceProvider |
|
19
|
|
|
{ |
|
20
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
21
|
|
|
| Properties |
|
22
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
23
|
|
|
*/ |
|
24
|
|
|
/** |
|
25
|
|
|
* The Route Registrar instance. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Illuminate\Contracts\Routing\Registrar |
|
28
|
|
|
*/ |
|
29
|
|
|
private $router; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Route collection. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $routes = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Routes path. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $routesPath = '../Http/Routes'; |
|
44
|
|
|
|
|
45
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
46
|
|
|
| Getters & Setters |
|
47
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
48
|
|
|
*/ |
|
49
|
|
|
/** |
|
50
|
|
|
* Set the Router. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Illuminate\Contracts\Routing\Registrar $router |
|
53
|
|
|
* |
|
54
|
|
|
* @return self |
|
55
|
|
|
*/ |
|
56
|
|
|
private function setRouter($router) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->router = $router; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the routes namespace. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Arcanedev\Support\Exceptions\RouteNamespaceUndefinedException |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getRouteNamespace() |
|
71
|
|
|
{ |
|
72
|
|
|
throw new RouteNamespaceUndefinedException( |
|
73
|
|
|
'The routes namespace is undefined.' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
78
|
|
|
| Main Functions |
|
79
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
80
|
|
|
*/ |
|
81
|
|
|
/** |
|
82
|
|
|
* Define the routes for the application. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \Illuminate\Contracts\Routing\Registrar $router |
|
85
|
|
|
*/ |
|
86
|
|
|
abstract public function map(Router $router); |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Map routes. |
|
90
|
|
|
* |
|
91
|
|
|
* @param \Illuminate\Contracts\Routing\Registrar $router |
|
92
|
|
|
* @param string $directory |
|
93
|
|
|
* @param array $attributes |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function mapRoutes(Router $router, $directory, array $attributes = []) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->setRouter($router); |
|
98
|
|
|
$this->registerRoutes($directory); |
|
99
|
|
|
|
|
100
|
|
|
$this->router->group($attributes, function () { |
|
101
|
|
|
foreach ($this->routes as $route) { |
|
102
|
|
|
$this->app->make($route)->map($this->router); |
|
103
|
|
|
} |
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Register Routes. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $directory |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function registerRoutes($directory) |
|
113
|
|
|
{ |
|
114
|
|
|
$di = new RecursiveDirectoryIterator( |
|
115
|
|
|
$directory . DS . $this->routesPath, |
|
116
|
|
|
RecursiveDirectoryIterator::SKIP_DOTS |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
foreach(new RecursiveIteratorIterator($di) as $file) { |
|
120
|
|
|
/** @var SplFileInfo $file */ |
|
121
|
|
|
$this->addRouteFromFile($file); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
126
|
|
|
| Other Functions |
|
127
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
128
|
|
|
*/ |
|
129
|
|
|
/** |
|
130
|
|
|
* Add route from file. |
|
131
|
|
|
* |
|
132
|
|
|
* @param SplFileInfo $file |
|
133
|
|
|
*/ |
|
134
|
|
|
private function addRouteFromFile(SplFileInfo $file) |
|
135
|
|
|
{ |
|
136
|
|
|
if ( |
|
137
|
|
|
! $file->isFile() || |
|
138
|
|
|
pathinfo($file, PATHINFO_EXTENSION) !== 'php' |
|
139
|
|
|
) return; |
|
140
|
|
|
|
|
141
|
|
|
$routeFolder = 'Routes' . DS; |
|
142
|
|
|
$pos = strpos($file->getRealPath(), $routeFolder); |
|
143
|
|
|
|
|
144
|
|
|
if ($pos !== false) { |
|
145
|
|
|
$route = substr( |
|
146
|
|
|
str_replace('.php', '', $file->getRealPath()), |
|
147
|
|
|
$pos + strlen($routeFolder) |
|
148
|
|
|
); |
|
149
|
|
|
$this->addRoute($route); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Add route to collection. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $route |
|
157
|
|
|
* |
|
158
|
|
|
* @return self |
|
159
|
|
|
*/ |
|
160
|
|
|
private function addRoute($route) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->routes[] = $this->getRouteNamespace() . '\\' . $route; |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|