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

68publishers / webpack-encore-bundle / 13510931892

25 Feb 2025 12:37AM UTC coverage: 93.452% (+2.1%) from 91.379%
13510931892

push

github

tg666
PHP-Cs-Fixer should be run on PHP 8.3 because the package does not support PHP 8.4

314 of 336 relevant lines covered (93.45%)

0.93 hits per line

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

97.44
/src/Bridge/Nette/Application/ApplicationResponseHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\WebpackEncoreBundle\Bridge\Nette\Application;
6

7
use Nette\Application\Application;
8
use Nette\Application\Response as ApplicationResponse;
9
use Nette\Application\Responses\TextResponse;
10
use Nette\Application\UI\Template;
11
use Nette\Http\IResponse as HttpResponse;
12
use Nette\Utils\Helpers;
13
use ReflectionClass;
14
use SixtyEightPublishers\WebpackEncoreBundle\Asset\EntryPointLookupCollectionInterface;
15
use SixtyEightPublishers\WebpackEncoreBundle\Asset\TagRenderer;
16
use Throwable;
17
use function array_merge;
18
use function assert;
19
use function explode;
20
use function implode;
21
use function is_string;
22
use function sprintf;
23

24
final class ApplicationResponseHandler
25
{
26
    private HttpResponse $response;
27

28
    private TagRenderer $tagRenderer;
29

30
    private EntryPointLookupCollectionInterface $entrypointLookupCollection;
31

32
    /** @var list<string> */
33
    private array $buildNames;
34

35
    /**
36
     * @param list<string> $buildNames
37
     */
38
    public function __construct(HttpResponse $response, TagRenderer $tagRenderer, EntryPointLookupCollectionInterface $entrypointLookupCollection, array $buildNames)
39
    {
40
        $this->response = $response;
1✔
41
        $this->tagRenderer = $tagRenderer;
1✔
42
        $this->entrypointLookupCollection = $entrypointLookupCollection;
1✔
43
        $this->buildNames = $buildNames;
1✔
44
    }
1✔
45

46
    /**
47
     * @param list<string> $buildNames
48
     */
49
    public static function register(Application $application, HttpResponse $response, TagRenderer $tagRenderer, EntryPointLookupCollectionInterface $entryPointLookupCollection, array $buildNames): void
50
    {
51
        $application->onResponse[] = new self($response, $tagRenderer, $entryPointLookupCollection, $buildNames);
1✔
52
    }
1✔
53

54
    /**
55
     * @throws Throwable
56
     */
57
    public function __invoke(Application $application, ApplicationResponse $response): void
58
    {
59
        if (!$response instanceof TextResponse || !$response->getSource() instanceof Template) {
1✔
60
            $this->processLinks();
1✔
61

62
            return;
1✔
63
        }
64

65
        $source = $response->getSource();
1✔
66
        assert($source instanceof Template);
67
        $rendered = Helpers::capture(static fn () => $source->render());
1✔
68

69
        $reflection = new ReflectionClass($response);
1✔
70
        $property = $reflection->getProperty('source');
1✔
71

72
        $property->setValue($response, $rendered);
1✔
73

74
        $this->processLinks();
1✔
75
    }
1✔
76

77
    private function processLinks(): void
78
    {
79
        $defaultAttributes = $this->tagRenderer->getDefaultAttributes();
1✔
80
        $crossOrigin = $defaultAttributes['crossorigin'] ?? null;
1✔
81
        assert(is_string($crossOrigin) || null === $crossOrigin);
82
        $links = [];
1✔
83

84
        foreach ($this->tagRenderer->getRenderedScripts() as $src) {
1✔
85
            $links[] = $this->createLink($src, 'script', $crossOrigin);
1✔
86
        }
87

88
        foreach ($this->tagRenderer->getRenderedStyles() as $href) {
1✔
89
            $links[] = $this->createLink($href, 'style', $crossOrigin);
1✔
90
        }
91

92
        if (empty($links)) {
1✔
93
            return;
×
94
        }
95

96
        $header = $this->response->getHeader('Link') ?? '';
1✔
97
        $links = array_merge(!empty($header) ? explode(',', $header) : [], $links);
1✔
98

99
        $this->response->setHeader('Link', implode(',', $links));
1✔
100

101
        foreach ($this->buildNames as $buildName) {
1✔
102
            $this->entrypointLookupCollection->getEntrypointLookup($buildName)->reset();
1✔
103
        }
104
    }
1✔
105

106
    private function createLink(string $link, string $as, ?string $crossOrigin): string
107
    {
108
        $attributes = [
1✔
109
            '',
1✔
110
            'rel="preload"',
1✔
111
            sprintf('as="%s"', $as),
1✔
112
        ];
113

114
        if (null !== $crossOrigin) {
1✔
115
            $attributes[] = sprintf('crossorigin="%s"', $crossOrigin);
1✔
116
        }
117

118
        return sprintf('<%s>%s', $link, implode('; ', $attributes));
1✔
119
    }
120
}
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

© 2025 Coveralls, Inc