[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpFoundation; 13 14 /** 15 * HeaderBag is a container for HTTP headers. 16 * 17 * @author Fabien Potencier <fabien@symfony.com> 18 */ 19 class HeaderBag implements \IteratorAggregate, \Countable 20 { 21 protected $headers; 22 protected $cacheControl; 23 24 /** 25 * Constructor. 26 * 27 * @param array $headers An array of HTTP headers 28 */ 29 public function __construct(array $headers = array()) 30 { 31 $this->cacheControl = array(); 32 $this->headers = array(); 33 foreach ($headers as $key => $values) { 34 $this->set($key, $values); 35 } 36 } 37 38 /** 39 * Returns the headers as a string. 40 * 41 * @return string The headers 42 */ 43 public function __toString() 44 { 45 if (!$this->headers) { 46 return ''; 47 } 48 49 $max = max(array_map('strlen', array_keys($this->headers))) + 1; 50 $content = ''; 51 ksort($this->headers); 52 foreach ($this->headers as $name => $values) { 53 $name = implode('-', array_map('ucfirst', explode('-', $name))); 54 foreach ($values as $value) { 55 $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); 56 } 57 } 58 59 return $content; 60 } 61 62 /** 63 * Returns the headers. 64 * 65 * @return array An array of headers 66 */ 67 public function all() 68 { 69 return $this->headers; 70 } 71 72 /** 73 * Returns the parameter keys. 74 * 75 * @return array An array of parameter keys 76 */ 77 public function keys() 78 { 79 return array_keys($this->headers); 80 } 81 82 /** 83 * Replaces the current HTTP headers by a new set. 84 * 85 * @param array $headers An array of HTTP headers 86 */ 87 public function replace(array $headers = array()) 88 { 89 $this->headers = array(); 90 $this->add($headers); 91 } 92 93 /** 94 * Adds new headers the current HTTP headers set. 95 * 96 * @param array $headers An array of HTTP headers 97 */ 98 public function add(array $headers) 99 { 100 foreach ($headers as $key => $values) { 101 $this->set($key, $values); 102 } 103 } 104 105 /** 106 * Returns a header value by name. 107 * 108 * @param string $key The header name 109 * @param mixed $default The default value 110 * @param bool $first Whether to return the first value or all header values 111 * 112 * @return string|array The first header value if $first is true, an array of values otherwise 113 */ 114 public function get($key, $default = null, $first = true) 115 { 116 $key = str_replace('_', '-', strtolower($key)); 117 118 if (!array_key_exists($key, $this->headers)) { 119 if (null === $default) { 120 return $first ? null : array(); 121 } 122 123 return $first ? $default : array($default); 124 } 125 126 if ($first) { 127 return count($this->headers[$key]) ? $this->headers[$key][0] : $default; 128 } 129 130 return $this->headers[$key]; 131 } 132 133 /** 134 * Sets a header by name. 135 * 136 * @param string $key The key 137 * @param string|array $values The value or an array of values 138 * @param bool $replace Whether to replace the actual value or not (true by default) 139 */ 140 public function set($key, $values, $replace = true) 141 { 142 $key = str_replace('_', '-', strtolower($key)); 143 144 $values = array_values((array) $values); 145 146 if (true === $replace || !isset($this->headers[$key])) { 147 $this->headers[$key] = $values; 148 } else { 149 $this->headers[$key] = array_merge($this->headers[$key], $values); 150 } 151 152 if ('cache-control' === $key) { 153 $this->cacheControl = $this->parseCacheControl($values[0]); 154 } 155 } 156 157 /** 158 * Returns true if the HTTP header is defined. 159 * 160 * @param string $key The HTTP header 161 * 162 * @return bool true if the parameter exists, false otherwise 163 */ 164 public function has($key) 165 { 166 return array_key_exists(str_replace('_', '-', strtolower($key)), $this->headers); 167 } 168 169 /** 170 * Returns true if the given HTTP header contains the given value. 171 * 172 * @param string $key The HTTP header name 173 * @param string $value The HTTP value 174 * 175 * @return bool true if the value is contained in the header, false otherwise 176 */ 177 public function contains($key, $value) 178 { 179 return in_array($value, $this->get($key, null, false)); 180 } 181 182 /** 183 * Removes a header. 184 * 185 * @param string $key The HTTP header name 186 */ 187 public function remove($key) 188 { 189 $key = str_replace('_', '-', strtolower($key)); 190 191 unset($this->headers[$key]); 192 193 if ('cache-control' === $key) { 194 $this->cacheControl = array(); 195 } 196 } 197 198 /** 199 * Returns the HTTP header value converted to a date. 200 * 201 * @param string $key The parameter key 202 * @param \DateTime $default The default value 203 * 204 * @return null|\DateTime The parsed DateTime or the default value if the header does not exist 205 * 206 * @throws \RuntimeException When the HTTP header is not parseable 207 */ 208 public function getDate($key, \DateTime $default = null) 209 { 210 if (null === $value = $this->get($key)) { 211 return $default; 212 } 213 214 if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) { 215 throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value)); 216 } 217 218 return $date; 219 } 220 221 /** 222 * Adds a custom Cache-Control directive. 223 * 224 * @param string $key The Cache-Control directive name 225 * @param mixed $value The Cache-Control directive value 226 */ 227 public function addCacheControlDirective($key, $value = true) 228 { 229 $this->cacheControl[$key] = $value; 230 231 $this->set('Cache-Control', $this->getCacheControlHeader()); 232 } 233 234 /** 235 * Returns true if the Cache-Control directive is defined. 236 * 237 * @param string $key The Cache-Control directive 238 * 239 * @return bool true if the directive exists, false otherwise 240 */ 241 public function hasCacheControlDirective($key) 242 { 243 return array_key_exists($key, $this->cacheControl); 244 } 245 246 /** 247 * Returns a Cache-Control directive value by name. 248 * 249 * @param string $key The directive name 250 * 251 * @return mixed|null The directive value if defined, null otherwise 252 */ 253 public function getCacheControlDirective($key) 254 { 255 return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null; 256 } 257 258 /** 259 * Removes a Cache-Control directive. 260 * 261 * @param string $key The Cache-Control directive 262 */ 263 public function removeCacheControlDirective($key) 264 { 265 unset($this->cacheControl[$key]); 266 267 $this->set('Cache-Control', $this->getCacheControlHeader()); 268 } 269 270 /** 271 * Returns an iterator for headers. 272 * 273 * @return \ArrayIterator An \ArrayIterator instance 274 */ 275 public function getIterator() 276 { 277 return new \ArrayIterator($this->headers); 278 } 279 280 /** 281 * Returns the number of headers. 282 * 283 * @return int The number of headers 284 */ 285 public function count() 286 { 287 return count($this->headers); 288 } 289 290 protected function getCacheControlHeader() 291 { 292 $parts = array(); 293 ksort($this->cacheControl); 294 foreach ($this->cacheControl as $key => $value) { 295 if (true === $value) { 296 $parts[] = $key; 297 } else { 298 if (preg_match('#[^a-zA-Z0-9._-]#', $value)) { 299 $value = '"'.$value.'"'; 300 } 301 302 $parts[] = "$key=$value"; 303 } 304 } 305 306 return implode(', ', $parts); 307 } 308 309 /** 310 * Parses a Cache-Control HTTP header. 311 * 312 * @param string $header The value of the Cache-Control HTTP header 313 * 314 * @return array An array representing the attribute values 315 */ 316 protected function parseCacheControl($header) 317 { 318 $cacheControl = array(); 319 preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER); 320 foreach ($matches as $match) { 321 $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true); 322 } 323 324 return $cacheControl; 325 } 326 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |