Passed
Pull Request — 2.x (#729)
by Antonio Carlos
05:57
created

HasRoutes::getRouteMiddleware()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 18
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Services\Routing;
4
5
use Illuminate\Support\Facades\Route;
6
7
trait HasRoutes
8
{
9
    protected function registerRoutes(
10
        $router,
11
        $groupOptions,
12
        $middlewares,
13
        $supportSubdomainRouting,
14
        $namespace,
15
        $routesFile
16
    ) {
17
        if (file_exists($routesFile)) {
18
            $hostRoutes = function ($router) use (
19
                $middlewares,
20
                $namespace,
21
                $routesFile
22
            ) {
23
                $router->group(
24
                    [
25
                        'namespace' => $namespace,
26
                        'middleware' => $middlewares,
27
                    ],
28
                    function ($router) use ($routesFile) {
29
                        require $routesFile;
30
                    }
31
                );
32
            };
33
34
            $router->group(
35
                $groupOptions + [
36
                    'domain' => config('twill.admin_app_url'),
37
                ],
38
                $hostRoutes
39
            );
40
41
            if ($supportSubdomainRouting) {
42
                $router->group(
43
                    $groupOptions + [
44
                        'domain' =>
45
                            config('twill.admin_app_subdomain', 'admin') .
46
                            '.{subdomain}.' .
47
                            config('app.url'),
48
                    ],
49
                    $hostRoutes
50
                );
51
            }
52
        }
53
    }
54
55
    public function registerRoutePatterns()
56
    {
57
        if (($patterns = config('twill.admin_route_patterns')) != null) {
58
            if (is_array($patterns)) {
59
                foreach ($patterns as $label => $pattern) {
60
                    Route::pattern($label, $pattern);
61
                }
62
            }
63
        }
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    protected function getRouteGroupOptions(): array
70
    {
71
        $groupOptions = [
72
            'as' => 'admin.',
73
            'middleware' => [config('twill.admin_middleware_group', 'web')],
74
            'prefix' => rtrim(ltrim(config('twill.admin_app_path'), '/'), '/'),
75
        ];
76
77
        return $groupOptions;
78
    }
79
80
    public function getRouteMiddleware($middleware = null)
81
    {
82
        if (is_array($middleware)) {
83
            return $middleware;
84
        }
85
86
        $middleware = [
87
            'twill_auth:twill_users',
88
            'impersonate',
89
            'validateBackHistory',
90
            'localization',
91
        ];
92
93
        if ($this->supportSubdomainRouting()) {
94
            array_unshift($middlewares, 'supportSubdomainRouting');
95
        }
96
97
        return $middleware;
98
    }
99
100
    public function supportSubdomainRouting()
101
    {
102
        return config('twill.support_subdomain_admin_routing', false);
103
    }
104
105
    public function registerCapsuleRoutes($router, $capsule)
106
    {
107
        if (file_exists($capsule['routes_file'])) {
108
            $this->registerRoutes(
109
                $router,
110
                $this->getRouteGroupOptions(),
111
                $this->getRouteMiddleware(),
112
                $this->supportSubdomainRouting(),
113
                $this->manager->capsuleNamespace(
114
                    $capsule['name'],
115
                    'controllers'
116
                ),
117
                $capsule['routes_file']
118
            );
119
        }
120
    }
121
}
122