|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Facile\ZFLinkHeadersModule\Listener; |
|
6
|
|
|
|
|
7
|
|
|
use Zend\Http\Header\GenericMultiHeader; |
|
8
|
|
|
use Zend\Http\Header\HeaderInterface; |
|
9
|
|
|
use Zend\Http\Header\HeaderValue; |
|
10
|
|
|
use Zend\Http\PhpEnvironment\Response; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractLinkHandler implements LinkHandlerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
const RFC_PARAMS = [ |
|
15
|
|
|
'rel', |
|
16
|
|
|
'anchor', |
|
17
|
|
|
'rev', |
|
18
|
|
|
'hreflang', |
|
19
|
|
|
'media', |
|
20
|
|
|
'title', |
|
21
|
|
|
'type', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get the attribute string for the header |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
* @param string $value |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
12 |
|
protected function getAttributeForHeader(string $name, $value): string |
|
32
|
|
|
{ |
|
33
|
12 |
|
$name = \strtolower($name); |
|
34
|
|
|
// all RFC params must have a value, but not link-extensions |
|
35
|
12 |
|
if (null === $value && ! \in_array($name, self::RFC_PARAMS, true)) { |
|
36
|
7 |
|
return $name; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
12 |
|
return \sprintf('%s="%s"', $name, \addslashes(HeaderValue::filter($value ?: null))); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns the link header |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $attributes |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
12 |
|
protected function createLinkHeaderValue(array $attributes): string |
|
49
|
|
|
{ |
|
50
|
12 |
|
$href = $attributes['href']; |
|
51
|
12 |
|
unset($attributes['href']); |
|
52
|
|
|
|
|
53
|
12 |
|
$attributeLines = []; |
|
54
|
12 |
|
foreach ($attributes as $name => $value) { |
|
55
|
12 |
|
$attributeLines[] = $this->getAttributeForHeader($name, $value); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
12 |
|
return \sprintf('<%s>; %s', $href, \implode('; ', $attributeLines)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param Response $response |
|
63
|
|
|
* @param array $values |
|
64
|
|
|
*/ |
|
65
|
15 |
|
protected function addLinkHeader(Response $response, array $values) |
|
66
|
|
|
{ |
|
67
|
15 |
|
if (! \count($values)) { |
|
68
|
3 |
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
12 |
|
$header = new GenericMultiHeader('Link', \implode(', ', $values)); |
|
72
|
12 |
|
$currentHeader = $response->getHeaders()->get($header->getFieldName()); |
|
73
|
|
|
|
|
74
|
|
|
/** @var HeaderInterface[] $headers */ |
|
75
|
12 |
|
$headers = []; |
|
76
|
12 |
|
if ($currentHeader instanceof \ArrayIterator) { |
|
77
|
1 |
|
$headers = \iterator_to_array($currentHeader); |
|
78
|
11 |
|
} elseif (false !== $currentHeader) { |
|
79
|
3 |
|
$headers[] = $currentHeader; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
12 |
|
foreach ($headers as $headerItem) { |
|
83
|
4 |
|
$response->getHeaders()->removeHeader($headerItem); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
12 |
|
$headers[] = $header; |
|
87
|
|
|
|
|
88
|
12 |
|
$headerValues = \array_map( |
|
89
|
12 |
|
function (HeaderInterface $header) { |
|
90
|
12 |
|
return $header->getFieldValue(); |
|
91
|
12 |
|
}, |
|
92
|
12 |
|
$headers |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
12 |
|
$response->getHeaders()->addHeader(new GenericMultiHeader($header->getFieldName(), \implode(', ', $headerValues))); |
|
96
|
12 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|