Passed
Pull Request — master (#178)
by
unknown
02:51
created

route_cache_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 cache settings
129
 * @return array|null
130
 */
131
function route_cache_settings(): ?array
132
{
133
	return RouteController::getCurrentRoute()['cache_settings'] ?? null;
134
}
135
136
/**
137
 * Gets the current route name
138
 * @return string|null
139
 */
140
function route_name(): ?string
141
{
142
    return RouteController::getCurrentRoute()['name'] ?? null;
143
}
144
145
/**
146
 * Gets the current route name
147
 * @return string|null
148
 */
149
function route_prefix(): ?string
150
{
151
    return RouteController::getCurrentRoute()['prefix'] ?? null;
152
}
153
154
/**
155
 * Finds the route by name in given module scope
156
 * @param string $name
157
 * @param string $module
158
 * @return array|null
159
 */
160
function find_route_by_name(string $name, string $module): ?array
161
{
162
    foreach (Router::getRoutes() as $route) {
163
        if (isset($route['name']) &&
164
                strtolower($route['name']) == strtolower($name) &&
165
                strtolower($route['module']) == strtolower($module)) {
166
            
167
            return $route;
168
        }
169
    }
170
171
    return null;
172
}
173
174
/**
175
 * Checks the existence of the route group by name in given module scope
176
 * @param string $name
177
 * @param string $module
178
 * @return bool
179
 */
180
function route_group_exists(string $name, string $module): bool
181
{
182
    foreach (Router::getRoutes() as $route) {
183
        if (isset($route['group']) &&
184
                strtolower($route['group']) == strtolower($name) &&
185
                strtolower($route['module']) == strtolower($module)) {
186
187
            return true;
188
        }
189
    }
190
191
    return false;
192
}
193