/
TwillRoutes.php
131 lines (117 loc) · 3.91 KB
/
TwillRoutes.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
<?php
namespace A17\Twill;
use A17\Twill\Facades\TwillRoutes as FacadesTwillRoutes;
use A17\Twill\Helpers\Capsule;
use Illuminate\Support\Facades\Route;
class TwillRoutes
{
public function registerRoutes(
$router,
$groupOptions,
$middlewares,
$supportSubdomainRouting,
$namespace,
$routesFile,
$instant = false
): void {
$callback = function () use ($router, $groupOptions, $middlewares, $supportSubdomainRouting, $namespace, $routesFile) {
if (file_exists($routesFile)) {
$hostRoutes = function ($router) use (
$middlewares,
$namespace,
$routesFile
) {
$router->group(
[
'namespace' => $namespace,
'middleware' => $middlewares,
],
function () use ($routesFile) {
require $routesFile;
}
);
};
$router->group(
$groupOptions + [
'domain' => config('twill.admin_app_url'),
],
$hostRoutes
);
if ($supportSubdomainRouting) {
$router->group(
$groupOptions + [
'domain' => config('twill.admin_app_subdomain', 'admin') .
'.{subdomain}.' .
config('app.url'),
],
$hostRoutes
);
}
}
};
if ($instant) {
// For some reasone the afterResolving does not work for the core routes.
// In other cases it is important to use the afterResolving because the routes are otherwise registered too
// early.
$callback();
} else {
FacadesTwillRoutes::resolved($callback);
}
}
public function registerRoutePatterns(): void
{
if (($patterns = config('twill.admin_route_patterns')) != null) {
if (is_array($patterns)) {
foreach ($patterns as $label => $pattern) {
Route::pattern($label, $pattern);
}
}
}
}
/**
* @return array
*/
public function getRouteGroupOptions(): array
{
return [
'as' => config('twill.admin_route_name_prefix', 'admin.'),
'middleware' => [config('twill.admin_middleware_group', 'web')],
'prefix' => rtrim(ltrim(config('twill.admin_app_path'), '/'), '/'),
];
}
public function getRouteMiddleware($middleware = null): array
{
if (is_array($middleware)) {
return $middleware;
}
$middleware = [
'twill_auth:twill_users',
'impersonate',
'validateBackHistory',
'localization',
];
if ($this->supportSubdomainRouting()) {
array_unshift($middleware, 'supportSubdomainRouting');
}
return $middleware;
}
public function supportSubdomainRouting()
{
return config('twill.support_subdomain_admin_routing', false);
}
public function registerCapsuleRoutes($router, Capsule $capsule): void
{
if ($capsule->routesFileExists()) {
$this->registerRoutes(
$router,
$this->getRouteGroupOptions(),
$this->getRouteMiddleware(),
$this->supportSubdomainRouting(),
$capsule->getControllersNamespace(),
$capsule->getRoutesFile(),
// When it is not a package capsule we can register it immediately.
! $capsule->packageCapsule
);
}
}
}