Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
AbstractMessage.php
001 <?php
002 namespace GuzzleHttp\Message;
003
004 use GuzzleHttp\Stream\StreamInterface;
005
006 abstract class AbstractMessage implements MessageInterface
007 {
008 /** @var array HTTP header collection */
009 private $headers = [];
010
011 /** @var array mapping a lowercase header name to its name over the wire */
012 private $headerNames = [];
013
014 /** @var StreamInterface Message body */
015 private $body;
016
017 /** @var string HTTP protocol version of the message */
018 private $protocolVersion = '1.1';
019
020 public function __toString()
021 {
022 return static::getStartLineAndHeaders($this)
023 . "\r\n\r\n" . $this->getBody();
024 }
025
026 public function getProtocolVersion()
027 {
028 return $this->protocolVersion;
029 }
030
031 public function getBody()
032 {
033 return $this->body;
034 }
035
036 public function setBody(StreamInterface $body = null)
037 {
038 if ($body === null) {
039 // Setting a null body will remove the body of the request
040 $this->removeHeader('Content-Length');
041 $this->removeHeader('Transfer-Encoding');
042 }
043
044 $this->body = $body;
045 }
046
047 public function addHeader($header, $value)
048 {
049 if (is_array($value)) {
050 $current = array_merge($this->getHeaderAsArray($header), $value);
051 } else {
052 $current = $this->getHeaderAsArray($header);
053 $current[] = (string) $value;
054 }
055
056 $this->setHeader($header, $current);
057 }
058
059 public function addHeaders(array $headers)
060 {
061 foreach ($headers as $name => $header) {
062 $this->addHeader($name, $header);
063 }
064 }
065
066 public function getHeader($header)
067 {
068 $name = strtolower($header);
069 return isset($this->headers[$name])
070 ? implode(', ', $this->headers[$name])
071 : '';
072 }
073
074 public function getHeaderAsArray($header)
075 {
076 $name = strtolower($header);
077 return isset($this->headers[$name]) ? $this->headers[$name] : [];
078 }
079
080 public function getHeaders()
081 {
082 $headers = [];
083 foreach ($this->headers as $name => $values) {
084 $headers[$this->headerNames[$name]] = $values;
085 }
086
087 return $headers;
088 }
089
090 public function setHeader($header, $value)
091 {
092 $header = trim($header);
093 $name = strtolower($header);
094 $this->headerNames[$name] = $header;
095
096 if (is_array($value)) {
097 foreach ($value as &$v) {
098 $v = trim($v);
099 }
100 $this->headers[$name] = $value;
101 } else {
102 $this->headers[$name] = [trim($value)];
103 }
104 }
105
106 public function setHeaders(array $headers)
107 {
108 $this->headers = $this->headerNames = [];
109 foreach ($headers as $key => $value) {
110 $this->addHeader($key, $value);
111 }
112 }
113
114 public function hasHeader($header)
115 {
116 return isset($this->headers[strtolower($header)]);
117 }
118
119 public function removeHeader($header)
120 {
121 $name = strtolower($header);
122 unset($this->headers[$name], $this->headerNames[$name]);
123 }
124
125 /**
126 * Parse an array of header values containing ";" separated data into an
127 * array of associative arrays representing the header key value pair
128 * data of the header. When a parameter does not contain a value, but just
129 * contains a key, this function will inject a key with a '' string value.
130 *
131 * @param MessageInterface $message That contains the header
132 * @param string $header Header to retrieve from the message
133 *
134 * @return array Returns the parsed header values.
135 */
136 public static function parseHeader(MessageInterface $message, $header)
137 {
138 static $trimmed = "\"' \n\t\r";
139 $params = $matches = [];
140
141 foreach (self::normalizeHeader($message, $header) as $val) {
142 $part = [];
143 foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
144 if (preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) {
145 $m = $matches[0];
146 if (isset($m[1])) {
147 $part[trim($m[0], $trimmed)] = trim($m[1], $trimmed);
148 } else {
149 $part[] = trim($m[0], $trimmed);
150 }
151 }
152 }
153 if ($part) {
154 $params[] = $part;
155 }
156 }
157
158 return $params;
159 }
160
161 /**
162 * Converts an array of header values that may contain comma separated
163 * headers into an array of headers with no comma separated values.
164 *
165 * @param MessageInterface $message That contains the header
166 * @param string $header Header to retrieve from the message
167 *
168 * @return array Returns the normalized header field values.
169 */
170 public static function normalizeHeader(MessageInterface $message, $header)
171 {
172 $h = $message->getHeaderAsArray($header);
173 for ($i = 0, $total = count($h); $i < $total; $i++) {
174 if (strpos($h[$i], ',') === false) {
175 continue;
176 }
177 foreach (preg_split('/,(?=([^"]*"[^"]*")*[^"]*$)/', $h[$i]) as $v) {
178 $h[] = trim($v);
179 }
180 unset($h[$i]);
181 }
182
183 return $h;
184 }
185
186 /**
187 * Gets the start-line and headers of a message as a string
188 *
189 * @param MessageInterface $message
190 *
191 * @return string
192 */
193 public static function getStartLineAndHeaders(MessageInterface $message)
194 {
195 return static::getStartLine($message)
196 . self::getHeadersAsString($message);
197 }
198
199 /**
200 * Gets the headers of a message as a string
201 *
202 * @param MessageInterface $message
203 *
204 * @return string
205 */
206 public static function getHeadersAsString(MessageInterface $message)
207 {
208 $result = '';
209 foreach ($message->getHeaders() as $name => $values) {
210 $result .= "\r\n{$name}: " . implode(', ', $values);
211 }
212
213 return $result;
214 }
215
216 /**
217 * Gets the start line of a message
218 *
219 * @param MessageInterface $message
220 *
221 * @return string
222 * @throws \InvalidArgumentException
223 */
224 public static function getStartLine(MessageInterface $message)
225 {
226 if ($message instanceof RequestInterface) {
227 return trim($message->getMethod() . ' '
228 . $message->getResource())
229 . ' HTTP/' . $message->getProtocolVersion();
230 } elseif ($message instanceof ResponseInterface) {
231 return 'HTTP/' . $message->getProtocolVersion() . ' '
232 . $message->getStatusCode() . ' '
233 . $message->getReasonPhrase();
234 } else {
235 throw new \InvalidArgumentException('Unknown message type');
236 }
237 }
238
239 /**
240 * Accepts and modifies the options provided to the message in the
241 * constructor.
242 *
243 * Can be overridden in subclasses as necessary.
244 *
245 * @param array $options Options array passed by reference.
246 */
247 protected function handleOptions(array &$options)
248 {
249 if (isset($options['protocol_version'])) {
250 $this->protocolVersion = $options['protocol_version'];
251 }
252 }
253 }
254