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

heimrichhannot / contao-encore-bundle / 43902875811

16 Nov 2026 04:01PM UTC coverage: 76.263% (+0.6%) from 75.679%
43902875811

push

github

web-flow
Fix CI and require Contao 5.3 / PHP 8.2 (#42)

* Fix CI and require Contao 5.3 with PHP 8.2

* Remove unreachable legacy output directory fallback

* Remove unused global class loader from test bootstrap

* Isolate default directory test fixture after Claude review

* Remove version prerequisites from README

---------

Co-authored-by: Thomas Körner <koertho@outlook.de>

1 of 1 new or added line in 1 file covered. (100.0%)

11 existing lines in 2 files now uncovered.

649 of 851 relevant lines covered (76.26%)

2.7 hits per line

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

75.47
/src/Helper/ConfigurationHelper.php
1
<?php
2

3
/*
4
 * Copyright (c) 2023 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\EncoreBundle\Helper;
10

11
use Contao\CoreBundle\Framework\ContaoFramework;
12
use Contao\CoreBundle\Routing\ScopeMatcher;
13
use Contao\LayoutModel;
14
use Contao\PageModel;
15
use HeimrichHannot\EncoreBundle\Event\EncoreEnabledEvent;
16
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
17
use Symfony\Component\Filesystem\Path;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21

22
class ConfigurationHelper
23
{
24
    protected array $bundleConfig;
25
    protected string $webDir;
26

27
    public function __construct(
28
        private readonly RequestStack $requestStack,
29
        ParameterBagInterface $parameterBag,
30
        private readonly ScopeMatcher $scopeMatcher,
31
        private readonly ContaoFramework $contaoFramework,
32
        private readonly EventDispatcherInterface $eventDispatcher,
33
    ) {
34
        $this->bundleConfig = $parameterBag->has('huh_encore') ? $parameterBag->get('huh_encore') : [];
4 ✔
35
        $this->webDir = $parameterBag->has('contao.web_dir') ? $parameterBag->get('contao.web_dir') : '';
4 ✔
36
    }
37

38
    /**
39
     * Check if encore is enabled on the current page.
40
     *
41
     * @deprecated
42
     */
43
    public function isEnabledOnCurrentPage(?PageModel $pageModel = null, ?LayoutModel $layout = null): bool
44
    {
UNCOV
45
        trigger_deprecation(
×
UNCOV
46
            'heimrichhannot/contao-encore-bundle',
×
UNCOV
47
            '2.2.0',
×
UNCOV
48
            'The method "isEnabledOnCurrentPage" is deprecated since version 2.2.0 and will be removed in version 3.0.0. Please use "isEnabledOnPage" instead.'
×
UNCOV
49
        );
×
50

UNCOV
51
        $pageModel ??= $this->getPageModel();
×
52

UNCOV
53
        if (null === $pageModel) {
×
UNCOV
54
            return false;
×
55
        }
56

UNCOV
57
        return $this->isEnabledOnPage($pageModel, $layout);
×
58
    }
59

60
    public function isEnabledOnPage(PageModel $page, ?LayoutModel $layout = null): bool
61
    {
62
        $request = $this->requestStack->getCurrentRequest();
1 ✔
63
        if (!$request || !$this->scopeMatcher->isFrontendRequest($request)) {
1 ✔
64
            return $this->dispatchEvent(false, $request);
1 ✔
65
        }
66

67
        if (!$layout) {
1 ✔
68
            $page->loadDetails();
1 ✔
69
            $layout = $this->contaoFramework
1 ✔
70
                ->getAdapter(LayoutModel::class)
1 ✔
71
                ->findByPk($page->layoutId ?? $page->layout);
1 ✔
72
        }
73

74
        if (!$layout?->addEncore) {
1 ✔
75
            return $this->dispatchEvent(false, $request, $page, $layout);
1 ✔
76
        }
77

78
        if ('modern' !== $layout->type) {
1 ✔
79
            if (false === $this->evaluateIsEnabled($page)) {
1 ✔
80
                return $this->dispatchEvent(false, $request, $page, $layout);
×
81
            }
82
        }
83

84
        return $this->dispatchEvent(true, $request, $page, $layout);
1 ✔
85
    }
86

87
    private function dispatchEvent(bool $result, ?Request $request = null, ?PageModel $page = null, ?LayoutModel $layout = null): bool
88
    {
89
        // ToDo: allow request = null in event
90
        if (!$request) {
1 ✔
91
            return false;
1 ✔
92
        }
93

94
        $event = $this->eventDispatcher->dispatch(
1 ✔
95
            new EncoreEnabledEvent($result, $request, $page, $layout)
1 ✔
96
        );
1 ✔
97

98
        return $event->enabled;
1 ✔
99
    }
100

101
    /**
102
     * Return the relative path to the encore output folder.
103
     */
104
    public function getRelativeOutputPath(): string
105
    {
106
        return Path::makeRelative($this->bundleConfig['outputPath'], $this->webDir);
1 ✔
107
    }
108

109
    /**
110
     * Return the absolute path to the encore output folder.
111
     */
112
    public function getAbsoluteOutputPath(): string
113
    {
114
        return $this->bundleConfig['outputPath'];
1 ✔
115
    }
116

117
    public function getPageModel(): ?PageModel
118
    {
119
        $request = $this->requestStack->getCurrentRequest();
2 ✔
120

121
        if (null === $request || !$request->attributes->has('pageModel')) {
2 ✔
122
            return null;
2 ✔
123
        }
124

125
        $pageModel = $request->attributes->get('pageModel');
2 ✔
126

127
        if ($pageModel instanceof PageModel) {
2 ✔
128
            return $pageModel;
2 ✔
129
        }
130

131
        if (
132
            isset($GLOBALS['objPage'])
1 ✔
133
            && $GLOBALS['objPage'] instanceof PageModel
1 ✔
134
            && (int) $GLOBALS['objPage']->id === (int) $pageModel
1 ✔
135
        ) {
136
            return $GLOBALS['objPage'];
1 ✔
137
        }
138

139
        return $this->contaoFramework->getAdapter(PageModel::class)->findByPk((int) $pageModel);
1 ✔
140
    }
141

142
    private function evaluateIsEnabled(?PageModel $pageModel): bool
143
    {
144
        $parentPageModel = $this->getPageModel();
1 ✔
145

146
        // Check if error page
147
        if (null !== $this->requestStack->getParentRequest()) {
1 ✔
148
            if (!$parentPageModel || !\in_array($parentPageModel->type, ['error_401', 'error_403', 'error_404', 'error_503'], true)) {
1 ✔
149
                return false;
×
150
            }
151
        }
152

153
        if (!$pageModel && $parentPageModel) {
1 ✔
154
            $pageModel = $parentPageModel;
×
155
        }
156

157
        if (!$pageModel) {
1 ✔
158
            return false;
×
159
        }
160

161
        return true;
1 ✔
162
    }
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc