Completed
Push — master ( e2f106...c79eb3 )
by Nazar
04:11
created

Request::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 6
nc 1
nop 6
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	cs\Request\Cookie,
11
	cs\Request\Data,
12
	cs\Request\Files,
13
	cs\Request\Query,
14
	cs\Request\Server;
15
16
class Request {
17
	use
18
		Singleton,
19
		Cookie,
20
		Data,
21
		Files,
22
		Query,
23
		Server;
24
25
	/**
26
	 * Initialize request object with specified data
27
	 *
28
	 * @param string[]        $server      Typically `$_SERVER`
29
	 * @param array           $query       Typically `$_GET`
30
	 * @param array           $data        Typically `$_POST`
31
	 * @param resource|string $data_stream String, like `php://input` or resource, like `fopen('php://input', 'br')`
32
	 * @param string[]        $cookie      Typically `$_COOKIE`
33
	 * @param array           $files       Typically `$_FILES`; might be like native PHP array `$_FILES` or normalized; each file item MUST contain keys `name`,
34
	 *                                     `type`, `size`, `error` and at least one of `tmp_name` or `stream`
35
	 *
36
	 * @throws ExitException
37
	 */
38
	function init ($server, $query, $data, $data_stream, $cookie, $files) {
39
		$this->init_server($server);
40
		$this->init_query($query);
41
		$this->init_data($data, $data_stream);
42
		$this->init_cookie($cookie);
43
		$this->init_files($files);
44
	}
45
	/**
46
	 * Initialize request object from superglobals `$_SERVER`, `$_GET`, `$_POST`, `$_COOKIE` and `$_FILES` (including parsing `php://input` in case of custom
47
	 * request methods)
48
	 *
49
	 * @throws ExitException
50
	 */
51
	function init_from_globals () {
52
		// Hack: we override out `$_SERVER`, so conversion from iterator to an array is needed
53
		$this->init_server(iterator_to_array($_SERVER));
54
		$this->init_query($_GET);
55
		$this->init_data($this->init_from_globals_get_data(), 'php://input');
56
		$this->init_cookie($_COOKIE);
57
		// TODO: parse input stream for handling files when using request methods other than POST
58
		$this->init_files($_FILES);
59
	}
60
	/**
61
	 * Parse data from input stream if necessary (JSON, custom request methods)
62
	 *
63
	 * `$this->init_server()` assumed to be called already
64
	 *
65
	 * @return array
66
	 */
67
	protected function init_from_globals_get_data () {
68
		/**
69
		 * Support for JSON requests and/or request methods different than POST
70
		 */
71
		if (preg_match('#^application/([^+\s]+\+)?json#', $this->content_type)) {
72
			return _json_decode(@file_get_contents('php://input')) ?: [];
73
		} elseif (
74
			$this->method !== 'POST' &&
75
			strpos($this->content_type, 'application/x-www-form-urlencoded') === 0
76
		) {
77
			@parse_str(file_get_contents('php://input'), $result);
78
			return $result;
79
		}
80
		return $_POST;
81
	}
82
}
83