Passed
Push — fix/singleton-navigation ( 6c7e89...5a669f )
by Quentin
06:53 queued 30s
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
6
if (!function_exists('moduleRoute')) {
7
    /**
8
     * @param string $moduleName
9
     * @param string $prefix
10
     * @param string $action
11
     * @param array $parameters
12
     * @param bool $absolute
13
     * @return string
14
     */
15
    function moduleRoute($moduleName, $prefix, $action = '', $parameters = [], $absolute = true)
16
    {
17
        // Fix module name case
18
        $moduleName = Str::camel($moduleName);
19
20
        // Nested module, pass in current parameters for deeply nested modules
21
        if (Str::contains($moduleName, '.')) {
22
            $parameters = array_merge(Route::current()->parameters(), $parameters);
23
        }
24
25
        // Create base route name
26
        $routeName = 'admin.' . ($prefix ? $prefix . '.' : '');
27
28
        // Prefix it with module name only if prefix doesn't contains it already
29
        if (
30
            config('twill.allow_duplicates_on_route_names', true) ||
31
            ($prefix !== $moduleName &&
32
                !Str::endsWith($prefix, '.' . $moduleName))
33
        ) {
34
            $routeName .= "{$moduleName}";
35
        }
36
37
        //  Add the action name
38
        $routeName .= $action ? ".{$action}" : '';
39
40
        // Build the route
41
        return route($routeName, $parameters, $absolute);
42
    }
43
}
44
45
if (!function_exists('getNavigationUrl')) {
46
    /**
47
     * @param array $element
48
     * @param string $key
49
     * @param string|null $prefix
50
     * @return string
51
     */
52
    function getNavigationUrl($element, $key, $prefix = null)
53
    {
54
        $isModule = $element['module'] ?? false;
55
        $isSingleton = $element['singleton'] ?? false;
56
57
        if ($isModule) {
58
            $action = $element['route'] ?? 'index';
59
            return moduleRoute($key, $prefix, $action);
60
        } elseif ($isSingleton) {
61
            return moduleRoute($key, $prefix);
62
        } elseif ($element['raw'] ?? false) {
63
            return !empty($element['route']) ? $element['route'] : '#';
64
        }
65
66
        return !empty($element['route']) ? route($element['route'], $element['params'] ?? []) : '#';
67
    }
68
}
69
70
if (!function_exists('isActiveNavigation')) {
71
    /**
72
     * @param array $navigationElement
73
     * @param string $navigationKey
74
     * @param string $activeNavigationKey
75
     * @return bool
76
     */
77
    function isActiveNavigation($navigationElement, $navigationKey, $activeNavigationKey)
78
    {
79
        $keysAreMatching = isset($activeNavigationKey) && $navigationKey === $activeNavigationKey;
80
81
        if ($keysAreMatching) {
82
            return true;
83
        }
84
85
        $urlsAreMatching = ($navigationElement['raw'] ?? false) && Str::endsWith(Request::url(), $navigationElement['route']);
86
87
        return $urlsAreMatching;
88
    }
89
}
90