Passed
Push — fix/singleton-navigation ( b0adab )
by Quentin
07:04
created

lastRouteGroupName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
use Illuminate\Support\Facades\Request;
4
use Illuminate\Support\Str;
5
use Illuminate\Support\Facades\Route;
6
7
if (!function_exists('moduleRoute')) {
8
    /**
9
     * @param string $moduleName
10
     * @param string $prefix
11
     * @param string $action
12
     * @param array $parameters
13
     * @param bool $absolute
14
     * @return string
15
     */
16
    function moduleRoute($moduleName, $prefix, $action = '', $parameters = [], $absolute = true)
17
    {
18
        // Fix module name case
19
        $moduleName = Str::camel($moduleName);
20
21
        // Nested module, pass in current parameters for deeply nested modules
22
        if (Str::contains($moduleName, '.')) {
23
            $parameters = array_merge(Route::current()->parameters(), $parameters);
24
        }
25
26
        // Create base route name
27
        $routeName = 'admin.' . ($prefix ? $prefix . '.' : '');
28
29
        // Prefix it with module name only if prefix doesn't contains it already
30
        if (
31
            config('twill.allow_duplicates_on_route_names', true) ||
32
            ($prefix !== $moduleName &&
33
                !Str::endsWith($prefix, '.' . $moduleName))
34
        ) {
35
            $routeName .= "{$moduleName}";
36
        }
37
38
        //  Add the action name
39
        $routeName .= $action ? ".{$action}" : '';
40
41
        // Build the route
42
        return route($routeName, $parameters, $absolute);
43
    }
44
}
45
46
if (!function_exists('getNavigationUrl')) {
47
    /**
48
     * @param array $element
49
     * @param string $key
50
     * @param string|null $prefix
51
     * @return string
52
     */
53
    function getNavigationUrl($element, $key, $prefix = null)
54
    {
55
        $isModule = $element['module'] ?? false;
56
        $isSingleton = $element['singleton'] ?? false;
57
58
        if ($isModule) {
59
            $action = $element['route'] ?? 'index';
60
            return moduleRoute($key, $prefix, $action);
61
        } elseif ($isSingleton) {
62
            return moduleRoute($key, $prefix);
63
        } elseif ($element['raw'] ?? false) {
64
            return !empty($element['route']) ? $element['route'] : '#';
65
        }
66
67
        return !empty($element['route']) ? route($element['route'], $element['params'] ?? []) : '#';
68
    }
69
}
70
71
if (!function_exists('isActiveNavigation')) {
72
    /**
73
     * @param array $navigationElement
74
     * @param string $navigationKey
75
     * @param string $activeNavigationKey
76
     * @return bool
77
     */
78
    function isActiveNavigation($navigationElement, $navigationKey, $activeNavigationKey)
79
    {
80
        $keysAreMatching = isset($activeNavigationKey) && $navigationKey === $activeNavigationKey;
81
82
        if ($keysAreMatching) {
83
            return true;
84
        }
85
86
        $urlsAreMatching = ($navigationElement['raw'] ?? false) && Str::endsWith(Request::url(), $navigationElement['route']);
87
88
        return $urlsAreMatching;
89
    }
90
}
91
92
if (!function_exists('twillRouteGroupPrefix')) {
93
    function twillRouteGroupPrefix()
94
    {
95
        $groupPrefix = trim(
96
            str_replace('/', '.', Route::getLastGroupPrefix()),
97
            '.'
98
        );
99
100
        if (!empty(config('twill.admin_app_path'))) {
101
            $groupPrefix = ltrim(
102
                str_replace(
103
                    config('twill.admin_app_path'),
104
                    '',
105
                    $groupPrefix
106
                ),
107
                '.'
108
            );
109
        }
110
111
        return $groupPrefix;
112
    }
113
}
114
115
if (!function_exists('lastRouteGroupName')) {
116
    function lastRouteGroupName()
117
    {
118
        // Get the current route groups
119
        $routeGroups = Route::getGroupStack() ?? [];
120
121
        // Get the name prefix of the last group
122
        return end($routeGroups)['as'] ?? '';
123
    }
124
}
125