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

heimrichhannot / contao-encore-bundle / 24667931129

20 Apr 2026 01:00PM UTC coverage: 73.04% (-0.8%) from 73.813%
24667931129

push

github

web-flow
Support contao twig slot layouts (#34)

112 of 150 new or added lines in 11 files covered. (74.67%)

7 existing lines in 2 files now uncovered.

531 of 727 relevant lines covered (73.04%)

2.03 hits per line

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

88.68
/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
    {
45
        trigger_deprecation(
1✔
46
            'heimrichhannot/contao-encore-bundle',
1✔
47
            '2.2.0',
1✔
48
            'The method "isEnabledOnCurrentPage" is deprecated since version 2.2.0 and will be removed in version 3.0.0. Please use "isEnabledOnPage" instead.'
1✔
49
        );
1✔
50

51
        $pageModel ??= $this->getPageModel();
1✔
52

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

57
        return $this->isEnabledOnPage($pageModel, $layout);
1✔
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✔
NEW
64
            return $this->dispatchEvent(false, $request);
×
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✔
NEW
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✔
UNCOV
91
            return false;
×
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✔
UNCOV
149
                return false;
×
150
            }
151
        }
152

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

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

161
        return true;
1✔
162
    }
163
}
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