1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Http\Message\Factory; |
15
|
|
|
|
16
|
|
|
use function array_key_exists; |
17
|
|
|
use function str_replace; |
18
|
|
|
use function strtolower; |
19
|
|
|
use function substr; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Abstract Class HeadersFactory. |
23
|
|
|
* |
24
|
|
|
* @author Melech Mizrachi |
25
|
|
|
*/ |
26
|
|
|
abstract class HeaderFactory |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Marshal headers from $_SERVER. |
30
|
|
|
* |
31
|
|
|
* @param array<string, string> $server |
32
|
|
|
* |
33
|
|
|
* @return array<string, string[]> |
34
|
|
|
*/ |
35
|
|
|
public static function marshalHeaders(array $server): array |
36
|
|
|
{ |
37
|
|
|
$headers = []; |
38
|
|
|
|
39
|
|
|
foreach ($server as $key => $value) { |
40
|
|
|
// Apache prefixes environment variables with REDIRECT_ |
41
|
|
|
// if they are added by rewrite rules |
42
|
|
|
if (str_starts_with($key, 'REDIRECT_')) { |
43
|
|
|
$key = substr($key, 9); |
44
|
|
|
|
45
|
|
|
// We will not overwrite existing variables with the |
46
|
|
|
// prefixed versions, though |
47
|
|
|
if (array_key_exists($key, $server)) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($value && str_starts_with($key, 'HTTP_')) { |
53
|
|
|
$name = str_replace('_', '-', strtolower(substr($key, 5))); |
54
|
|
|
$headers[$name] = [$value]; |
55
|
|
|
|
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($value && str_starts_with($key, 'CONTENT_')) { |
60
|
|
|
$name = 'content-' . strtolower(substr($key, 8)); |
61
|
|
|
$headers[$name] = [$value]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $headers; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|