|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.8 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Http\Traits\Request; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Config\Exceptions\ConfigException; |
|
18
|
|
|
use Quantum\App\Exceptions\BaseException; |
|
19
|
|
|
use Quantum\Http\Constants\ContentType; |
|
20
|
|
|
use Quantum\Di\Exceptions\DiException; |
|
21
|
|
|
use Quantum\Environment\Server; |
|
22
|
|
|
use ReflectionException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Trait Internal |
|
26
|
|
|
* @package Quantum\Http\Request |
|
27
|
|
|
*/ |
|
28
|
|
|
trait Internal |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Creates an internal request for testing purposes |
|
33
|
|
|
* @param string $method |
|
34
|
|
|
* @param string $url |
|
35
|
|
|
* @param array $params |
|
36
|
|
|
* @param array $headers |
|
37
|
|
|
* @param array $files |
|
38
|
|
|
* @throws BaseException |
|
39
|
|
|
* @throws ReflectionException |
|
40
|
|
|
* @throws ConfigException |
|
41
|
|
|
* @throws DiException |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function create( |
|
44
|
|
|
string $method, |
|
45
|
|
|
string $url, |
|
46
|
|
|
array $params = [], |
|
47
|
|
|
array $headers = [], |
|
48
|
|
|
array $files = [] |
|
49
|
|
|
) |
|
50
|
|
|
{ |
|
51
|
|
|
$parsed = parse_url($url); |
|
52
|
|
|
|
|
53
|
|
|
$server = Server::getInstance(); |
|
54
|
|
|
|
|
55
|
|
|
$server->flush(); |
|
56
|
|
|
|
|
57
|
|
|
$server->set('REQUEST_METHOD', strtoupper($method)); |
|
58
|
|
|
|
|
59
|
|
|
$path = isset($parsed['path']) ? ltrim($parsed['path'], '/') : '/'; |
|
60
|
|
|
|
|
61
|
|
|
$server->set('REQUEST_URI', $path); |
|
62
|
|
|
|
|
63
|
|
|
if (isset($parsed['scheme'])) { |
|
64
|
|
|
$server->set('REQUEST_SCHEME', $parsed['scheme']); |
|
65
|
|
|
$server->set('HTTPS', $parsed['scheme'] === 'https' ? 'on' : 'off'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (isset($parsed['host'])) { |
|
69
|
|
|
$server->set('HTTP_HOST', $parsed['host']); |
|
70
|
|
|
$server->set('SERVER_NAME', $parsed['host']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (isset($parsed['port'])) { |
|
74
|
|
|
$server->set('SERVER_PORT', $parsed['port']); |
|
75
|
|
|
} else { |
|
76
|
|
|
$server->set('SERVER_PORT', ($parsed['scheme'] ?? '') === 'https' ? 443 : 80); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (isset($parsed['query'])) { |
|
80
|
|
|
$server->set('QUERY_STRING', $parsed['query']); |
|
81
|
|
|
} else { |
|
82
|
|
|
$server->set('QUERY_STRING', ''); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
self::detectAndSetContentType($server, $params, $files); |
|
86
|
|
|
|
|
87
|
|
|
if ($headers) { |
|
|
|
|
|
|
88
|
|
|
foreach ($headers as $name => $value) { |
|
89
|
|
|
$server->set('HTTP_' . strtoupper($name), $value); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
self::flush(); |
|
94
|
|
|
|
|
95
|
|
|
self::init($server); |
|
96
|
|
|
|
|
97
|
|
|
if ($params) { |
|
|
|
|
|
|
98
|
|
|
self::setRequestParams($params); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($files) { |
|
|
|
|
|
|
102
|
|
|
self::setUploadedFiles(self::handleFiles($files)); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Detects the content type |
|
108
|
|
|
* @param $server |
|
109
|
|
|
* @param array|null $data |
|
110
|
|
|
* @param array|null $files |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
protected static function detectAndSetContentType($server, ?array $data = null, ?array $files = null): void |
|
114
|
|
|
{ |
|
115
|
|
|
if ($files && count($files) > 0) { |
|
116
|
|
|
$server->set('CONTENT_TYPE', ContentType::FORM_DATA); |
|
117
|
|
|
} elseif ($data) { |
|
118
|
|
|
$server->set('CONTENT_TYPE', ContentType::URL_ENCODED); |
|
119
|
|
|
} else { |
|
120
|
|
|
$server->set('CONTENT_TYPE', ContentType::HTML); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.