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

heimrichhannot / contao-encore-bundle / 24726753103

21 Apr 2026 02:02PM UTC coverage: 77.273% (+3.7%) from 73.58%
24726753103

Pull #36

github

web-flow
Merge 542968519 into 9616b9688
Pull Request #36: Add response context support

82 of 86 new or added lines in 7 files covered. (95.35%)

2 existing lines in 1 file now uncovered.

629 of 814 relevant lines covered (77.27%)

2.34 hits per line

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

90.48
/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 ('regular' !== $event->getPage()->type) {
2✔
30
            return;
1✔
31
        }
32

33
        if ($event->getLayout()->customOption) {
1✔
34
            $event->getTemplate()->set('customAttribute', 'Lorem Ipsum');
1✔
35
        }
36

37
        if (!$this->configurationHelper->isEnabledOnPage($event->getPage(), $event->getLayout())) {
1✔
38
            return;
×
39
        }
40

41
        $loader = function () use ($event): string {
1✔
42
            $this->globalContaoAsset->cleanGlobalArrayFromConfiguration();
1✔
43

44
            $entryPoints = $this->entrypointBuilderFactory->create()
1✔
45
                ->setPage($event->getPage())
1✔
46
                ->setLayout($event->getLayout())
1✔
47
                ->setResponseContext($this->responseContextAccessor->getResponseContext())
1✔
48
                ->build();
1✔
49

50
            if ($request = $this->requestStack->getCurrentRequest()) {
1✔
51
                $request->attributes->add([
1✔
52
                    'encore_entries' => $entryPoints,
1✔
53
                ]);
1✔
54
            }
55

56
            $this->tagRenderer->reset();
1✔
57

58
            foreach ($entryPoints->allActive() as $entrypoint) {
1✔
59
                if ($entrypoint->requiresCss) {
1✔
60
                    $GLOBALS['TL_HEAD'][] = $this->tagRenderer->renderWebpackLinkTags($entrypoint->name);
1✔
61
                }
62
                if ($entrypoint->head) {
1✔
63
                    $GLOBALS['TL_HEAD'][] = $this->tagRenderer->renderWebpackScriptTags($entrypoint->name);
1✔
64
                } else {
65
                    $GLOBALS['TL_BODY'][] = $this->tagRenderer->renderWebpackScriptTags($entrypoint->name);
1✔
66
                }
67
            }
68

69
            return '';
1✔
70
        };
1✔
71

72
        $responseContext = $event->getTemplate()->get('response_context');
1✔
73
        if (!is_object($responseContext)) {
1✔
NEW
74
            $loader();
×
75

NEW
76
            return;
×
77
        }
78

79
        $responseContext = new class($responseContext, $loader) {
1✔
80
            public function __construct(
81
                private $responseContext,
82
                private readonly \Closure $loader,
83
            ) {
84
            }
1✔
85

86
            public function __get(string $key): mixed
87
            {
88
                if ('end_of_head' === $key) {
1✔
89
                    ($this->loader)();
1✔
90
                }
91

92
                return $this->responseContext->{$key};
1✔
93
            }
94

95
            public function __isset(string $key): bool
96
            {
97
                if ('end_of_head' === $key) {
1✔
98
                    return true;
1✔
99
                }
100

NEW
101
                return isset($this->responseContext->{$key});
×
102
            }
103

104
            public function __call(string $name, array $arguments): mixed
105
            {
106
                return $this->responseContext->{$name}(...$arguments);
1✔
107
            }
108
        };
1✔
109
        $event->getTemplate()->set('response_context', $responseContext);
1✔
110
    }
111
}
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