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

heimrichhannot / contao-encore-bundle / 24727905195

21 Apr 2026 02:25PM UTC coverage: 77.228% (+3.6%) from 73.58%
24727905195

push

github

web-flow
Add response context support (#36)

76 of 80 new or added lines in 6 files covered. (95.0%)

624 of 808 relevant lines covered (77.23%)

2.35 hits per line

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

92.11
/src/EventListener/InjectPageEntriesListener.php
1
<?php
2

3
namespace HeimrichHannot\EncoreBundle\EventListener;
4

5
use Contao\CoreBundle\Event\LayoutEvent;
6
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
7
use HeimrichHannot\EncoreBundle\Asset\GlobalContaoAsset;
8
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
9
use HeimrichHannot\EncoreBundle\Helper\ConfigurationHelper;
10
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
13

14
class InjectPageEntriesListener
15
{
16
    public function __construct(
17
        private readonly TagRenderer $tagRenderer,
18
        private readonly EntryPointBuilderFactory $entrypointBuilderFactory,
19
        private readonly GlobalContaoAsset $globalContaoAsset,
20
        private readonly ConfigurationHelper $configurationHelper,
21
        private readonly RequestStack $requestStack,
22
        private readonly ResponseContextAccessor $responseContextAccessor,
23
    ) {
24
    }
2✔
25

26
    #[AsEventListener]
27
    public function onLayoutEvent(LayoutEvent $event): void
28
    {
29
        if (!$this->configurationHelper->isEnabledOnPage($event->getPage(), $event->getLayout())) {
2✔
30
            return;
1✔
31
        }
32

33
        $loader = function () use ($event): string {
1✔
34
            $this->globalContaoAsset->cleanGlobalArrayFromConfiguration();
1✔
35

36
            $entryPoints = $this->entrypointBuilderFactory->create()
1✔
37
                ->setPage($event->getPage())
1✔
38
                ->setLayout($event->getLayout())
1✔
39
                ->setResponseContext($this->responseContextAccessor->getResponseContext())
1✔
40
                ->build();
1✔
41

42
            if ($request = $this->requestStack->getCurrentRequest()) {
1✔
43
                $request->attributes->add([
1✔
44
                    'encore_entries' => $entryPoints,
1✔
45
                ]);
1✔
46
            }
47

48
            $this->tagRenderer->reset();
1✔
49

50
            foreach ($entryPoints->allActive() as $entrypoint) {
1✔
51
                if ($entrypoint->requiresCss) {
1✔
52
                    $GLOBALS['TL_HEAD'][] = $this->tagRenderer->renderWebpackLinkTags($entrypoint->name);
1✔
53
                }
54
                if ($entrypoint->head) {
1✔
55
                    $GLOBALS['TL_HEAD'][] = $this->tagRenderer->renderWebpackScriptTags($entrypoint->name);
1✔
56
                } else {
57
                    $GLOBALS['TL_BODY'][] = $this->tagRenderer->renderWebpackScriptTags($entrypoint->name);
1✔
58
                }
59
            }
60

61
            return '';
1✔
62
        };
1✔
63

64
        $responseContext = $event->getTemplate()->get('response_context');
1✔
65
        if (!is_object($responseContext)) {
1✔
NEW
66
            $loader();
×
67

NEW
68
            return;
×
69
        }
70

71
        $responseContext = new class($responseContext, $loader) {
1✔
72
            public function __construct(
73
                private $responseContext,
74
                private readonly \Closure $loader,
75
            ) {
76
            }
1✔
77

78
            public function __get(string $key): mixed
79
            {
80
                if ('end_of_head' === $key) {
1✔
81
                    ($this->loader)();
1✔
82
                }
83

84
                return $this->responseContext->{$key};
1✔
85
            }
86

87
            public function __isset(string $key): bool
88
            {
89
                if ('end_of_head' === $key) {
1✔
90
                    return true;
1✔
91
                }
92

NEW
93
                return isset($this->responseContext->{$key});
×
94
            }
95

96
            public function __call(string $name, array $arguments): mixed
97
            {
98
                return $this->responseContext->{$name}(...$arguments);
1✔
99
            }
100
        };
1✔
101
        $event->getTemplate()->set('response_context', $responseContext);
1✔
102
    }
103
}
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