Skip to content

Commit 9616b96

Browse files
authored
Add symfony debug bar support (#35)
1 parent 4785053 commit 9616b96

11 files changed

Lines changed: 329 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
## [2.2.0] - 2026-04-20
66
- Added: support for modern twig layouts of contao 5.7 ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
77
- Added: EntrypointsBuilder concept for retriving current page entrypoints ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
8+
- Added: support for symfony debug bar ([#35](https://github.com/heimrichhannot/contao-encore-bundle/pull/35))
89
- Changed: add constant for default field name ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
910
- Fixed: removed dead or unnecessary code and checks ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
1011
- Deprecated: `src/Asset/PageEntrypoints.php`, `src/Asset/TemplateAsset.php` and `src/Asset/TemplateAssetGenerator.php` ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Contao Encore Bundle
1+
# ![waypoints.svg](docs/img/waypoints.svg) Contao Encore Bundle
22
[![Latest Stable Version](https://img.shields.io/packagist/v/heimrichhannot/contao-encore-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-encore-bundle)
33
[![Total Downloads](https://img.shields.io/packagist/dt/heimrichhannot/contao-encore-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-encore-bundle)
44
![CI](https://github.com/heimrichhannot/contao-encore-bundle/workflows/CI/badge.svg)
55
[![Coverage Status](https://coveralls.io/repos/github/heimrichhannot/contao-encore-bundle/badge.svg?branch=master)](https://coveralls.io/github/heimrichhannot/contao-encore-bundle?branch=master)
6-
![](https://img.shields.io/badge/PHPStan-level%204-brightgreen.svg?style=flat)
6+
![](https://img.shields.io/badge/PHPStan-level%205-brightgreen.svg?style=flat)
77

88
Use the power and simplicity of symfony webpack encore in contao. This bundle let you decide on layout and page level, which encore entries should be loaded. If you want more, you can prepare your bundles define their own encore entries, so never need to manually add or remove encore entries again.
99

config/services.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use HeimrichHannot\EncoreBundle\Asset\FrontendAsset;
1212
use HeimrichHannot\EncoreBundle\Asset\TemplateAsset;
13+
use HeimrichHannot\EncoreBundle\DataCollector\EncoreCollector;
1314
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
1415
use Psr\Cache\CacheItemPoolInterface;
1516
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
@@ -43,11 +44,15 @@
4344

4445
$services
4546
->alias('huh.encore.asset.frontend', FrontendAsset::class)
46-
->public()
47+
->public()
4748
;
4849

4950
$services
5051
->alias('huh.encore.asset.template', TemplateAsset::class)
51-
->public()
52+
->public()
5253
;
54+
55+
$services->set(EncoreCollector::class)
56+
->autoconfigure()
57+
->autowire();
5358
};

contao/templates/twig/.twig-root

Whitespace-only changes.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{% extends '@WebProfiler/Profiler/layout.html.twig' %}
2+
3+
{% block toolbar %}
4+
5+
{% set icon %}
6+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
7+
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
8+
class="lucide lucide-waypoints-icon lucide-waypoints"><path d="m10.586 5.414-5.172 5.172"/><path
9+
d="m18.586 13.414-5.172 5.172"/><path d="M6 12h12"/><circle cx="12" cy="20" r="2"/><circle
10+
cx="12" cy="4" r="2"/><circle cx="20" cy="12" r="2"/><circle cx="4" cy="12" r="2"/></svg>
11+
<span class="sf-toolbar-value">{{ collector.entries|length }}</span>
12+
{% endset %}
13+
14+
{% set text %}
15+
<div class="sf-toolbar-info-group">
16+
<div class="sf-toolbar-info-piece">
17+
<b>Enabled</b>
18+
<span class="sf-toolbar-status sf-toolbar-status-{% if collector.enabled %}green{% else %}red{% endif %}">{{ collector.enabled ? 'yes' : 'no' }}</span>
19+
</div>
20+
21+
<div class="sf-toolbar-info-piece">
22+
<b>Encore Entries</b>
23+
<span class="sf-toolbar-status">{{ collector.entries|length }}</span>
24+
</div>
25+
26+
<div class="sf-toolbar-info-piece">
27+
<b>Encore Extensions</b>
28+
<span class="sf-toolbar-status">{{ collector.extensions|length }}</span>
29+
</div>
30+
</div>
31+
<div class="sf-toolbar-info-group">
32+
<div class="sf-toolbar-info-piece">
33+
<b>Resources</b>
34+
<span><a href="https://github.com/heimrichhannot/contao-encore-bundle" target="_blank" rel="help noreferrer noopener">Read the encore bundle docs</a></span>
35+
</div>
36+
</div>
37+
{% endset %}
38+
39+
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', {
40+
link: true,
41+
additional_classes: (collector.enabled ? '' : 'sf-toolbar-status-red') }
42+
) }}
43+
44+
{% endblock %}
45+
46+
{% block menu %}
47+
<span class="label">
48+
<span class="icon">
49+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
50+
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
51+
class="lucide lucide-waypoints-icon lucide-waypoints"><path d="m10.586 5.414-5.172 5.172"/><path
52+
d="m18.586 13.414-5.172 5.172"/><path d="M6 12h12"/><circle cx="12" cy="20" r="2"/><circle
53+
cx="12" cy="4" r="2"/><circle cx="20" cy="12" r="2"/><circle cx="4" cy="12" r="2"/></svg>
54+
</span>
55+
<strong>Encore</strong>
56+
</span>
57+
{% endblock %}
58+
59+
{% block panel %}
60+
<h2>Encore</h2>
61+
62+
<div class="metrics">
63+
<div class="metric">
64+
<span class="value status-{{ collector.enabled ? 'success' : 'danger' }}">{{ collector.enabled ? 'yes' : 'no' }}</span>
65+
<span class="label">Enabled</span>
66+
</div>
67+
68+
<div class="metric">
69+
<span class="value">{{ collector.version }}</span>
70+
<span class="label">Encore Bundle version</span>
71+
</div>
72+
73+
<div class="metric">
74+
<span class="value">{{ collector.extensions|length }}</span>
75+
<span class="label">Registered extensions</span>
76+
</div>
77+
78+
</div>
79+
80+
81+
<h2>Page entry points</h2>
82+
83+
<table>
84+
<thead>
85+
<tr>
86+
<th>Name</th>
87+
<th>Active</th>
88+
<th>Head</th>
89+
<th>CSS</th>
90+
<th>Origin</th>
91+
<th>Extension</th>
92+
</tr>
93+
</thead>
94+
<tbody>
95+
96+
{% for entry in collector.entries %}
97+
<tr>
98+
<td>{{ entry.name }}</td>
99+
<td>{{ entry.active }}</td>
100+
<td>{{ entry.head }}</td>
101+
<td>{{ entry.requiresCss }}</td>
102+
<td>{{ entry.origin }}</td>
103+
<td>{{ entry.extension }}</td>
104+
</tr>
105+
{% endfor %}
106+
</tbody>
107+
</table>
108+
109+
<h2>Registered extensions</h2>
110+
111+
<table>
112+
<thead>
113+
<tr>
114+
<th>Extension class</th>
115+
<th>Entry Points</th>
116+
</tr>
117+
</thead>
118+
<tbody>
119+
{% for extension in collector.extensions %}
120+
<tr>
121+
<td>{{ extension.name }}</td>
122+
<td>
123+
<ul>
124+
{% for entry in extension.entries %}
125+
<li>{{ entry.name }}</li>
126+
{% endfor %}
127+
</ul>
128+
</td>
129+
</tr>
130+
{% endfor %}
131+
</tbody>
132+
</table>
133+
{% endblock %}

docs/img/waypoints.svg

Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace HeimrichHannot\EncoreBundle\DataCollector;
4+
5+
use Composer\InstalledVersions;
6+
use HeimrichHannot\EncoreBundle\Collection\ExtensionCollection;
7+
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPoints;
8+
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
class EncoreCollector extends AbstractDataCollector
13+
{
14+
public function __construct(
15+
private readonly ExtensionCollection $extensionCollection,
16+
) {
17+
}
18+
19+
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
20+
{
21+
if ($request->attributes->has('encore_entries')) {
22+
$entryPoints = $request->attributes->get('encore_entries');
23+
if (!($entryPoints instanceof EntryPoints)) {
24+
$this->data['enabled'] = false;
25+
} else {
26+
$this->data['entries'] = $entryPoints->all();
27+
$this->data['enabled'] = true;
28+
}
29+
} else {
30+
$this->data['enabled'] = false;
31+
}
32+
33+
$extensions = $this->extensionCollection->getExtensions();
34+
$extensionEntries = [];
35+
foreach ($extensions as $extension) {
36+
$reflection = new \ReflectionClass($extension->getBundle());
37+
$extensionEntries[] = [
38+
'name' => $reflection->getShortName(),
39+
'entries' => $extension->getEntries(),
40+
];
41+
}
42+
43+
$this->data['extensions'] = $extensionEntries;
44+
}
45+
46+
public static function getTemplate(): ?string
47+
{
48+
return '@Contao/data_collector/huh_encore.html.twig';
49+
}
50+
51+
public function isEnabled(): bool
52+
{
53+
return $this->data['enabled'] ?? false;
54+
}
55+
56+
public function getEntries(): array
57+
{
58+
return $this->data['entries'] ?? [];
59+
}
60+
61+
public function getVersion(): string
62+
{
63+
return InstalledVersions::getPrettyVersion('heimrichhannot/contao-encore-bundle') ?? 'unknown';
64+
}
65+
66+
public function getExtensions(): array
67+
{
68+
return $this->data['extensions'] ?? [];
69+
}
70+
}

src/EventListener/Contao/ReplaceDynamicScriptTagsListener.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
1616
use HeimrichHannot\EncoreBundle\Helper\ConfigurationHelper;
1717
use HeimrichHannot\UtilsBundle\Util\Utils;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1819
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
1920

2021
#[AsHook('replaceDynamicScriptTags')]
@@ -27,6 +28,7 @@ public function __construct(
2728
private readonly EntryPointBuilderFactory $entryPointBuilderFactory,
2829
private readonly FrontendAsset $frontendAsset,
2930
private readonly TagRenderer $tagRenderer,
31+
private readonly RequestStack $requestStack,
3032
) {
3133
}
3234

@@ -47,6 +49,12 @@ public function __invoke(string $buffer): string
4749
->setPage($pageModel)
4850
->build();
4951

52+
if ($request = $this->requestStack->getCurrentRequest()) {
53+
$request->attributes->add([
54+
'encore_entries' => $entryPoints,
55+
]);
56+
}
57+
5058
$css = '';
5159
$headJs = '';
5260
$bodyJs = '';

src/EventListener/InjectPageEntriesListener.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
99
use HeimrichHannot\EncoreBundle\Helper\ConfigurationHelper;
1010
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
11+
use Symfony\Component\HttpFoundation\RequestStack;
1112
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
1213

1314
class InjectPageEntriesListener
@@ -18,6 +19,7 @@ public function __construct(
1819
private readonly FrontendAsset $frontendAsset,
1920
private readonly GlobalContaoAsset $globalContaoAsset,
2021
private readonly ConfigurationHelper $configurationHelper,
22+
private readonly RequestStack $requestStack,
2123
) {
2224
}
2325

@@ -36,6 +38,12 @@ public function onLayoutEvent(LayoutEvent $event): void
3638
->setFrontendAsset($this->frontendAsset)
3739
->build();
3840

41+
if ($request = $this->requestStack->getCurrentRequest()) {
42+
$request->attributes->add([
43+
'encore_entries' => $entryPoints,
44+
]);
45+
}
46+
3947
$this->tagRenderer->reset();
4048

4149
foreach ($entryPoints->allActive() as $entrypoint) {

0 commit comments

Comments
 (0)