1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\ZFLinkHeadersModule\Listener; |
6
|
|
|
|
7
|
|
|
use Facile\ZFLinkHeadersModule\OptionsInterface; |
8
|
|
|
use Zend\Http\PhpEnvironment\Response; |
9
|
|
|
use Zend\Mvc\MvcEvent; |
10
|
|
|
use Zend\View\Helper\HeadLink; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class StylesheetHandler |
14
|
|
|
*/ |
15
|
|
|
final class StylesheetHandler extends AbstractLinkHandler |
16
|
|
|
{ |
17
|
|
|
const TYPE_STYLESHEET = 'stylesheet'; |
18
|
|
|
/** |
19
|
|
|
* @var HeadLink |
20
|
|
|
*/ |
21
|
|
|
private $headLink; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var OptionsInterface |
25
|
|
|
*/ |
26
|
|
|
private $options; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* StylesheetHandler constructor. |
30
|
|
|
* |
31
|
|
|
* @param HeadLink $headLink |
32
|
|
|
* @param OptionsInterface $options |
33
|
|
|
*/ |
34
|
8 |
|
public function __construct(HeadLink $headLink, OptionsInterface $options) |
35
|
|
|
{ |
36
|
8 |
|
$this->headLink = $headLink; |
37
|
8 |
|
$this->options = $options; |
38
|
8 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param MvcEvent $event |
42
|
|
|
*/ |
43
|
7 |
|
public function __invoke(MvcEvent $event) |
44
|
|
|
{ |
45
|
7 |
|
if (! $this->options->isStylesheetEnabled()) { |
46
|
1 |
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
$response = $event->getResponse(); |
50
|
6 |
|
if (! $response instanceof Response) { |
51
|
1 |
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
5 |
|
$values = []; |
55
|
|
|
|
56
|
5 |
|
foreach ($this->headLink->getContainer() as $item) { |
57
|
5 |
|
$attributes = \get_object_vars($item); |
58
|
|
|
|
59
|
5 |
|
if (! $this->canInjectLink($attributes)) { |
60
|
5 |
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$attributes = [ |
64
|
3 |
|
'href' => $attributes['href'], |
65
|
3 |
|
'rel' => $this->options->getStylesheetMode(), |
66
|
3 |
|
'as' => 'script', |
67
|
3 |
|
'type' => $attributes['type'] ?? null, |
68
|
3 |
|
'media' => $attributes['media'] ?? null, |
69
|
|
|
]; |
70
|
|
|
|
71
|
3 |
|
$attributes = \array_filter($attributes); |
72
|
|
|
|
73
|
3 |
|
$attributes['rel'] = $this->options->getStylesheetMode(); |
74
|
3 |
|
$attributes['as'] = 'style'; |
75
|
|
|
|
76
|
3 |
|
if (! $this->options->isHttp2PushEnabled()) { |
77
|
2 |
|
$attributes['nopush'] = null; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
$values[] = $this->createLinkHeaderValue($attributes); |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
$this->addLinkHeader($response, $values); |
84
|
5 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Whether the link is valid to be injected in headers |
88
|
|
|
* |
89
|
|
|
* @param array $attributes |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
5 |
|
private function canInjectLink(array $attributes): bool |
93
|
|
|
{ |
94
|
5 |
|
if (empty($attributes['href'] ?? '')) { |
95
|
1 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
return self::TYPE_STYLESHEET === ($attributes['rel'] ?? ''); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|