|
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\App\Exceptions\BaseException; |
|
18
|
|
|
use Quantum\Http\Constants\ContentType; |
|
19
|
|
|
use Quantum\Environment\Server; |
|
20
|
|
|
use ReflectionException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Trait Internal |
|
24
|
|
|
* @package Quantum\Http\Request |
|
25
|
|
|
*/ |
|
26
|
|
|
trait Internal |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $method |
|
31
|
|
|
* @param string $url |
|
32
|
|
|
* @param array|null $data |
|
33
|
|
|
* @param array|null $files |
|
34
|
|
|
* @return void |
|
35
|
|
|
* @throws BaseException |
|
36
|
|
|
* @throws ReflectionException |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function create(string $method, string $url, array $data = null, array $files = null): void |
|
39
|
|
|
{ |
|
40
|
|
|
$parsed = parse_url($url); |
|
41
|
|
|
|
|
42
|
|
|
$server = Server::getInstance(); |
|
43
|
|
|
|
|
44
|
|
|
$server->set('REQUEST_METHOD', strtoupper($method)); |
|
45
|
|
|
|
|
46
|
|
|
$path = isset($parsed['path']) ? ltrim($parsed['path'], '/') : '/'; |
|
47
|
|
|
|
|
48
|
|
|
$server->set('REQUEST_URI', $path); |
|
49
|
|
|
|
|
50
|
|
|
if (isset($parsed['scheme'])) { |
|
51
|
|
|
$server->set('REQUEST_SCHEME', $parsed['scheme']); |
|
52
|
|
|
$server->set('HTTPS', $parsed['scheme'] === 'https' ? 'on' : 'off'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (isset($parsed['host'])) { |
|
56
|
|
|
$server->set('HTTP_HOST', $parsed['host']); |
|
57
|
|
|
$server->set('SERVER_NAME', $parsed['host']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (isset($parsed['port'])) { |
|
61
|
|
|
$server->set('SERVER_PORT', $parsed['port']); |
|
62
|
|
|
} else { |
|
63
|
|
|
$server->set('SERVER_PORT', ($parsed['scheme'] ?? '') === 'https' ? 443 : 80); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (isset($parsed['query'])) { |
|
67
|
|
|
$server->set('QUERY_STRING', $parsed['query']); |
|
68
|
|
|
} else { |
|
69
|
|
|
$server->set('QUERY_STRING', ''); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
self::detectAndSetContentType($server, $data, $files); |
|
73
|
|
|
|
|
74
|
|
|
static::flush(); |
|
75
|
|
|
|
|
76
|
|
|
static::init(Server::getInstance()); |
|
77
|
|
|
|
|
78
|
|
|
if ($data) { |
|
79
|
|
|
self::setRequestParams($data); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($files) { |
|
83
|
|
|
self::setUploadedFiles(self::handleFiles($files)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Detects the content type |
|
89
|
|
|
* @param $server |
|
90
|
|
|
* @param array|null $data |
|
91
|
|
|
* @param array|null $files |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
protected static function detectAndSetContentType($server, ?array $data = null, ?array $files = null): void |
|
95
|
|
|
{ |
|
96
|
|
|
if ($files && count($files) > 0) { |
|
97
|
|
|
$server->set('CONTENT_TYPE', ContentType::FORM_DATA); |
|
98
|
|
|
} elseif ($data) { |
|
99
|
|
|
$server->set('CONTENT_TYPE', ContentType::URL_ENCODED); |
|
100
|
|
|
} else { |
|
101
|
|
|
$server->set('CONTENT_TYPE', ContentType::HTML); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |