updated backend

This commit is contained in:
Torsten 2020-01-17 10:58:01 +01:00
parent 1fea0f4fe7
commit 11521c2339
3459 changed files with 72735 additions and 168535 deletions

View file

@ -18,13 +18,13 @@ namespace Symfony\Component\HttpFoundation;
*/
class HeaderBag implements \IteratorAggregate, \Countable
{
protected $headers = array();
protected $cacheControl = array();
protected const UPPER = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ';
protected const LOWER = '-abcdefghijklmnopqrstuvwxyz';
/**
* @param array $headers An array of HTTP headers
*/
public function __construct(array $headers = array())
protected $headers = [];
protected $cacheControl = [];
public function __construct(array $headers = [])
{
foreach ($headers as $key => $values) {
$this->set($key, $values);
@ -46,7 +46,7 @@ class HeaderBag implements \IteratorAggregate, \Countable
$max = max(array_map('strlen', array_keys($headers))) + 1;
$content = '';
foreach ($headers as $name => $values) {
$name = implode('-', array_map('ucfirst', explode('-', $name)));
$name = ucwords($name, '-');
foreach ($values as $value) {
$content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
}
@ -58,10 +58,16 @@ class HeaderBag implements \IteratorAggregate, \Countable
/**
* Returns the headers.
*
* @param string|null $key The name of the headers to return or null to get them all
*
* @return array An array of headers
*/
public function all()
public function all(/*string $key = null*/)
{
if (1 <= \func_num_args() && null !== $key = func_get_arg(0)) {
return $this->headers[strtr($key, self::UPPER, self::LOWER)] ?? [];
}
return $this->headers;
}
@ -77,19 +83,15 @@ class HeaderBag implements \IteratorAggregate, \Countable
/**
* Replaces the current HTTP headers by a new set.
*
* @param array $headers An array of HTTP headers
*/
public function replace(array $headers = array())
public function replace(array $headers = [])
{
$this->headers = array();
$this->headers = [];
$this->add($headers);
}
/**
* Adds new headers the current HTTP headers set.
*
* @param array $headers An array of HTTP headers
*/
public function add(array $headers)
{
@ -101,30 +103,31 @@ class HeaderBag implements \IteratorAggregate, \Countable
/**
* Returns a header value by name.
*
* @param string $key The header name
* @param string|string[]|null $default The default value
* @param bool $first Whether to return the first value or all header values
* @param string $key The header name
* @param string|null $default The default value
*
* @return string|string[]|null The first header value or default value if $first is true, an array of values otherwise
* @return string|null The first header value or default value
*/
public function get($key, $default = null, $first = true)
public function get($key, $default = null)
{
$key = str_replace('_', '-', strtolower($key));
$headers = $this->all();
$headers = $this->all((string) $key);
if (2 < \func_num_args()) {
@trigger_error(sprintf('Passing a third argument to "%s()" is deprecated since Symfony 4.4, use method "all()" instead', __METHOD__), E_USER_DEPRECATED);
if (!array_key_exists($key, $headers)) {
if (null === $default) {
return $first ? null : array();
if (!func_get_arg(2)) {
return $headers;
}
return $first ? $default : array($default);
}
if ($first) {
return \count($headers[$key]) ? $headers[$key][0] : $default;
if (!$headers) {
return $default;
}
return $headers[$key];
if (null === $headers[0]) {
return null;
}
return (string) $headers[0];
}
/**
@ -136,7 +139,7 @@ class HeaderBag implements \IteratorAggregate, \Countable
*/
public function set($key, $values, $replace = true)
{
$key = str_replace('_', '-', strtolower($key));
$key = strtr($key, self::UPPER, self::LOWER);
if (\is_array($values)) {
$values = array_values($values);
@ -148,7 +151,7 @@ class HeaderBag implements \IteratorAggregate, \Countable
}
} else {
if (true === $replace || !isset($this->headers[$key])) {
$this->headers[$key] = array($values);
$this->headers[$key] = [$values];
} else {
$this->headers[$key][] = $values;
}
@ -168,7 +171,7 @@ class HeaderBag implements \IteratorAggregate, \Countable
*/
public function has($key)
{
return array_key_exists(str_replace('_', '-', strtolower($key)), $this->all());
return \array_key_exists(strtr($key, self::UPPER, self::LOWER), $this->all());
}
/**
@ -181,7 +184,7 @@ class HeaderBag implements \IteratorAggregate, \Countable
*/
public function contains($key, $value)
{
return \in_array($value, $this->get($key, null, false));
return \in_array($value, $this->all((string) $key));
}
/**
@ -191,22 +194,21 @@ class HeaderBag implements \IteratorAggregate, \Countable
*/
public function remove($key)
{
$key = str_replace('_', '-', strtolower($key));
$key = strtr($key, self::UPPER, self::LOWER);
unset($this->headers[$key]);
if ('cache-control' === $key) {
$this->cacheControl = array();
$this->cacheControl = [];
}
}
/**
* Returns the HTTP header value converted to a date.
*
* @param string $key The parameter key
* @param \DateTime $default The default value
* @param string $key The parameter key
*
* @return null|\DateTime The parsed DateTime or the default value if the header does not exist
* @return \DateTimeInterface|null The parsed DateTime or the default value if the header does not exist
*
* @throws \RuntimeException When the HTTP header is not parseable
*/
@ -245,7 +247,7 @@ class HeaderBag implements \IteratorAggregate, \Countable
*/
public function hasCacheControlDirective($key)
{
return array_key_exists($key, $this->cacheControl);
return \array_key_exists($key, $this->cacheControl);
}
/**
@ -257,7 +259,7 @@ class HeaderBag implements \IteratorAggregate, \Countable
*/
public function getCacheControlDirective($key)
{
return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
return \array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
}
/**
@ -294,21 +296,9 @@ class HeaderBag implements \IteratorAggregate, \Countable
protected function getCacheControlHeader()
{
$parts = array();
ksort($this->cacheControl);
foreach ($this->cacheControl as $key => $value) {
if (true === $value) {
$parts[] = $key;
} else {
if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
$value = '"'.$value.'"';
}
$parts[] = "$key=$value";
}
}
return implode(', ', $parts);
return HeaderUtils::toString($this->cacheControl, ',');
}
/**
@ -320,12 +310,8 @@ class HeaderBag implements \IteratorAggregate, \Countable
*/
protected function parseCacheControl($header)
{
$cacheControl = array();
preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
}
$parts = HeaderUtils::split($header, ',=');
return $cacheControl;
return HeaderUtils::combine($parts);
}
}