-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathContext.php
142 lines (124 loc) · 3.53 KB
/
Context.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Upward;
use Laminas\Http\PhpEnvironment\Request;
class Context extends AbstractKeyValueStore
{
/**
* Static values that should always return as-is.
*/
public const BUILTIN_VALUES = [
true,
false,
'GET',
'POST',
'mustache',
'text/html',
'text/plain',
'application/json',
'utf-8',
'utf8',
'latin-1',
'base64',
'binary',
'hex',
];
/** @var string[] */
private $unsetOnClone = [];
/**
* Do not propagate non-cloneable keys.
*/
public function __clone()
{
foreach ($this->unsetOnClone as $key) {
unset($this->data[$key]);
}
}
/**
* Instantiate a Context from a Zend Http Request.
*/
public static function fromRequest(Request $request): self
{
return new self([
'request' => [
'headers' => $request->getHeaders()->toArray(),
'headerEntries' => self::assocToEntries($request->getHeaders()->toArray()),
'queryEntries' => self::assocToEntries($request->getQuery()->toArray()),
'url' => [
'host' => $request->getUri()->getHost(),
'hostname' => $request->getUri()->getHost() . ':' . $request->getUri()->getPort(),
'port' => $request->getUri()->getPort(),
'pathname' => $request->getUri()->getPath(),
'search' => '?' . $request->getUri()->getQuery(),
'query' => $request->getQuery()->toArray(),
],
],
'env' => getenv(),
]);
}
/**
* If $lookup is a builtin value, return it directly.
*
* {@inheritdoc}
*/
public function get($lookup)
{
if ($this->isBuiltinValue($lookup)) {
return $lookup;
}
return parent::get($lookup);
}
/**
* Is $lookup a builtin value or set in the store?
*
* {@inheritdoc}
*/
public function has($lookup): bool
{
return $this->isBuiltinValue($lookup) || parent::has($lookup);
}
/**
* Is $value a built in constant or HTTP status code?
*/
public function isBuiltinValue($value): bool
{
return \in_array($value, self::BUILTIN_VALUES, true) || $this->isStatusCode($value);
}
/**
* Is $value an HTTP status code?
*/
public function isStatusCode($value): bool
{
return (\is_int($value) || (\is_string($value) && ctype_digit($value))) && 100 <= $value && $value < 600;
}
/**
* @throws RuntimeException if $lookup is a built in value
*
* {@inheritdoc}
*/
public function set(string $lookup, $value, bool $cloneable = true): void
{
if ($this->isBuiltinValue($lookup)) {
throw new \RuntimeException('Cannot override a builtin value.');
}
if (!$cloneable) {
$this->unsetOnClone[] = $lookup;
}
parent::set($lookup, $value);
}
/**
* Convert an an array of associative arrays to ['key' => $key, 'value' => $value].
*/
private static function assocToEntries(array $array): array
{
$result = [];
foreach ($array as $key => $value) {
$result[] = compact('key', 'value');
}
return $result;
}
}