• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tito10047 / progressive-image-bundle / 20675022582

03 Jan 2026 08:51AM UTC coverage: 86.782%. Remained the same
20675022582

push

github

tito10047
update copyright in LICENSE.md

604 of 696 relevant lines covered (86.78%)

175.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Event/KernelResponseEventListener.php
1
<?php
2

3
/*
4
 * This file is part of the Progressive Image Bundle.
5
 *
6
 * (c) Jozef Môstka <https://github.com/tito10047/progressive-image-bundle>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Tito10047\ProgressiveImageBundle\Event;
12

13
use Tito10047\ProgressiveImageBundle\Service\PreloadCollector;
14
use Symfony\Component\HttpKernel\Event\ResponseEvent;
15

16
final class KernelResponseEventListener {
17

18
        public function __construct(
19
                private readonly PreloadCollector $preloadCollector,
20
        ) { }
143✔
21

22
        public function __invoke(ResponseEvent $event) {
23
                $response = $event->getResponse();
143✔
24

25
                $preloads = $this->preloadCollector->getUrls();
143✔
26
                if (empty($preloads)) return;
143✔
27

28
                $links = [];
58✔
29
                foreach ($preloads as $url => $attr) {
58✔
30
                        $link = sprintf('<%s>; rel=preload; as=%s; fetchpriority=%s',
58✔
31
                                $url, $attr['as'], $attr['priority']);
58✔
32
                        if (!empty($attr['imagesrcset'])) {
58✔
33
                                $link .= sprintf('; imagesrcset="%s"', $attr['imagesrcset']);
14✔
34
                        }
35
                        if (!empty($attr['imagesizes'])) {
58✔
36
                                $link .= sprintf('; imagesizes="%s"', $attr['imagesizes']);
14✔
37
                        }
38
                        $links[] = $link;
58✔
39
                }
40
                $response->headers->set('Link', implode(', ', $links), false);
58✔
41

42
                $content = $response->getContent();
58✔
43

44
                $html = "";
58✔
45
                foreach ($preloads as $url => $attr) {
58✔
46
                        $html .= sprintf('<link rel="preload" href="%s" as="%s" fetchpriority="%s"',
58✔
47
                                $url, $attr['as'], $attr['priority']);
58✔
48
                        if (!empty($attr['imagesrcset'])) {
58✔
49
                                $html .= sprintf(' imagesrcset="%s"', $attr['imagesrcset']);
14✔
50
                        }
51
                        if (!empty($attr['imagesizes'])) {
58✔
52
                                $html .= sprintf(' imagesizes="%s"', $attr['imagesizes']);
14✔
53
                        }
54
                        $html .= '>';
58✔
55
                }
56

57
                $newContent = str_replace('</head>', $html . '</head>', $content);
58✔
58
                $response->setContent($newContent);
58✔
59
        }
60

61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc