PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.1.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.1.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Http / Request.php

Request.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.1.0, at src/Performance/Http/Request.php

236 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A basic request.
4 *
5 * @since 0.1.0
6 *
7 * @package SolidWP\Performance
8 */
9
10 namespace SolidWP\Performance\Http;
11
12 use SolidWP\Performance\StellarWP\SuperGlobals\SuperGlobals;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * A class representing a request.
20 *
21 * @since 0.1.0
22 *
23 * @package SolidWP\Performance
24 */
25 class Request {
26
27 /**
28 * The URI of the request.
29 *
30 * @since 0.1.0
31 *
32 * @var string
33 */
34 public string $uri;
35
36 /**
37 * The request path.
38 *
39 * @since 0.1.0
40 *
41 * @var string
42 */
43 public string $path;
44
45 /**
46 * The request query string.
47 *
48 * @since 0.1.0
49 *
50 * @var string
51 */
52 public string $query;
53
54 /**
55 * The time the request was captured.
56 *
57 * @since 0.1.0
58 *
59 * @var string
60 */
61 public string $start;
62
63 /**
64 * The global $_SERVER variable.
65 *
66 * @since 0.1.0
67 *
68 * @var array
69 */
70 public array $server;
71
72 /**
73 * The global $_GET variable.
74 *
75 * @since 0.1.0
76 *
77 * @var array
78 */
79 public array $get;
80
81 /**
82 * The global $_POST variable.
83 *
84 * @since 0.1.0
85 *
86 * @var array
87 */
88 public array $post;
89
90 /**
91 * The global $_ENV variable.
92 *
93 * @since 0.1.0
94 *
95 * @var array
96 */
97 public array $env;
98
99 /**
100 * The global $_COOKIE variable.
101 *
102 * @since 0.1.0
103 *
104 * @var array
105 */
106 public array $cookie;
107
108 /**
109 * The method of the request.
110 *
111 * @var string
112 */
113 public string $method;
114
115 /**
116 * The class constructor.
117 *
118 * @param string $uri The URI for the request.
119 *
120 * @since 0.1.0
121 */
122 public function __construct( string $uri = '' ) {
123 $this->set_start();
124 $this->set_uri( $uri );
125 $this->set_query();
126 $this->set_path();
127 $this->set_globals();
128 }
129
130 /**
131 * Sets the micro timestamp to mark the beginning of the request.
132 *
133 * @since 0.1.0
134 *
135 * @param string $start The timestamp used to mark the beginning of the request.
136 *
137 * @return void
138 */
139 public function set_start( string $start = '' ): void {
140
141 if ( strlen( $start ) > 0 ) {
142 $this->start = $start;
143 return;
144 }
145
146 $this->start = floatval( $this->server['REQUEST_TIME_FLOAT'] ?? microtime() );
147 }
148
149 /**
150 * Sets the request URI.
151 *
152 * @since 0.1.0
153 *
154 * @param string $uri The full URI for the request.
155 *
156 * @return void
157 */
158 public function set_uri( string $uri = '' ): void {
159
160 if ( strlen( $uri ) > 0 ) {
161 $this->uri = $uri;
162 return;
163 }
164
165 $protocol = 'http';
166
167 if ( SuperGlobals::get_server_var( 'HTTPS', 'off' ) === 'on' ) {
168 $protocol = 'https';
169 }
170
171 $host = SuperGlobals::get_server_var( 'HTTP_HOST', '' );
172 $path = SuperGlobals::get_server_var( 'REQUEST_URI', '' );
173
174 $this->uri = $protocol . '://' . $host . $path;
175 }
176
177 /**
178 * Sets the path from the URI.
179 *
180 * @since 0.1.0
181 *
182 * @param string $path The path to set for the request.
183 *
184 * @return void
185 */
186 public function set_path( string $path = '' ): void {
187 if ( $path === '' ) {
188 $this->path = parse_url( $this->uri, PHP_URL_PATH ) ?: ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
189 return;
190 }
191
192 $this->path = $path;
193 }
194
195 /**
196 * Sets the query string for the current request.
197 *
198 * @since 0.1.0
199 *
200 * @param string $query The query string to set for the request.
201 *
202 * @return void
203 */
204 public function set_query( string $query = '' ): void {
205 if ( $query === '' ) {
206 $this->query = parse_url( $this->uri, PHP_URL_QUERY ) ?? ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
207 return;
208 }
209
210 $this->query = $query;
211 }
212
213 /**
214 * Sets the associated global variables.
215 *
216 * @since 0.1.0
217 *
218 * @param array $server The SERVER superglobal array for the request.
219 * @param array $get The GET superglobal array for the request.
220 * @param array $post The POST superglobal array for the request.
221 * @param array $env The ENV superglobal array for the request.
222 * @param array $cookie The COOKIE superglobal array for the request.
223 * @param string $method The REQUEST_METHOD for the request.
224 *
225 * @return void
226 */
227 public function set_globals( array $server = [], array $get = [], array $post = [], array $env = [], array $cookie = [], string $method = '' ): void {
228 $this->server = $server ?: SuperGlobals::get_raw_superglobal( 'SERVER' );
229 $this->get = $get ?: SuperGlobals::get_raw_superglobal( 'GET' );
230 $this->post = $post ?: SuperGlobals::get_raw_superglobal( 'POST' );
231 $this->env = $env ?: SuperGlobals::get_raw_superglobal( 'ENV' );
232 $this->cookie = $cookie ?: SuperGlobals::get_raw_superglobal( 'COOKIE' );
233 $this->method = $method ?: SuperGlobals::get_server_var( 'REQUEST_METHOD', '' );
234 }
235 }
236