@@ -22,412 +22,412 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class ServerRequest extends Request implements ServerRequestInterface |
| 24 | 24 | { |
| 25 | - /** @var array The server parameters. */ |
|
| 26 | - private $serverParams; |
|
| 27 | - |
|
| 28 | - /** @var array The cookie parameters. */ |
|
| 29 | - private $cookieParams; |
|
| 30 | - |
|
| 31 | - /** @var array The query parameters. */ |
|
| 32 | - private $queryParams; |
|
| 33 | - |
|
| 34 | - /** @var array The post parameters. */ |
|
| 35 | - private $postParams; |
|
| 36 | - |
|
| 37 | - /** @var array The files parameters. */ |
|
| 38 | - private $filesParams; |
|
| 39 | - |
|
| 40 | - /** @var array The uploaded files. */ |
|
| 41 | - private $uploadedFiles; |
|
| 42 | - |
|
| 43 | - /** @var null|array|object The parsed body. */ |
|
| 44 | - private $parsedBody; |
|
| 45 | - |
|
| 46 | - /** @var array The attributes. */ |
|
| 47 | - private $attributes; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Construct a Request object with the given method, uri, version, headers & body. |
|
| 51 | - * |
|
| 52 | - * @global array $_SERVER The server parameters. |
|
| 53 | - * @global array $_COOKIE The cookie parameters. |
|
| 54 | - * @global array $_GET The query parameters. |
|
| 55 | - * @global array $_POST The post parameters. |
|
| 56 | - * @global array $_FILES The files parameters. |
|
| 57 | - * |
|
| 58 | - * @param string $method = '' |
|
| 59 | - * @param UriInterface|null $uri = null |
|
| 60 | - * @param string $version = self::DEFAULT_VERSION |
|
| 61 | - * @param array $headers = [] |
|
| 62 | - * @param StreamInterface|null $body = null |
|
| 63 | - */ |
|
| 64 | - public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 65 | - { |
|
| 66 | - if ($body === null) { |
|
| 67 | - $body = new Stream(fopen('php://input', 'r')); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->serverParams = $_SERVER; |
|
| 71 | - $this->cookieParams = $_COOKIE; |
|
| 72 | - $this->queryParams = $this->initQueryParams($this->serverParams); |
|
| 73 | - $this->postParams = $_POST; |
|
| 74 | - $this->filesParams = $_FILES; |
|
| 75 | - $this->uploadedFiles = $this->initUploadedFiles($this->filesParams); |
|
| 76 | - $this->attributes = []; |
|
| 77 | - |
|
| 78 | - parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Initialize the method. |
|
| 83 | - * |
|
| 84 | - * @param string $method |
|
| 85 | - * @return string the method. |
|
| 86 | - */ |
|
| 87 | - private function initMethod($method) |
|
| 88 | - { |
|
| 89 | - return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Initialize the URI. |
|
| 94 | - * |
|
| 95 | - * @param UriInterface|null $uri |
|
| 96 | - * @return UriInterface the URI. |
|
| 97 | - */ |
|
| 98 | - private function initUri($uri) |
|
| 99 | - { |
|
| 100 | - if ($uri !== null) { |
|
| 101 | - return $uri; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; |
|
| 105 | - $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; |
|
| 106 | - $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; |
|
| 107 | - |
|
| 108 | - return new URI($host . $path); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Initialize the headers. |
|
| 113 | - * |
|
| 114 | - * @param array $headers |
|
| 115 | - * @return array the headers. |
|
| 116 | - */ |
|
| 117 | - private function initHeaders($headers) |
|
| 118 | - { |
|
| 119 | - return $headers ?: getallheaders(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Initialize the headers. |
|
| 124 | - * |
|
| 125 | - * @param string $serverParams |
|
| 126 | - * @return array the headers. |
|
| 127 | - */ |
|
| 128 | - private function initQueryParams($serverParams) |
|
| 129 | - { |
|
| 130 | - $result = []; |
|
| 131 | - |
|
| 132 | - if (isset($serverParams['REQUEST_URI']) && ($query = parse_url($serverParams['REQUEST_URI'], \PHP_URL_QUERY))) { |
|
| 133 | - parse_str($query, $result); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return $result ?? []; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Initialize the uploaded files. |
|
| 141 | - * |
|
| 142 | - * @param array $files |
|
| 143 | - * @return array the uploaded files. |
|
| 144 | - */ |
|
| 145 | - private function initUploadedFiles(array $files) |
|
| 146 | - { |
|
| 147 | - $result = []; |
|
| 148 | - |
|
| 149 | - foreach ($files as $key => $value) { |
|
| 150 | - $result[$key] = $this->parseUploadedFiles($value); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $result; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Parse uploaded files. |
|
| 158 | - * |
|
| 159 | - * @param array $files |
|
| 160 | - * @return UploadedFile|array uploaded files. |
|
| 161 | - */ |
|
| 162 | - private function parseUploadedFiles($files) |
|
| 163 | - { |
|
| 164 | - // Empty |
|
| 165 | - $first = reset($files); |
|
| 166 | - |
|
| 167 | - // Single |
|
| 168 | - if (!is_array($first)) { |
|
| 169 | - return $this->parseSingleUploadedFiles($files); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - // Multiple |
|
| 173 | - if (count(array_filter(array_keys($first), 'is_string')) === 0) { |
|
| 174 | - return $this->parseMultipleUploadedFiles($files); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - // Namespace |
|
| 178 | - return $this->initUploadedFiles($files); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Parse single uploaded file. |
|
| 183 | - * |
|
| 184 | - * @param array $file |
|
| 185 | - * @return UploadedFile single uploaded file. |
|
| 186 | - */ |
|
| 187 | - private function parseSingleUploadedFiles(array $file) |
|
| 188 | - { |
|
| 189 | - return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Parse multiple uploaded files. |
|
| 194 | - * |
|
| 195 | - * @param array $files |
|
| 196 | - * @return UploadedFiles[] multiple uploaded files. |
|
| 197 | - */ |
|
| 198 | - private function parseMultipleUploadedFiles(array $files) |
|
| 199 | - { |
|
| 200 | - $count = count($files['name']); |
|
| 201 | - $result = []; |
|
| 202 | - |
|
| 203 | - for ($i = 0; $i < $count; $i++) { |
|
| 204 | - $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $result; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * {@inheritdoc} |
|
| 212 | - */ |
|
| 213 | - public function getServerParams() |
|
| 214 | - { |
|
| 215 | - return $this->serverParams; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * {@inheritdoc} |
|
| 220 | - */ |
|
| 221 | - public function getCookieParams() |
|
| 222 | - { |
|
| 223 | - return $this->cookieParams; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Set the cookie params. |
|
| 228 | - * |
|
| 229 | - * @param array $cookieParams |
|
| 230 | - * @return $this |
|
| 231 | - */ |
|
| 232 | - private function setCookieParams(array $cookieParams) |
|
| 233 | - { |
|
| 234 | - $this->cookieParams = $cookieParams; |
|
| 235 | - |
|
| 236 | - return $this; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * {@inheritdoc} |
|
| 241 | - */ |
|
| 242 | - public function withCookieParams(array $cookieParams) |
|
| 243 | - { |
|
| 244 | - $result = clone $this; |
|
| 245 | - |
|
| 246 | - return $result->setCookieParams($cookieParams); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * {@inheritdoc} |
|
| 251 | - */ |
|
| 252 | - public function getQueryParams() |
|
| 253 | - { |
|
| 254 | - return $this->queryParams; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Set the query params. |
|
| 259 | - * |
|
| 260 | - * @param array $queryParams |
|
| 261 | - * @return $this |
|
| 262 | - */ |
|
| 263 | - private function setQueryParams(array $queryParams) |
|
| 264 | - { |
|
| 265 | - $this->queryParams = $queryParams; |
|
| 266 | - |
|
| 267 | - return $this; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * {@inheritdoc} |
|
| 272 | - */ |
|
| 273 | - public function withQueryParams(array $queryParams) |
|
| 274 | - { |
|
| 275 | - $result = clone $this; |
|
| 276 | - |
|
| 277 | - return $result->setQueryParams($queryParams); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * {@inheritdoc} |
|
| 282 | - */ |
|
| 283 | - public function getUploadedFiles() |
|
| 284 | - { |
|
| 285 | - return $this->uploadedFiles; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Set the uploaded files. |
|
| 290 | - * |
|
| 291 | - * @param array $uploadedFiles |
|
| 292 | - * @return $this |
|
| 293 | - */ |
|
| 294 | - private function setUploadedFiles(array $uploadedFiles) |
|
| 295 | - { |
|
| 296 | - $this->uploadedFiles = $uploadedFiles; |
|
| 297 | - |
|
| 298 | - return $this; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * {@inheritdoc} |
|
| 303 | - */ |
|
| 304 | - public function withUploadedFiles(array $uploadedFiles) |
|
| 305 | - { |
|
| 306 | - $result = clone $this; |
|
| 307 | - |
|
| 308 | - return $result->setUploadedFiles($uploadedFiles); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * {@inheritdoc} |
|
| 313 | - */ |
|
| 314 | - public function getParsedBody() |
|
| 315 | - { |
|
| 316 | - if ($this->parsedBody !== null) { |
|
| 317 | - return $this->parsedBody; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) { |
|
| 321 | - return $this->postParams; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - if ($this->hasContentType('application/json')) { |
|
| 325 | - return json_decode((string) $this->getBody()); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return null; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Checks if a content type header exists with the given content type. |
|
| 333 | - * |
|
| 334 | - * @param string $contentType |
|
| 335 | - * @return bool true if a content type header exists with the given content type. |
|
| 336 | - */ |
|
| 337 | - private function hasContentType($contentType) |
|
| 338 | - { |
|
| 339 | - foreach ($this->getHeader('Content-Type') as $key => $value) { |
|
| 340 | - if (substr($value, 0, strlen($contentType)) == $contentType) { |
|
| 341 | - return true; |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - return false; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Set the parsed body. |
|
| 350 | - * |
|
| 351 | - * @param null|array|object $parsedBody |
|
| 352 | - * @return $this |
|
| 353 | - */ |
|
| 354 | - private function setParsedBody($parsedBody) |
|
| 355 | - { |
|
| 356 | - $this->parsedBody = $parsedBody; |
|
| 357 | - |
|
| 358 | - return $this; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * {@inheritdoc} |
|
| 363 | - */ |
|
| 364 | - public function withParsedBody($parsedBody) |
|
| 365 | - { |
|
| 366 | - $result = clone $this; |
|
| 367 | - |
|
| 368 | - return $result->setParsedBody($parsedBody); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * {@inheritdoc} |
|
| 373 | - */ |
|
| 374 | - public function getAttributes() |
|
| 375 | - { |
|
| 376 | - return $this->attributes; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * {@inheritdoc} |
|
| 381 | - */ |
|
| 382 | - public function getAttribute($name, $default = null) |
|
| 383 | - { |
|
| 384 | - return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Set the attribute. |
|
| 389 | - * |
|
| 390 | - * @param string $name |
|
| 391 | - * @param mixed $value |
|
| 392 | - * @return $this |
|
| 393 | - */ |
|
| 394 | - private function setAttribute($name, $value) |
|
| 395 | - { |
|
| 396 | - $this->attributes[$name] = $value; |
|
| 397 | - |
|
| 398 | - return $this; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * {@inheritdoc} |
|
| 403 | - */ |
|
| 404 | - public function withAttribute($name, $value) |
|
| 405 | - { |
|
| 406 | - $result = clone $this; |
|
| 407 | - |
|
| 408 | - return $result->setAttribute($name, $value); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * Remove the attribute. |
|
| 413 | - * |
|
| 414 | - * @param string $name |
|
| 415 | - * @return $this |
|
| 416 | - */ |
|
| 417 | - private function removeAttribute($name) |
|
| 418 | - { |
|
| 419 | - unset($this->attributes[$name]); |
|
| 420 | - |
|
| 421 | - return $this; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * {@inheritdoc} |
|
| 426 | - */ |
|
| 427 | - public function withoutAttribute($name) |
|
| 428 | - { |
|
| 429 | - $result = clone $this; |
|
| 430 | - |
|
| 431 | - return $result->removeAttribute($name); |
|
| 432 | - } |
|
| 25 | + /** @var array The server parameters. */ |
|
| 26 | + private $serverParams; |
|
| 27 | + |
|
| 28 | + /** @var array The cookie parameters. */ |
|
| 29 | + private $cookieParams; |
|
| 30 | + |
|
| 31 | + /** @var array The query parameters. */ |
|
| 32 | + private $queryParams; |
|
| 33 | + |
|
| 34 | + /** @var array The post parameters. */ |
|
| 35 | + private $postParams; |
|
| 36 | + |
|
| 37 | + /** @var array The files parameters. */ |
|
| 38 | + private $filesParams; |
|
| 39 | + |
|
| 40 | + /** @var array The uploaded files. */ |
|
| 41 | + private $uploadedFiles; |
|
| 42 | + |
|
| 43 | + /** @var null|array|object The parsed body. */ |
|
| 44 | + private $parsedBody; |
|
| 45 | + |
|
| 46 | + /** @var array The attributes. */ |
|
| 47 | + private $attributes; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Construct a Request object with the given method, uri, version, headers & body. |
|
| 51 | + * |
|
| 52 | + * @global array $_SERVER The server parameters. |
|
| 53 | + * @global array $_COOKIE The cookie parameters. |
|
| 54 | + * @global array $_GET The query parameters. |
|
| 55 | + * @global array $_POST The post parameters. |
|
| 56 | + * @global array $_FILES The files parameters. |
|
| 57 | + * |
|
| 58 | + * @param string $method = '' |
|
| 59 | + * @param UriInterface|null $uri = null |
|
| 60 | + * @param string $version = self::DEFAULT_VERSION |
|
| 61 | + * @param array $headers = [] |
|
| 62 | + * @param StreamInterface|null $body = null |
|
| 63 | + */ |
|
| 64 | + public function __construct($method = '', UriInterface $uri = null, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 65 | + { |
|
| 66 | + if ($body === null) { |
|
| 67 | + $body = new Stream(fopen('php://input', 'r')); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->serverParams = $_SERVER; |
|
| 71 | + $this->cookieParams = $_COOKIE; |
|
| 72 | + $this->queryParams = $this->initQueryParams($this->serverParams); |
|
| 73 | + $this->postParams = $_POST; |
|
| 74 | + $this->filesParams = $_FILES; |
|
| 75 | + $this->uploadedFiles = $this->initUploadedFiles($this->filesParams); |
|
| 76 | + $this->attributes = []; |
|
| 77 | + |
|
| 78 | + parent::__construct($this->initMethod($method), $this->initUri($uri), $version, $this->initHeaders($headers), $body); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Initialize the method. |
|
| 83 | + * |
|
| 84 | + * @param string $method |
|
| 85 | + * @return string the method. |
|
| 86 | + */ |
|
| 87 | + private function initMethod($method) |
|
| 88 | + { |
|
| 89 | + return $method === '' && isset($this->getServerParams()['REQUEST_METHOD']) ? $this->getServerParams()['REQUEST_METHOD'] : $method; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Initialize the URI. |
|
| 94 | + * |
|
| 95 | + * @param UriInterface|null $uri |
|
| 96 | + * @return UriInterface the URI. |
|
| 97 | + */ |
|
| 98 | + private function initUri($uri) |
|
| 99 | + { |
|
| 100 | + if ($uri !== null) { |
|
| 101 | + return $uri; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $scheme = isset($this->getServerParams()['HTTPS']) ? 'https://' : 'http://'; |
|
| 105 | + $host = isset($this->getServerParams()['HTTP_HOST']) ? $scheme . $this->getServerParams()['HTTP_HOST'] : ''; |
|
| 106 | + $path = isset($this->getServerParams()['REQUEST_URI']) ? $this->getServerParams()['REQUEST_URI'] : ''; |
|
| 107 | + |
|
| 108 | + return new URI($host . $path); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Initialize the headers. |
|
| 113 | + * |
|
| 114 | + * @param array $headers |
|
| 115 | + * @return array the headers. |
|
| 116 | + */ |
|
| 117 | + private function initHeaders($headers) |
|
| 118 | + { |
|
| 119 | + return $headers ?: getallheaders(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Initialize the headers. |
|
| 124 | + * |
|
| 125 | + * @param string $serverParams |
|
| 126 | + * @return array the headers. |
|
| 127 | + */ |
|
| 128 | + private function initQueryParams($serverParams) |
|
| 129 | + { |
|
| 130 | + $result = []; |
|
| 131 | + |
|
| 132 | + if (isset($serverParams['REQUEST_URI']) && ($query = parse_url($serverParams['REQUEST_URI'], \PHP_URL_QUERY))) { |
|
| 133 | + parse_str($query, $result); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return $result ?? []; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Initialize the uploaded files. |
|
| 141 | + * |
|
| 142 | + * @param array $files |
|
| 143 | + * @return array the uploaded files. |
|
| 144 | + */ |
|
| 145 | + private function initUploadedFiles(array $files) |
|
| 146 | + { |
|
| 147 | + $result = []; |
|
| 148 | + |
|
| 149 | + foreach ($files as $key => $value) { |
|
| 150 | + $result[$key] = $this->parseUploadedFiles($value); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $result; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Parse uploaded files. |
|
| 158 | + * |
|
| 159 | + * @param array $files |
|
| 160 | + * @return UploadedFile|array uploaded files. |
|
| 161 | + */ |
|
| 162 | + private function parseUploadedFiles($files) |
|
| 163 | + { |
|
| 164 | + // Empty |
|
| 165 | + $first = reset($files); |
|
| 166 | + |
|
| 167 | + // Single |
|
| 168 | + if (!is_array($first)) { |
|
| 169 | + return $this->parseSingleUploadedFiles($files); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + // Multiple |
|
| 173 | + if (count(array_filter(array_keys($first), 'is_string')) === 0) { |
|
| 174 | + return $this->parseMultipleUploadedFiles($files); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + // Namespace |
|
| 178 | + return $this->initUploadedFiles($files); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Parse single uploaded file. |
|
| 183 | + * |
|
| 184 | + * @param array $file |
|
| 185 | + * @return UploadedFile single uploaded file. |
|
| 186 | + */ |
|
| 187 | + private function parseSingleUploadedFiles(array $file) |
|
| 188 | + { |
|
| 189 | + return new UploadedFile($file['name'], $file['type'], $file['tmp_name'], $file['error'], $file['size']); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Parse multiple uploaded files. |
|
| 194 | + * |
|
| 195 | + * @param array $files |
|
| 196 | + * @return UploadedFiles[] multiple uploaded files. |
|
| 197 | + */ |
|
| 198 | + private function parseMultipleUploadedFiles(array $files) |
|
| 199 | + { |
|
| 200 | + $count = count($files['name']); |
|
| 201 | + $result = []; |
|
| 202 | + |
|
| 203 | + for ($i = 0; $i < $count; $i++) { |
|
| 204 | + $result[] = new UploadedFile($files['name'][$i], $files['type'][$i], $files['tmp_name'][$i], $files['error'][$i], $files['size'][$i]); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $result; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * {@inheritdoc} |
|
| 212 | + */ |
|
| 213 | + public function getServerParams() |
|
| 214 | + { |
|
| 215 | + return $this->serverParams; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * {@inheritdoc} |
|
| 220 | + */ |
|
| 221 | + public function getCookieParams() |
|
| 222 | + { |
|
| 223 | + return $this->cookieParams; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Set the cookie params. |
|
| 228 | + * |
|
| 229 | + * @param array $cookieParams |
|
| 230 | + * @return $this |
|
| 231 | + */ |
|
| 232 | + private function setCookieParams(array $cookieParams) |
|
| 233 | + { |
|
| 234 | + $this->cookieParams = $cookieParams; |
|
| 235 | + |
|
| 236 | + return $this; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * {@inheritdoc} |
|
| 241 | + */ |
|
| 242 | + public function withCookieParams(array $cookieParams) |
|
| 243 | + { |
|
| 244 | + $result = clone $this; |
|
| 245 | + |
|
| 246 | + return $result->setCookieParams($cookieParams); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * {@inheritdoc} |
|
| 251 | + */ |
|
| 252 | + public function getQueryParams() |
|
| 253 | + { |
|
| 254 | + return $this->queryParams; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Set the query params. |
|
| 259 | + * |
|
| 260 | + * @param array $queryParams |
|
| 261 | + * @return $this |
|
| 262 | + */ |
|
| 263 | + private function setQueryParams(array $queryParams) |
|
| 264 | + { |
|
| 265 | + $this->queryParams = $queryParams; |
|
| 266 | + |
|
| 267 | + return $this; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * {@inheritdoc} |
|
| 272 | + */ |
|
| 273 | + public function withQueryParams(array $queryParams) |
|
| 274 | + { |
|
| 275 | + $result = clone $this; |
|
| 276 | + |
|
| 277 | + return $result->setQueryParams($queryParams); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * {@inheritdoc} |
|
| 282 | + */ |
|
| 283 | + public function getUploadedFiles() |
|
| 284 | + { |
|
| 285 | + return $this->uploadedFiles; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Set the uploaded files. |
|
| 290 | + * |
|
| 291 | + * @param array $uploadedFiles |
|
| 292 | + * @return $this |
|
| 293 | + */ |
|
| 294 | + private function setUploadedFiles(array $uploadedFiles) |
|
| 295 | + { |
|
| 296 | + $this->uploadedFiles = $uploadedFiles; |
|
| 297 | + |
|
| 298 | + return $this; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * {@inheritdoc} |
|
| 303 | + */ |
|
| 304 | + public function withUploadedFiles(array $uploadedFiles) |
|
| 305 | + { |
|
| 306 | + $result = clone $this; |
|
| 307 | + |
|
| 308 | + return $result->setUploadedFiles($uploadedFiles); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * {@inheritdoc} |
|
| 313 | + */ |
|
| 314 | + public function getParsedBody() |
|
| 315 | + { |
|
| 316 | + if ($this->parsedBody !== null) { |
|
| 317 | + return $this->parsedBody; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + if ($this->getMethod() === 'POST' && ($this->hasContentType('application/x-www-form-urlencoded') || $this->hasContentType('multipart/form-data'))) { |
|
| 321 | + return $this->postParams; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + if ($this->hasContentType('application/json')) { |
|
| 325 | + return json_decode((string) $this->getBody()); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return null; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Checks if a content type header exists with the given content type. |
|
| 333 | + * |
|
| 334 | + * @param string $contentType |
|
| 335 | + * @return bool true if a content type header exists with the given content type. |
|
| 336 | + */ |
|
| 337 | + private function hasContentType($contentType) |
|
| 338 | + { |
|
| 339 | + foreach ($this->getHeader('Content-Type') as $key => $value) { |
|
| 340 | + if (substr($value, 0, strlen($contentType)) == $contentType) { |
|
| 341 | + return true; |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + return false; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Set the parsed body. |
|
| 350 | + * |
|
| 351 | + * @param null|array|object $parsedBody |
|
| 352 | + * @return $this |
|
| 353 | + */ |
|
| 354 | + private function setParsedBody($parsedBody) |
|
| 355 | + { |
|
| 356 | + $this->parsedBody = $parsedBody; |
|
| 357 | + |
|
| 358 | + return $this; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * {@inheritdoc} |
|
| 363 | + */ |
|
| 364 | + public function withParsedBody($parsedBody) |
|
| 365 | + { |
|
| 366 | + $result = clone $this; |
|
| 367 | + |
|
| 368 | + return $result->setParsedBody($parsedBody); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * {@inheritdoc} |
|
| 373 | + */ |
|
| 374 | + public function getAttributes() |
|
| 375 | + { |
|
| 376 | + return $this->attributes; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * {@inheritdoc} |
|
| 381 | + */ |
|
| 382 | + public function getAttribute($name, $default = null) |
|
| 383 | + { |
|
| 384 | + return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Set the attribute. |
|
| 389 | + * |
|
| 390 | + * @param string $name |
|
| 391 | + * @param mixed $value |
|
| 392 | + * @return $this |
|
| 393 | + */ |
|
| 394 | + private function setAttribute($name, $value) |
|
| 395 | + { |
|
| 396 | + $this->attributes[$name] = $value; |
|
| 397 | + |
|
| 398 | + return $this; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * {@inheritdoc} |
|
| 403 | + */ |
|
| 404 | + public function withAttribute($name, $value) |
|
| 405 | + { |
|
| 406 | + $result = clone $this; |
|
| 407 | + |
|
| 408 | + return $result->setAttribute($name, $value); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * Remove the attribute. |
|
| 413 | + * |
|
| 414 | + * @param string $name |
|
| 415 | + * @return $this |
|
| 416 | + */ |
|
| 417 | + private function removeAttribute($name) |
|
| 418 | + { |
|
| 419 | + unset($this->attributes[$name]); |
|
| 420 | + |
|
| 421 | + return $this; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * {@inheritdoc} |
|
| 426 | + */ |
|
| 427 | + public function withoutAttribute($name) |
|
| 428 | + { |
|
| 429 | + $result = clone $this; |
|
| 430 | + |
|
| 431 | + return $result->removeAttribute($name); |
|
| 432 | + } |
|
| 433 | 433 | } |
@@ -22,141 +22,141 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class Request extends Message implements RequestInterface |
| 24 | 24 | { |
| 25 | - /** @var string The request target. */ |
|
| 26 | - private $requestTarget; |
|
| 27 | - |
|
| 28 | - /** @var string The method. */ |
|
| 29 | - private $method; |
|
| 30 | - |
|
| 31 | - /** @var UriInterface The URI. */ |
|
| 32 | - private $uri; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Construct a Request object with the given method, uri, version, headers & body. |
|
| 36 | - * |
|
| 37 | - * @param string $method |
|
| 38 | - * @param UriInterface $uri |
|
| 39 | - * @param string $version = self::DEFAULT_VERSION |
|
| 40 | - * @param array $headers = [] |
|
| 41 | - * @param StreamInterface|null $body = null |
|
| 42 | - */ |
|
| 43 | - public function __construct($method, UriInterface $uri, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 44 | - { |
|
| 45 | - parent::__construct($version, $headers, $body); |
|
| 46 | - |
|
| 47 | - $this->setMethod($method); |
|
| 48 | - $this->setUri($uri); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * {@inheritdoc} |
|
| 53 | - */ |
|
| 54 | - public function getRequestTarget() |
|
| 55 | - { |
|
| 56 | - if (isset($this->requestTarget)) { |
|
| 57 | - return $this->requestTarget; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $result = $this->getUri()->getPath() ?: URI::DELIMITER_PATH; |
|
| 61 | - |
|
| 62 | - if ($this->getUri()->getQuery()) { |
|
| 63 | - $result .= URI::DELIMITER_QUERY . $this->getUri()->getQuery(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return $result; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Set the request target. |
|
| 71 | - * |
|
| 72 | - * @param string $requestTarget |
|
| 73 | - * @return $this |
|
| 74 | - */ |
|
| 75 | - private function setRequestTarget($requestTarget) |
|
| 76 | - { |
|
| 77 | - $this->requestTarget = $requestTarget; |
|
| 78 | - |
|
| 79 | - return $this; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * {@inheritdoc} |
|
| 84 | - */ |
|
| 85 | - public function withRequestTarget($requestTarget) |
|
| 86 | - { |
|
| 87 | - $result = clone $this; |
|
| 88 | - |
|
| 89 | - return $result->setRequestTarget($requestTarget); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * {@inheritdoc} |
|
| 94 | - */ |
|
| 95 | - public function getMethod() |
|
| 96 | - { |
|
| 97 | - return $this->method; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Set the method. |
|
| 102 | - * |
|
| 103 | - * @param string $method |
|
| 104 | - * @return $this |
|
| 105 | - */ |
|
| 106 | - private function setMethod($method) |
|
| 107 | - { |
|
| 108 | - $this->method = $method; |
|
| 109 | - |
|
| 110 | - return $this; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * {@inheritdoc} |
|
| 115 | - */ |
|
| 116 | - public function withMethod($method) |
|
| 117 | - { |
|
| 118 | - $result = clone $this; |
|
| 119 | - |
|
| 120 | - return $result->setMethod($method); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * {@inheritdoc} |
|
| 125 | - */ |
|
| 126 | - public function getUri() |
|
| 127 | - { |
|
| 128 | - return $this->uri; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Set the uri. |
|
| 133 | - * |
|
| 134 | - * @param UriInterface $uri |
|
| 135 | - * @param bool $preserveHost = false |
|
| 136 | - * @return $this |
|
| 137 | - */ |
|
| 138 | - private function setUri(UriInterface $uri, $preserveHost = false) |
|
| 139 | - { |
|
| 140 | - $this->uri = $uri; |
|
| 141 | - |
|
| 142 | - if (!$preserveHost && ($host = $uri->getHost())) { |
|
| 143 | - if ($uri->getPort() !== null) { |
|
| 144 | - $host .= URI::DELIMITER_PORT . $uri->getPort(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $this->setHeader('Host', $host); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return $this; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * {@inheritdoc} |
|
| 155 | - */ |
|
| 156 | - public function withUri(UriInterface $uri, $preserveHost = false) |
|
| 157 | - { |
|
| 158 | - $result = clone $this; |
|
| 159 | - |
|
| 160 | - return $result->setUri($uri, $preserveHost); |
|
| 161 | - } |
|
| 25 | + /** @var string The request target. */ |
|
| 26 | + private $requestTarget; |
|
| 27 | + |
|
| 28 | + /** @var string The method. */ |
|
| 29 | + private $method; |
|
| 30 | + |
|
| 31 | + /** @var UriInterface The URI. */ |
|
| 32 | + private $uri; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Construct a Request object with the given method, uri, version, headers & body. |
|
| 36 | + * |
|
| 37 | + * @param string $method |
|
| 38 | + * @param UriInterface $uri |
|
| 39 | + * @param string $version = self::DEFAULT_VERSION |
|
| 40 | + * @param array $headers = [] |
|
| 41 | + * @param StreamInterface|null $body = null |
|
| 42 | + */ |
|
| 43 | + public function __construct($method, UriInterface $uri, $version = self::DEFAULT_VERSION, array $headers = [], StreamInterface $body = null) |
|
| 44 | + { |
|
| 45 | + parent::__construct($version, $headers, $body); |
|
| 46 | + |
|
| 47 | + $this->setMethod($method); |
|
| 48 | + $this->setUri($uri); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * {@inheritdoc} |
|
| 53 | + */ |
|
| 54 | + public function getRequestTarget() |
|
| 55 | + { |
|
| 56 | + if (isset($this->requestTarget)) { |
|
| 57 | + return $this->requestTarget; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $result = $this->getUri()->getPath() ?: URI::DELIMITER_PATH; |
|
| 61 | + |
|
| 62 | + if ($this->getUri()->getQuery()) { |
|
| 63 | + $result .= URI::DELIMITER_QUERY . $this->getUri()->getQuery(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return $result; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Set the request target. |
|
| 71 | + * |
|
| 72 | + * @param string $requestTarget |
|
| 73 | + * @return $this |
|
| 74 | + */ |
|
| 75 | + private function setRequestTarget($requestTarget) |
|
| 76 | + { |
|
| 77 | + $this->requestTarget = $requestTarget; |
|
| 78 | + |
|
| 79 | + return $this; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * {@inheritdoc} |
|
| 84 | + */ |
|
| 85 | + public function withRequestTarget($requestTarget) |
|
| 86 | + { |
|
| 87 | + $result = clone $this; |
|
| 88 | + |
|
| 89 | + return $result->setRequestTarget($requestTarget); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * {@inheritdoc} |
|
| 94 | + */ |
|
| 95 | + public function getMethod() |
|
| 96 | + { |
|
| 97 | + return $this->method; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Set the method. |
|
| 102 | + * |
|
| 103 | + * @param string $method |
|
| 104 | + * @return $this |
|
| 105 | + */ |
|
| 106 | + private function setMethod($method) |
|
| 107 | + { |
|
| 108 | + $this->method = $method; |
|
| 109 | + |
|
| 110 | + return $this; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * {@inheritdoc} |
|
| 115 | + */ |
|
| 116 | + public function withMethod($method) |
|
| 117 | + { |
|
| 118 | + $result = clone $this; |
|
| 119 | + |
|
| 120 | + return $result->setMethod($method); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * {@inheritdoc} |
|
| 125 | + */ |
|
| 126 | + public function getUri() |
|
| 127 | + { |
|
| 128 | + return $this->uri; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Set the uri. |
|
| 133 | + * |
|
| 134 | + * @param UriInterface $uri |
|
| 135 | + * @param bool $preserveHost = false |
|
| 136 | + * @return $this |
|
| 137 | + */ |
|
| 138 | + private function setUri(UriInterface $uri, $preserveHost = false) |
|
| 139 | + { |
|
| 140 | + $this->uri = $uri; |
|
| 141 | + |
|
| 142 | + if (!$preserveHost && ($host = $uri->getHost())) { |
|
| 143 | + if ($uri->getPort() !== null) { |
|
| 144 | + $host .= URI::DELIMITER_PORT . $uri->getPort(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $this->setHeader('Host', $host); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return $this; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * {@inheritdoc} |
|
| 155 | + */ |
|
| 156 | + public function withUri(UriInterface $uri, $preserveHost = false) |
|
| 157 | + { |
|
| 158 | + $result = clone $this; |
|
| 159 | + |
|
| 160 | + return $result->setUri($uri, $preserveHost); |
|
| 161 | + } |
|
| 162 | 162 | } |
@@ -19,151 +19,151 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class Session implements \IteratorAggregate |
| 21 | 21 | { |
| 22 | - use SingletonTrait; |
|
| 23 | - |
|
| 24 | - /** @var array The session. */ |
|
| 25 | - private $session; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Construct a Sesssion object. |
|
| 29 | - * |
|
| 30 | - * @global array $_SESSION The session parameters. |
|
| 31 | - */ |
|
| 32 | - protected function __construct() |
|
| 33 | - { |
|
| 34 | - $this->session = &$_SESSION; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Initialize the session, if none exists. |
|
| 39 | - * |
|
| 40 | - * @param $name = null |
|
| 41 | - * @return null |
|
| 42 | - * @throws \RuntimeException on failure. |
|
| 43 | - */ |
|
| 44 | - public static function start($name = null) |
|
| 45 | - { |
|
| 46 | - if (session_status() !== PHP_SESSION_NONE) { |
|
| 47 | - throw new \RuntimeException('Can\'t start a new session.'); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - if ($name !== null) { |
|
| 51 | - session_name($name); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - session_start(); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Destroy the session, if one exists. |
|
| 59 | - * |
|
| 60 | - * @return null |
|
| 61 | - * @throws \RuntimeException on failure. |
|
| 62 | - */ |
|
| 63 | - public static function destroy() |
|
| 64 | - { |
|
| 65 | - if (session_status() !== PHP_SESSION_ACTIVE) { |
|
| 66 | - throw new \RuntimeException('Can\'t destroy the session. There is no active session.'); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - if (!session_destroy()) { |
|
| 70 | - throw new \RuntimeException('Failed to destroy the active session.'); |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * {@inheritdoc} |
|
| 76 | - */ |
|
| 77 | - public function getIterator() |
|
| 78 | - { |
|
| 79 | - return new \ArrayIterator(static::getInstance()->session); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Returns the number of key-value mappings in the session map. |
|
| 84 | - * |
|
| 85 | - * @return int the number of key-value mappings in the session map. |
|
| 86 | - */ |
|
| 87 | - public static function count() |
|
| 88 | - { |
|
| 89 | - return count(static::getInstance()->session); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Returns true if the session map contains no key-value mappings. |
|
| 94 | - * |
|
| 95 | - * @return bool true if the session map contains no key-value mappings. |
|
| 96 | - */ |
|
| 97 | - public static function isEmpty() |
|
| 98 | - { |
|
| 99 | - return empty(static::getInstance()->session); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Returns true if the session map contains a mapping for the specified key. |
|
| 104 | - * |
|
| 105 | - * @param string $key |
|
| 106 | - * @return bool true if the session map contains a mapping for the specified key. |
|
| 107 | - */ |
|
| 108 | - public static function containsKey($key) |
|
| 109 | - { |
|
| 110 | - return isset(static::getInstance()->session[$key]); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Returns true if the session map maps one or more keys to the specified value. |
|
| 115 | - * |
|
| 116 | - * @param string $value |
|
| 117 | - * @return bool true if the session map maps one or more keys to the specified value. |
|
| 118 | - */ |
|
| 119 | - public static function containsValue($value) |
|
| 120 | - { |
|
| 121 | - return in_array($value, static::getInstance()->session); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
| 126 | - * |
|
| 127 | - * @param string $key |
|
| 128 | - * @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
| 129 | - */ |
|
| 130 | - public static function get($key) |
|
| 131 | - { |
|
| 132 | - return static::containsKey($key) ? static::getInstance()->session[$key] : null; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Associates the specified value with the specified key in the session map. |
|
| 137 | - * |
|
| 138 | - * @param string $key |
|
| 139 | - * @param string $value |
|
| 140 | - * @return null |
|
| 141 | - */ |
|
| 142 | - public static function set($key, $value) |
|
| 143 | - { |
|
| 144 | - static::getInstance()->session[$key] = $value; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Removes the mapping for the specified key from the session map if present. |
|
| 149 | - * |
|
| 150 | - * @param string $key |
|
| 151 | - * @return null |
|
| 152 | - */ |
|
| 153 | - public static function remove($key) |
|
| 154 | - { |
|
| 155 | - if (static::containsKey($key)) { |
|
| 156 | - unset(static::getInstance()->session[$key]); |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Removes all of the mappings from the session map. |
|
| 162 | - * |
|
| 163 | - * @return null |
|
| 164 | - */ |
|
| 165 | - public static function clear() |
|
| 166 | - { |
|
| 167 | - static::getInstance()->session = []; |
|
| 168 | - } |
|
| 22 | + use SingletonTrait; |
|
| 23 | + |
|
| 24 | + /** @var array The session. */ |
|
| 25 | + private $session; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Construct a Sesssion object. |
|
| 29 | + * |
|
| 30 | + * @global array $_SESSION The session parameters. |
|
| 31 | + */ |
|
| 32 | + protected function __construct() |
|
| 33 | + { |
|
| 34 | + $this->session = &$_SESSION; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Initialize the session, if none exists. |
|
| 39 | + * |
|
| 40 | + * @param $name = null |
|
| 41 | + * @return null |
|
| 42 | + * @throws \RuntimeException on failure. |
|
| 43 | + */ |
|
| 44 | + public static function start($name = null) |
|
| 45 | + { |
|
| 46 | + if (session_status() !== PHP_SESSION_NONE) { |
|
| 47 | + throw new \RuntimeException('Can\'t start a new session.'); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + if ($name !== null) { |
|
| 51 | + session_name($name); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + session_start(); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Destroy the session, if one exists. |
|
| 59 | + * |
|
| 60 | + * @return null |
|
| 61 | + * @throws \RuntimeException on failure. |
|
| 62 | + */ |
|
| 63 | + public static function destroy() |
|
| 64 | + { |
|
| 65 | + if (session_status() !== PHP_SESSION_ACTIVE) { |
|
| 66 | + throw new \RuntimeException('Can\'t destroy the session. There is no active session.'); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + if (!session_destroy()) { |
|
| 70 | + throw new \RuntimeException('Failed to destroy the active session.'); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * {@inheritdoc} |
|
| 76 | + */ |
|
| 77 | + public function getIterator() |
|
| 78 | + { |
|
| 79 | + return new \ArrayIterator(static::getInstance()->session); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Returns the number of key-value mappings in the session map. |
|
| 84 | + * |
|
| 85 | + * @return int the number of key-value mappings in the session map. |
|
| 86 | + */ |
|
| 87 | + public static function count() |
|
| 88 | + { |
|
| 89 | + return count(static::getInstance()->session); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Returns true if the session map contains no key-value mappings. |
|
| 94 | + * |
|
| 95 | + * @return bool true if the session map contains no key-value mappings. |
|
| 96 | + */ |
|
| 97 | + public static function isEmpty() |
|
| 98 | + { |
|
| 99 | + return empty(static::getInstance()->session); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Returns true if the session map contains a mapping for the specified key. |
|
| 104 | + * |
|
| 105 | + * @param string $key |
|
| 106 | + * @return bool true if the session map contains a mapping for the specified key. |
|
| 107 | + */ |
|
| 108 | + public static function containsKey($key) |
|
| 109 | + { |
|
| 110 | + return isset(static::getInstance()->session[$key]); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Returns true if the session map maps one or more keys to the specified value. |
|
| 115 | + * |
|
| 116 | + * @param string $value |
|
| 117 | + * @return bool true if the session map maps one or more keys to the specified value. |
|
| 118 | + */ |
|
| 119 | + public static function containsValue($value) |
|
| 120 | + { |
|
| 121 | + return in_array($value, static::getInstance()->session); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
| 126 | + * |
|
| 127 | + * @param string $key |
|
| 128 | + * @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
|
| 129 | + */ |
|
| 130 | + public static function get($key) |
|
| 131 | + { |
|
| 132 | + return static::containsKey($key) ? static::getInstance()->session[$key] : null; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Associates the specified value with the specified key in the session map. |
|
| 137 | + * |
|
| 138 | + * @param string $key |
|
| 139 | + * @param string $value |
|
| 140 | + * @return null |
|
| 141 | + */ |
|
| 142 | + public static function set($key, $value) |
|
| 143 | + { |
|
| 144 | + static::getInstance()->session[$key] = $value; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Removes the mapping for the specified key from the session map if present. |
|
| 149 | + * |
|
| 150 | + * @param string $key |
|
| 151 | + * @return null |
|
| 152 | + */ |
|
| 153 | + public static function remove($key) |
|
| 154 | + { |
|
| 155 | + if (static::containsKey($key)) { |
|
| 156 | + unset(static::getInstance()->session[$key]); |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Removes all of the mappings from the session map. |
|
| 162 | + * |
|
| 163 | + * @return null |
|
| 164 | + */ |
|
| 165 | + public static function clear() |
|
| 166 | + { |
|
| 167 | + static::getInstance()->session = []; |
|
| 168 | + } |
|
| 169 | 169 | } |