Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.31% |
48 / 52 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
RequestHandler | |
92.31% |
48 / 52 |
|
88.89% |
8 / 9 |
21.20 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
prepare | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
3 | |||
setRequest | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getRequest | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
request | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasRequest | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
prepareHeaderAsString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
prepareAsString | |
78.95% |
15 / 19 |
|
0.00% |
0 / 1 |
9.76 | |||
getRequestTimestamp | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Debug\Handler; |
15 | |
16 | use Pop\Http\Server\Request; |
17 | use Pop\Http\Uri; |
18 | use Pop\Session\Session; |
19 | |
20 | /** |
21 | * Debug request handler class |
22 | * |
23 | * @category Pop |
24 | * @package Pop\Debug |
25 | * @author Nick Sagona, III <dev@nolainteractive.com> |
26 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
27 | * @license http://www.popphp.org/license New BSD License |
28 | * @version 2.0.0 |
29 | */ |
30 | class RequestHandler extends AbstractHandler |
31 | { |
32 | |
33 | /** |
34 | * Request |
35 | * @var ?Request |
36 | */ |
37 | protected ?Request $request = null; |
38 | |
39 | /** |
40 | * Request timestamp |
41 | * @var ?float |
42 | */ |
43 | protected ?float $requestTimestamp = null; |
44 | |
45 | /** |
46 | * Constructor |
47 | * |
48 | * Instantiate a request handler object |
49 | * |
50 | * @param ?string $name |
51 | * @param ?Request $request |
52 | */ |
53 | public function __construct(?string $name = null, ?Request $request = null) |
54 | { |
55 | parent::__construct($name); |
56 | if ($request === null) { |
57 | $request = new Request(new Uri()); |
58 | } |
59 | $this->setRequest($request); |
60 | |
61 | $this->requestTimestamp = microtime(true); |
62 | } |
63 | |
64 | /** |
65 | * Prepare handler data for storage |
66 | * |
67 | * @return array |
68 | */ |
69 | public function prepare(): array |
70 | { |
71 | Session::getInstance(); |
72 | |
73 | return [ |
74 | 'uri' => $this->request->getUri()->getUri(), |
75 | 'method' => $this->request->getMethod(), |
76 | 'headers' => $this->request->getHeaders(), |
77 | 'server' => $this->request->getServer(), |
78 | 'env' => $this->request->getEnv(), |
79 | 'get' => $this->request->getQuery(), |
80 | 'post' => $this->request->getPost(), |
81 | 'put' => $this->request->getPut(), |
82 | 'patch' => $this->request->getPatch(), |
83 | 'delete' => $this->request->getDelete(), |
84 | 'files' => $this->request->getFiles(), |
85 | 'cookie' => (isset($_COOKIE)) ? $_COOKIE : [], |
86 | 'session' => (isset($_SESSION)) ? $_SESSION : [], |
87 | 'raw' => $this->request->getRawData(), |
88 | 'parsed' => $this->request->getParsedData(), |
89 | 'timestamp' => number_format($this->requestTimestamp, 5, '.', '') |
90 | ]; |
91 | } |
92 | |
93 | /** |
94 | * Set request |
95 | * |
96 | * @param Request $request |
97 | * @return RequestHandler |
98 | */ |
99 | public function setRequest(Request $request): RequestHandler |
100 | { |
101 | $this->request = $request; |
102 | return $this; |
103 | } |
104 | |
105 | /** |
106 | * Get request |
107 | * |
108 | * @return Request |
109 | */ |
110 | public function getRequest(): Request |
111 | { |
112 | return $this->request; |
113 | } |
114 | |
115 | /** |
116 | * Get request (alias) |
117 | * |
118 | * @return Request |
119 | */ |
120 | public function request(): Request |
121 | { |
122 | return $this->request; |
123 | } |
124 | |
125 | /** |
126 | * Has request |
127 | * |
128 | * @return bool |
129 | */ |
130 | public function hasRequest(): bool |
131 | { |
132 | return ($this->request !== null); |
133 | } |
134 | |
135 | /** |
136 | * Prepare header string |
137 | * |
138 | * @return string |
139 | */ |
140 | public function prepareHeaderAsString(): string |
141 | { |
142 | $string = ((!empty($this->name)) ? $this->name . ' ' : '') . 'Request Handler'; |
143 | $string .= PHP_EOL . str_repeat('=', strlen($string)) . PHP_EOL; |
144 | |
145 | return $string; |
146 | } |
147 | |
148 | /** |
149 | * Prepare handler data as string |
150 | * |
151 | * @return string |
152 | */ |
153 | public function prepareAsString(): string |
154 | { |
155 | $string = ''; |
156 | if (!empty($this->request->getUri()) && !empty($this->request->getUri()->getUri())) { |
157 | $string .= $this->request->getMethod() . ' ' . $this->request->getUri()->getUri() . ' [' . |
158 | number_format($this->requestTimestamp, 5, '.', '') . ']' . PHP_EOL . PHP_EOL; |
159 | |
160 | $dataArrays = $this->prepare(); |
161 | |
162 | foreach ($dataArrays as $key => $data) { |
163 | if (is_array($data) && (count($data) > 0)) { |
164 | $string .= str_replace('DATA', '', strtoupper($key)) . ":" . PHP_EOL; |
165 | $string .= str_repeat('-', (strlen(str_replace('DATA', '', strtoupper($key))) + 1)) . PHP_EOL; |
166 | foreach ($data as $k => $v) { |
167 | $string .= $k . ": " . ((is_array($v)) ? http_build_query($v) : $v) . PHP_EOL; |
168 | } |
169 | $string .= PHP_EOL; |
170 | } |
171 | } |
172 | if (!empty($this->request->getRawData())) { |
173 | $string .= "RAW:" . PHP_EOL; |
174 | $string .= "----" . PHP_EOL; |
175 | $string .= $this->request->getRawData() . PHP_EOL; |
176 | } |
177 | } else { |
178 | $string .= "No Request URI Detected." . PHP_EOL; |
179 | } |
180 | |
181 | $string .= PHP_EOL; |
182 | |
183 | return $string; |
184 | } |
185 | |
186 | /** |
187 | * Get request timestamp |
188 | * |
189 | * @return float |
190 | */ |
191 | public function getRequestTimestamp(): float |
192 | { |
193 | return $this->requestTimestamp; |
194 | } |
195 | |
196 | } |