Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
## [2.2.0] - 2026-04-20
- Added: support for modern twig layouts of contao 5.7 ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Added: EntrypointsBuilder concept for retriving current page entrypoints ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Added: support for symfony debug bar ([#35](https://github.com/heimrichhannot/contao-encore-bundle/pull/35))
- Changed: add constant for default field name ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Fixed: removed dead or unnecessary code and checks ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
- Deprecated: `src/Asset/PageEntrypoints.php`, `src/Asset/TemplateAsset.php` and `src/Asset/TemplateAssetGenerator.php` ([#34](https://github.com/heimrichhannot/contao-encore-bundle/pull/34))
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Contao Encore Bundle
# ![waypoints.svg](docs/img/waypoints.svg) Contao Encore Bundle
[![Latest Stable Version](https://img.shields.io/packagist/v/heimrichhannot/contao-encore-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-encore-bundle)
[![Total Downloads](https://img.shields.io/packagist/dt/heimrichhannot/contao-encore-bundle.svg)](https://packagist.org/packages/heimrichhannot/contao-encore-bundle)
![CI](https://github.com/heimrichhannot/contao-encore-bundle/workflows/CI/badge.svg)
[![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)
![](https://img.shields.io/badge/PHPStan-level%204-brightgreen.svg?style=flat)
![](https://img.shields.io/badge/PHPStan-level%205-brightgreen.svg?style=flat)

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.

Expand Down
9 changes: 7 additions & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use HeimrichHannot\EncoreBundle\Asset\FrontendAsset;
use HeimrichHannot\EncoreBundle\Asset\TemplateAsset;
use HeimrichHannot\EncoreBundle\DataCollector\EncoreCollector;
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand Down Expand Up @@ -43,11 +44,15 @@

$services
->alias('huh.encore.asset.frontend', FrontendAsset::class)
->public()
->public()
;

$services
->alias('huh.encore.asset.template', TemplateAsset::class)
->public()
->public()
;

$services->set(EncoreCollector::class)
->autoconfigure()
->autowire();
};
Empty file.
133 changes: 133 additions & 0 deletions contao/templates/twig/data_collector/huh_encore.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% block toolbar %}

{% set icon %}
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="lucide lucide-waypoints-icon lucide-waypoints"><path d="m10.586 5.414-5.172 5.172"/><path
d="m18.586 13.414-5.172 5.172"/><path d="M6 12h12"/><circle cx="12" cy="20" r="2"/><circle
cx="12" cy="4" r="2"/><circle cx="20" cy="12" r="2"/><circle cx="4" cy="12" r="2"/></svg>
<span class="sf-toolbar-value">{{ collector.entries|length }}</span>
{% endset %}

{% set text %}
<div class="sf-toolbar-info-group">
<div class="sf-toolbar-info-piece">
<b>Enabled</b>
<span class="sf-toolbar-status sf-toolbar-status-{% if collector.enabled %}green{% else %}red{% endif %}">{{ collector.enabled ? 'yes' : 'no' }}</span>
</div>

<div class="sf-toolbar-info-piece">
<b>Encore Entries</b>
<span class="sf-toolbar-status">{{ collector.entries|length }}</span>
</div>

<div class="sf-toolbar-info-piece">
<b>Encore Extensions</b>
<span class="sf-toolbar-status">{{ collector.extensions|length }}</span>
</div>
</div>
<div class="sf-toolbar-info-group">
<div class="sf-toolbar-info-piece">
<b>Resources</b>
<span><a href="https://github.com/heimrichhannot/contao-encore-bundle" target="_blank" rel="help noreferrer noopener">Read the encore bundle docs</a></span>
</div>
</div>
{% endset %}

{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', {
link: true,
additional_classes: (collector.enabled ? '' : 'sf-toolbar-status-red') }
) }}

{% endblock %}

{% block menu %}
<span class="label">
<span class="icon">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="lucide lucide-waypoints-icon lucide-waypoints"><path d="m10.586 5.414-5.172 5.172"/><path
d="m18.586 13.414-5.172 5.172"/><path d="M6 12h12"/><circle cx="12" cy="20" r="2"/><circle
cx="12" cy="4" r="2"/><circle cx="20" cy="12" r="2"/><circle cx="4" cy="12" r="2"/></svg>
</span>
<strong>Encore</strong>
</span>
{% endblock %}

{% block panel %}
<h2>Encore</h2>

<div class="metrics">
<div class="metric">
<span class="value status-{{ collector.enabled ? 'success' : 'danger' }}">{{ collector.enabled ? 'yes' : 'no' }}</span>
<span class="label">Enabled</span>
</div>

<div class="metric">
<span class="value">{{ collector.version }}</span>
<span class="label">Encore Bundle version</span>
</div>

<div class="metric">
<span class="value">{{ collector.extensions|length }}</span>
<span class="label">Registered extensions</span>
</div>

</div>


<h2>Page entry points</h2>

<table>
<thead>
<tr>
<th>Name</th>
<th>Active</th>
<th>Head</th>
<th>CSS</th>
<th>Origin</th>
<th>Extension</th>
</tr>
</thead>
<tbody>

{% for entry in collector.entries %}
<tr>
<td>{{ entry.name }}</td>
<td>{{ entry.active }}</td>
<td>{{ entry.head }}</td>
<td>{{ entry.requiresCss }}</td>
<td>{{ entry.origin }}</td>
<td>{{ entry.extension }}</td>
</tr>
{% endfor %}
</tbody>
</table>

<h2>Registered extensions</h2>

<table>
<thead>
<tr>
<th>Extension class</th>
<th>Entry Points</th>
</tr>
</thead>
<tbody>
{% for extension in collector.extensions %}
<tr>
<td>{{ extension.name }}</td>
<td>
<ul>
{% for entry in extension.entries %}
<li>{{ entry.name }}</li>
{% endfor %}
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
1 change: 1 addition & 0 deletions docs/img/waypoints.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions src/DataCollector/EncoreCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace HeimrichHannot\EncoreBundle\DataCollector;

use Composer\InstalledVersions;
use HeimrichHannot\EncoreBundle\Collection\ExtensionCollection;
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPoints;
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class EncoreCollector extends AbstractDataCollector
{
public function __construct(
private readonly ExtensionCollection $extensionCollection,
) {
Comment thread
koertho marked this conversation as resolved.
}

public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
if ($request->attributes->has('encore_entries')) {
$entryPoints = $request->attributes->get('encore_entries');
if (!($entryPoints instanceof EntryPoints)) {
$this->data['enabled'] = false;
} else {
$this->data['entries'] = $entryPoints->all();
$this->data['enabled'] = true;
}
} else {
$this->data['enabled'] = false;
}

$extensions = $this->extensionCollection->getExtensions();
$extensionEntries = [];
foreach ($extensions as $extension) {
$reflection = new \ReflectionClass($extension->getBundle());
$extensionEntries[] = [
'name' => $reflection->getShortName(),
'entries' => $extension->getEntries(),
];
}

$this->data['extensions'] = $extensionEntries;
}

public static function getTemplate(): ?string
{
return '@Contao/data_collector/huh_encore.html.twig';
}

public function isEnabled(): bool
{
return $this->data['enabled'] ?? false;
}

public function getEntries(): array
{
return $this->data['entries'] ?? [];
}

public function getVersion(): string
{
return InstalledVersions::getPrettyVersion('heimrichhannot/contao-encore-bundle') ?? 'unknown';
}

public function getExtensions(): array
{
return $this->data['extensions'] ?? [];
}
}
8 changes: 8 additions & 0 deletions src/EventListener/Contao/ReplaceDynamicScriptTagsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
use HeimrichHannot\EncoreBundle\Helper\ConfigurationHelper;
use HeimrichHannot\UtilsBundle\Util\Utils;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;

#[AsHook('replaceDynamicScriptTags')]
Expand All @@ -27,6 +28,7 @@ public function __construct(
private readonly EntryPointBuilderFactory $entryPointBuilderFactory,
private readonly FrontendAsset $frontendAsset,
private readonly TagRenderer $tagRenderer,
private readonly RequestStack $requestStack,
) {
}

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

if ($request = $this->requestStack->getCurrentRequest()) {
$request->attributes->add([
'encore_entries' => $entryPoints,
]);
}

$css = '';
$headJs = '';
$bodyJs = '';
Expand Down
8 changes: 8 additions & 0 deletions src/EventListener/InjectPageEntriesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use HeimrichHannot\EncoreBundle\EntryPoint\EntryPointBuilderFactory;
use HeimrichHannot\EncoreBundle\Helper\ConfigurationHelper;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;

class InjectPageEntriesListener
Expand All @@ -18,6 +19,7 @@ public function __construct(
private readonly FrontendAsset $frontendAsset,
private readonly GlobalContaoAsset $globalContaoAsset,
private readonly ConfigurationHelper $configurationHelper,
private readonly RequestStack $requestStack,
) {
}

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

if ($request = $this->requestStack->getCurrentRequest()) {
$request->attributes->add([
'encore_entries' => $entryPoints,
]);
}

$this->tagRenderer->reset();

foreach ($entryPoints->allActive() as $entrypoint) {
Expand Down
Loading
Loading