Passed
Push — master ( 5bda87...a2d46a )
by Arman
03:19 queued 12s
created

route_prefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
use Quantum\Router\RouteController;
15
use Quantum\Router\Router;
16
17
/**
18
 * Gets current middlewares
19
 * @return array|null
20
 */
21
function current_middlewares(): ?array
22
{
23
    return RouteController::getCurrentRoute()['middlewares'] ?? null;
24
}
25
26
/**
27
 * Gets current module
28
 * @return string|null
29
 */
30
function current_module(): ?string
31
{
32
    return RouteController::getCurrentRoute()['module'] ?? null;
33
}
34
35
/**
36
 * Get current controller
37
 * @return string|null
38
 */
39
function current_controller(): ?string
40
{
41
    return RouteController::getCurrentRoute()['controller'] ?? null;
42
}
43
44
/**
45
 * Gets current action
46
 * @return string|null
47
 */
48
function current_action(): ?string
49
{
50
    return RouteController::getCurrentRoute()['action'] ?? null;
51
}
52
53
/**
54
 * Get current callback
55
 * @return \Closure $callback|null
56
 */
57
function route_callback(): ?Closure
58
{
59
    return RouteController::getCurrentRoute()['callback'] ?? null;
60
}
61
62
/**
63
 * Gets current route
64
 * @return string|null
65
 */
66
function current_route(): ?string
67
{
68
    return RouteController::getCurrentRoute()['route'] ?? null;
69
}
70
71
/**
72
 * Gets current route parameters
73
 * @return array
74
 */
75
function route_params(): array
76
{
77
    return RouteController::getCurrentRoute()['params'] ?? [];
78
}
79
80
/**
81
 * Gets route parameter by name
82
 * @param string $name
83
 * @return mixed
84
 */
85
function route_param(string $name)
86
{
87
    $params = RouteController::getCurrentRoute()['params'];
88
89
    if ($params) {
90
        foreach ($params as $param) {
91
            if ($param['name'] == $name) {
92
                return $param['value'];
93
            }
94
        }
95
    }
96
97
    return null;
98
}
99
100
/**
101
 * Gets current route pattern
102
 * @return string
103
 */
104
function route_pattern(): string
105
{
106
    return RouteController::getCurrentRoute()['pattern'] ?? '';
107
}
108
109
/**
110
 * Gets current route method
111
 * @return string
112
 */
113
function route_method(): string
114
{
115
    return RouteController::getCurrentRoute()['method'] ?? '';
116
}
117
118
/**
119
 * Gets the current route uri
120
 * @return string
121
 */
122
function route_uri(): string
123
{
124
    return RouteController::getCurrentRoute()['uri'] ?? '';
125
}
126
127
/**
128
 * Gets the current route name
129
 * @return string|null
130
 */
131
function route_name(): ?string
132
{
133
    return RouteController::getCurrentRoute()['name'] ?? null;
134
}
135
136
/**
137
 * Gets the current route name
138
 * @return string|null
139
 */
140
function route_prefix(): ?string
141
{
142
    return RouteController::getCurrentRoute()['prefix'] ?? null;
143
}
144
145
/**
146
 * Finds the route by name in given module scope
147
 * @param string $name
148
 * @param string $module
149
 * @return array|null
150
 */
151
function find_route_by_name(string $name, string $module): ?array
152
{
153
    foreach (Router::getRoutes() as $route) {
154
        if (isset($route['name']) &&
155
                strtolower($route['name']) == strtolower($name) &&
156
                strtolower($route['module']) == strtolower($module)) {
157
            
158
            return $route;
159
        }
160
    }
161
162
    return null;
163
}
164
165
/**
166
 * Checks the existence of the route group by name in given module scope
167
 * @param string $name
168
 * @param string $module
169
 * @return bool
170
 */
171
function route_group_exists(string $name, string $module): bool
172
{
173
    foreach (Router::getRoutes() as $route) {
174
        if (isset($route['group']) &&
175
                strtolower($route['group']) == strtolower($name) &&
176
                strtolower($route['module']) == strtolower($module)) {
177
178
            return true;
179
        }
180
    }
181
182
    return false;
183
}
184