Completed
Push — master ( 2afdf2...fdda97 )
by Arman
05:37 queued 03:05
created

Route::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
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.0.0
13
 */
14
15
namespace Quantum\Routes;
16
17
use Closure;
18
19
/**
20
 * Route Class
21
 *
22
 * Route class allows to add new route entries
23
 *
24
 * @package Quantum
25
 * @category Routes
26
 */
27
class Route
28
{
29
30
    /**
31
     * Current module
32
     * @var string
33
     */
34
    private $module;
35
36
    /**
37
     * Identifies the group middleware 
38
     *
39
     * @var boolean
40
     */
41
    private $isGroupeMiddlewares;
42
43
    /**
44
     * Identifies the group
45
     * @var boolean
46
     */
47
    private $isGroupe = false;
48
49
    /**
50
     * Current group name
51
     * @var string
52
     */
53
    private $currentGroupName = null;
54
55
    /**
56
     * Current route
57
     * @var array
58
     */
59
    private $currentRoute = [];
60
61
    /**
62
     * Virtual routes
63
     * @var array
64
     */
65
    private $virtualRoutes = [];
66
67
    /**
68
     * Class constructor
69
     * @param string $module
70
     */
71
    public function __construct($module)
72
    {
73
        $this->virtualRoutes['*'] = [];
74
        $this->module = $module;
75
    }
76
77
    /**
78
     * Adds new route entry to routes
79
     * @param string $route
80
     * @param string $method
81
     * @param string $controller
82
     * @param string $action
83
     * @return $this
84
     */
85
    public function add($route, $method, $controller, $action)
86
    {
87
        $this->currentRoute = [
88
            'route' => $route,
89
            'method' => $method,
90
            'controller' => $controller,
91
            'action' => $action,
92
            'module' => $this->module,
93
        ];
94
95
        if ($this->currentGroupName) {
96
            $this->virtualRoutes[$this->currentGroupName][] = $this->currentRoute;
97
        } else {
98
            $this->virtualRoutes['*'][] = $this->currentRoute;
99
        }
100
101
        return $this;
102
    }
103
104
    /**
105
     * Adds new get route entry to routes
106
     * @param string $route
107
     * @param string $controller
108
     * @param string $action
109
     * @return $this
110
     */
111
    public function get($route, $controller, $action)
112
    {
113
        return $this->add($route, 'GET', $controller, $action);
114
    }
115
116
    /**
117
     * Adds new post route entry to routes
118
     * @param string $route
119
     * @param string $controller
120
     * @param string $action
121
     * @return $this
122
     */
123
    public function post($route, $controller, $action)
124
    {
125
        return $this->add($route, 'POST', $controller, $action);
126
    }
127
128
    /**
129
     * Starts a named group of routes
130
     * @param string $groupName
131
     * @param Closure $callback
132
     * @return $this
133
     */
134
    public function group(string $groupName, Closure $callback)
135
    {
136
        $this->currentGroupName = $groupName;
137
138
        $this->isGroupe = true;
139
        $this->isGroupeMiddlewares = false;
140
        $callback($this);
141
        $this->isGroupeMiddlewares = true;
142
        $this->currentGroupName = null;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Adds middlewares to routes and route groups
149
     * @param array $middlewares
150
     */
151
    public function middlewares(array $middlewares = [])
152
    {
153
        if (!$this->isGroupe) {
154
            end($this->virtualRoutes['*']);
155
            $lastKey = key($this->virtualRoutes['*']);
156
            $this->virtualRoutes['*'][$lastKey]['middlewares'] = $middlewares;
157
        } else {
158
            end($this->virtualRoutes);
159
            $lastKeyOfFirstRound = key($this->virtualRoutes);
160
161
            if (!$this->isGroupeMiddlewares) {
162
                end($this->virtualRoutes[$lastKeyOfFirstRound]);
163
                $lastKeyOfSecondRound = key($this->virtualRoutes[$lastKeyOfFirstRound]);
164
                $this->virtualRoutes[$lastKeyOfFirstRound][$lastKeyOfSecondRound]['middlewares'] = $middlewares;
165
            } else {
166
                $this->isGroupe = false;
167
                foreach ($this->virtualRoutes[$lastKeyOfFirstRound] as &$route) {
168
                    $hasMiddleware = end($route);
169
                    if (!is_array($hasMiddleware)) {
170
                        $route['middlewares'] = $middlewares;
171
                    } else {
172
                        $reversedMiddlewares = array_reverse($middlewares);
173
                        foreach ($reversedMiddlewares as $middleware) {
174
                            array_unshift($route['middlewares'], $middleware);
175
                        }
176
                    }
177
                }
178
            }
179
        }
180
    }
181
182
    /**
183
     * Gets the run-time routes
184
     * @return array
185
     */
186
    public function getRuntimeRoutes()
187
    {
188
        $runtimeRoutes = [];
189
190
        foreach ($this->virtualRoutes as $virtualRoute) {
191
            foreach ($virtualRoute as $route) {
192
                $runtimeRoutes[] = $route;
193
            }
194
        }
195
        return $runtimeRoutes;
196
    }
197
198
    /**
199
     * Gets the virtual routes
200
     * @return array
201
     */
202
    public function getVirtualRoutes()
203
    {
204
        return $this->virtualRoutes;
205
    }
206
207
}
208