|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
namespace One; |
|
4
|
|
|
|
|
5
|
|
|
use One\Http\PumpStream; |
|
6
|
|
|
use One\Http\Stream; |
|
7
|
|
|
use Psr\Http\Message\StreamInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* createUriFromString |
|
11
|
|
|
* |
|
12
|
|
|
* @param string $uri |
|
13
|
|
|
* @return \Psr\Http\Message\UriInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
function createUriFromString($uri) |
|
16
|
|
|
{ |
|
17
|
|
|
$parts = parse_url($uri); |
|
18
|
|
|
$scheme = isset($parts['scheme']) ? $parts['scheme'] : ''; |
|
19
|
|
|
$user = isset($parts['user']) ? $parts['user'] : ''; |
|
20
|
|
|
$pass = isset($parts['pass']) ? $parts['pass'] : ''; |
|
21
|
|
|
$host = isset($parts['host']) ? $parts['host'] : ''; |
|
22
|
|
|
$port = isset($parts['port']) ? $parts['port'] : null; |
|
23
|
|
|
$path = isset($parts['path']) ? $parts['path'] : ''; |
|
24
|
|
|
$query = isset($parts['query']) ? $parts['query'] : ''; |
|
25
|
|
|
$fragment = isset($parts['fragment']) ? $parts['fragment'] : ''; |
|
26
|
|
|
return new Uri( |
|
27
|
|
|
$scheme, |
|
28
|
|
|
$host, |
|
29
|
|
|
$port, |
|
30
|
|
|
$path, |
|
31
|
|
|
$query, |
|
32
|
|
|
$fragment, |
|
33
|
|
|
$user, |
|
34
|
|
|
$pass |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* createuriFromServer |
|
40
|
|
|
* |
|
41
|
|
|
* @return \Psr\Http\Message\UriInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
function createuriFromServer() |
|
45
|
|
|
{ |
|
46
|
|
|
$scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
47
|
|
|
$host = !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; |
|
48
|
|
|
$port = empty($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null; |
|
49
|
|
|
$path = (string) parse_url('http://www.example.com/' . $_SERVER['REQUEST_URI'], PHP_URL_PATH); |
|
50
|
|
|
$query = empty($_SERVER['QUERY_STRING']) ? parse_url('http://example.com' . $_SERVER['REQUEST_URI'], PHP_URL_QUERY) : $_SERVER['QUERY_STRING']; |
|
51
|
|
|
$fragment = ''; |
|
52
|
|
|
$user = !empty($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : ''; |
|
53
|
|
|
$password = !empty($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : ''; |
|
54
|
|
|
if (empty($user) && empty($password) && !empty($_SERVER['HTTP_AUTHORIZATION'])) { |
|
55
|
|
|
list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return new Uri( |
|
59
|
|
|
$scheme, |
|
60
|
|
|
$host, |
|
61
|
|
|
$port, |
|
62
|
|
|
$path, |
|
63
|
|
|
$query, |
|
64
|
|
|
$fragment, |
|
65
|
|
|
$user, |
|
66
|
|
|
$password |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Create a new stream based on the input type. |
|
72
|
|
|
* |
|
73
|
|
|
* Options is an associative array that can contain the following keys: |
|
74
|
|
|
* - metadata: Array of custom metadata. |
|
75
|
|
|
* - size: Size of the stream. |
|
76
|
|
|
* |
|
77
|
|
|
* @param mixed $resource Entity body data |
|
78
|
|
|
* @param array $options Additional options |
|
79
|
|
|
* |
|
80
|
|
|
* @return StreamInterface |
|
81
|
|
|
* @throws \InvalidArgumentException if the $resource arg is not valid. |
|
82
|
|
|
*/ |
|
83
|
|
|
function stream_for($resource = '', array $options = []) |
|
84
|
|
|
{ |
|
85
|
|
|
if (is_scalar($resource)) { |
|
86
|
|
|
return openStream($resource, $options); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return createStream($resource, $options); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Helper to create stream based on resource and options |
|
94
|
|
|
* @param mixed $resource |
|
95
|
|
|
* @param array $options |
|
96
|
|
|
* @return StreamInterface |
|
97
|
|
|
* @throws \InvalidArgumentException if the $resource arg is not valid. |
|
98
|
|
|
*/ |
|
99
|
|
|
function createStream($resource, $options) |
|
100
|
|
|
{ |
|
101
|
|
|
switch (gettype($resource)) { |
|
102
|
|
|
case 'resource': |
|
103
|
|
|
return new Stream($resource, $options); |
|
104
|
|
|
case 'object': |
|
105
|
|
|
if ($resource instanceof StreamInterface) { |
|
106
|
|
|
return $resource; |
|
107
|
|
|
} elseif (method_exists($resource, '__toString')) { |
|
108
|
|
|
return stream_for((string) $resource, $options); |
|
109
|
|
|
} |
|
110
|
|
|
return new PumpStream(function () use ($resource) { |
|
111
|
|
|
if (!$resource->valid()) { |
|
112
|
|
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
$result = $resource->current(); |
|
115
|
|
|
$resource->next(); |
|
116
|
|
|
return $result; |
|
117
|
|
|
}, $options); |
|
118
|
|
|
case 'NULL': |
|
119
|
|
|
return new Stream(fopen('php://temp', 'r+'), $options); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (is_callable($resource)) { |
|
123
|
|
|
return new \One\Http\PumpStream($resource, $options); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Copy the contents of a stream into a string until the given number of |
|
131
|
|
|
* bytes have been read. |
|
132
|
|
|
* |
|
133
|
|
|
* @param StreamInterface $stream Stream to read |
|
134
|
|
|
* @param int $maxLen Maximum number of bytes to read. Pass -1 |
|
135
|
|
|
* to read the entire stream. |
|
136
|
|
|
* @return string |
|
137
|
|
|
* @throws \RuntimeException on error. |
|
138
|
|
|
*/ |
|
139
|
|
|
function copy_to_string(StreamInterface $stream, $maxLen = -1) |
|
140
|
|
|
{ |
|
141
|
|
|
$buffer = ''; |
|
142
|
|
|
if ($maxLen === -1) { |
|
143
|
|
|
while (!$stream->eof()) { |
|
144
|
|
|
$buf = $stream->read(1048576); |
|
145
|
|
|
// Using a loose equality here to match on '' and false. |
|
146
|
|
|
if ($buf == null) { |
|
147
|
|
|
break; |
|
148
|
|
|
} |
|
149
|
|
|
$buffer .= $buf; |
|
150
|
|
|
} |
|
151
|
|
|
return $buffer; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$len = 0; |
|
155
|
|
|
while (!$stream->eof() && $len < $maxLen) { |
|
156
|
|
|
$buf = $stream->read($maxLen - $len); |
|
157
|
|
|
// Using a loose equality here to match on '' and false. |
|
158
|
|
|
if ($buf == null) { |
|
159
|
|
|
break; |
|
160
|
|
|
} |
|
161
|
|
|
$buffer .= $buf; |
|
162
|
|
|
$len = strlen($buffer); |
|
163
|
|
|
} |
|
164
|
|
|
return $buffer; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
* Open Stream when resource is a scalar type |
|
|
|
|
|
|
168
|
|
|
* @param mixed $resource |
|
169
|
|
|
* @param array $options |
|
170
|
|
|
* @return StreamInterface |
|
171
|
|
|
*/ |
|
172
|
|
|
function openStream($resource, $options) |
|
173
|
|
|
{ |
|
174
|
|
|
$stream = fopen('php://temp', 'r+'); |
|
175
|
|
|
if ($resource !== '' && $stream !== false) { |
|
176
|
|
|
fwrite($stream, $resource); |
|
177
|
|
|
fseek($stream, 0); |
|
178
|
|
|
} |
|
179
|
|
|
return new Stream($stream, $options); |
|
180
|
|
|
} |
|
181
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.