1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Luthier Request middleware (internal) |
5
|
|
|
* |
6
|
|
|
* @author Anderson Salas <[email protected]> |
7
|
|
|
* @copyright 2017 |
8
|
|
|
* @license GNU-3.0 |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Luthier\Middleware; |
13
|
|
|
|
14
|
|
|
use Luthier\Core\Route as Route; |
15
|
|
|
|
16
|
|
|
class Request extends \Luthier\Core\Middleware |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Current (improved) route |
21
|
|
|
* |
22
|
|
|
* @var $route |
23
|
|
|
* |
24
|
|
|
* @access protected |
25
|
|
|
*/ |
26
|
|
|
protected $route; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Infered request method |
30
|
|
|
* |
31
|
|
|
* @var $requestMethod |
32
|
|
|
* |
33
|
|
|
* @access protected |
34
|
|
|
*/ |
35
|
|
|
protected $requestMethod; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Class constructor |
39
|
|
|
* |
40
|
|
|
* @return void |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @access public |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
parent::__construct(); |
47
|
|
|
$this->deterimeRequestMethod(); |
48
|
|
|
$this->route = Route::getRouteByPath(self::$uri_string, $this->requestMethod); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Determines the actual request method |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
* |
56
|
|
|
* @access private |
57
|
|
|
*/ |
58
|
|
|
private function deterimeRequestMethod() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
$requestMethod = $_SERVER['REQUEST_METHOD']; |
62
|
|
|
$formMethod = NULL; |
63
|
|
|
$validMethods = Route::getHTTPVerbs(); |
64
|
|
|
|
65
|
|
|
// FIXME: Solve ambiguity here! POST with _method="GET" makes no sense |
66
|
|
|
|
67
|
|
|
if (isset($_POST['_method']) && in_array(strtoupper($_POST['_method']), $validMethods, TRUE)) |
68
|
|
|
$formMethod = strtoupper($_POST['_method']); |
69
|
|
|
|
70
|
|
|
if (is_null($formMethod)) |
71
|
|
|
{ |
72
|
|
|
$this->requestMethod = $requestMethod; |
73
|
|
|
} |
74
|
|
|
else |
75
|
|
|
{ |
76
|
|
|
if ($requestMethod == 'POST') |
77
|
|
|
$this->requestMethod = $formMethod; |
78
|
|
|
|
79
|
|
|
if (!$this->CI->input->is_ajax_request() && $this->requestMethod == 'HEAD') |
|
|
|
|
80
|
|
|
$this->requestMethod = 'POST'; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Entry point of the middleware |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
* |
89
|
|
|
* @access public |
90
|
|
|
*/ |
91
|
|
|
public function run() |
92
|
|
|
{ |
93
|
|
|
if (!$this->route) |
94
|
|
|
{ |
95
|
|
|
//if(is_null(Route::get404())) |
|
|
|
|
96
|
|
|
// show_404(); |
97
|
|
|
|
98
|
|
|
if (Route::get404()->controller != get_class($this->CI)) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if (ENVIRONMENT != 'production') |
101
|
|
|
{ |
102
|
|
|
show_error('The request method '.$this->requestMethod.' is not allowed to view the resource', 403, 'Forbidden method'); |
103
|
|
|
} else |
104
|
|
|
{ |
105
|
|
|
//redirect(Route::get404()->path); |
|
|
|
|
106
|
|
|
Route::trigger404(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} else |
110
|
|
|
{ |
111
|
|
|
if (method_exists($this->CI, $this->route->method)) |
112
|
|
|
{ |
113
|
|
|
$path_args = Route::getRouteArgs($this->route, self::$uri_string); |
|
|
|
|
114
|
|
|
$route_args = Route::compileRoute($this->route)->args; |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
|
118
|
|
|
// Redirect to 404 if not enough parameters provided |
119
|
|
|
|
120
|
|
|
if(count($path_args) < count($route_args['required'])) |
121
|
|
|
redirect(Route::get404()->path); |
122
|
|
|
|
123
|
|
|
if(count($path_args) == 0) |
124
|
|
|
{ |
125
|
|
|
$this->CI->{$this->route->method}(); |
126
|
|
|
} |
127
|
|
|
else |
128
|
|
|
{ |
129
|
|
|
call_user_func_array( [$this->CI, $this->route->method], array_values($path_args) ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
// TODO: Add support to hooks in this execution thread |
134
|
|
|
|
135
|
|
|
$this->CI->output->_display(); |
|
|
|
|
136
|
|
|
exit(0); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
else |
139
|
|
|
{ |
140
|
|
|
if (ENVIRONMENT != 'production') |
141
|
|
|
{ |
142
|
|
|
show_error('The method '.$this->route->controller.'::'.$this->route->method.'() does not exists', 500, 'Method not found'); |
143
|
|
|
} |
144
|
|
|
else |
145
|
|
|
{ |
146
|
|
|
//redirect(Route::get404()->path); |
|
|
|
|
147
|
|
|
Route::trigger404(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.