| 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 HTTP Request headers. |
| 117 |
* |
| 118 |
* @var Header |
| 119 |
*/ |
| 120 |
public Header $header; |
| 121 |
|
| 122 |
/** |
| 123 |
* The class constructor. |
| 124 |
* |
| 125 |
* @param string $uri The URI for the request. |
| 126 |
* |
| 127 |
* @since 0.1.0 |
| 128 |
*/ |
| 129 |
public function __construct( string $uri = '' ) { |
| 130 |
$this->set_start(); |
| 131 |
$this->set_uri( $uri ); |
| 132 |
$this->set_query(); |
| 133 |
$this->set_path(); |
| 134 |
$this->set_globals(); |
| 135 |
|
| 136 |
$this->header = new Header( getallheaders() ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Sets the micro timestamp to mark the beginning of the request. |
| 141 |
* |
| 142 |
* @since 0.1.0 |
| 143 |
* |
| 144 |
* @param string $start The timestamp used to mark the beginning of the request. |
| 145 |
* |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
public function set_start( string $start = '' ): void { |
| 149 |
|
| 150 |
if ( strlen( $start ) > 0 ) { |
| 151 |
$this->start = $start; |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
$this->start = floatval( $this->server['REQUEST_TIME_FLOAT'] ?? microtime() ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Sets the request URI. |
| 160 |
* |
| 161 |
* @since 0.1.0 |
| 162 |
* |
| 163 |
* @param string $uri The full URI for the request. |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function set_uri( string $uri = '' ): void { |
| 168 |
|
| 169 |
if ( strlen( $uri ) > 0 ) { |
| 170 |
$this->uri = $uri; |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
$protocol = 'http'; |
| 175 |
|
| 176 |
if ( SuperGlobals::get_server_var( 'HTTPS', 'off' ) === 'on' ) { |
| 177 |
$protocol = 'https'; |
| 178 |
} |
| 179 |
|
| 180 |
$host = Uri::normalize_host( (string) SuperGlobals::get_server_var( 'HTTP_HOST', '' ) ); |
| 181 |
$path = SuperGlobals::get_server_var( 'REQUEST_URI', '' ); |
| 182 |
|
| 183 |
$this->uri = $protocol . '://' . $host . $path; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Sets the path from the URI. |
| 188 |
* |
| 189 |
* @since 0.1.0 |
| 190 |
* |
| 191 |
* @param string $path The path to set for the request. |
| 192 |
* |
| 193 |
* @return void |
| 194 |
*/ |
| 195 |
public function set_path( string $path = '' ): void { |
| 196 |
if ( $path === '' ) { |
| 197 |
$this->path = parse_url( $this->uri, PHP_URL_PATH ) ?: ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
$this->path = $path; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Sets the query string for the current request. |
| 206 |
* |
| 207 |
* @since 0.1.0 |
| 208 |
* |
| 209 |
* @param string $query The query string to set for the request. |
| 210 |
* |
| 211 |
* @return void |
| 212 |
*/ |
| 213 |
public function set_query( string $query = '' ): void { |
| 214 |
if ( $query === '' ) { |
| 215 |
$this->query = parse_url( $this->uri, PHP_URL_QUERY ) ?? ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
$this->query = $query; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Sets the associated global variables. |
| 224 |
* |
| 225 |
* @since 0.1.0 |
| 226 |
* |
| 227 |
* @param array $server The SERVER superglobal array for the request. |
| 228 |
* @param array $get The GET superglobal array for the request. |
| 229 |
* @param array $post The POST superglobal array for the request. |
| 230 |
* @param array $env The ENV superglobal array for the request. |
| 231 |
* @param array $cookie The COOKIE superglobal array for the request. |
| 232 |
* @param string $method The REQUEST_METHOD for the request. |
| 233 |
* |
| 234 |
* @return void |
| 235 |
*/ |
| 236 |
public function set_globals( array $server = [], array $get = [], array $post = [], array $env = [], array $cookie = [], string $method = '' ): void { |
| 237 |
$this->server = $server ?: SuperGlobals::get_raw_superglobal( 'SERVER' ); |
| 238 |
$this->get = $get ?: SuperGlobals::get_raw_superglobal( 'GET' ); |
| 239 |
$this->post = $post ?: SuperGlobals::get_raw_superglobal( 'POST' ); |
| 240 |
$this->env = $env ?: SuperGlobals::get_raw_superglobal( 'ENV' ); |
| 241 |
$this->cookie = $cookie ?: SuperGlobals::get_raw_superglobal( 'COOKIE' ); |
| 242 |
$this->method = $method ?: SuperGlobals::get_server_var( 'REQUEST_METHOD', '' ); |
| 243 |
} |
| 244 |
} |
| 245 |
|