Skip to content

Commit

Permalink
do not process templates already based on twig
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Oct 1, 2024
1 parent b55babd commit 82b83d0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 60 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.8.3] - 2024-10-01
- Fixed: do not process templates already based on twig

## [1.8.2] - 2024-10-01
- Fixed: issues with different bundle structures

Expand Down
64 changes: 21 additions & 43 deletions src/EventListener/RenderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,32 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Twig\Environment;

class RenderListener implements ServiceSubscriberInterface
{
public const TWIG_TEMPLATE = 'twig_template';
public const TWIG_CONTEXT = 'twig_context';

/** @var string[] */
protected $templates = [];
/**
* @var TwigTemplateLocator
*/
protected $templateLocator;
/**
* @var string
*/
protected $rootDir;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var KernelInterface
*/
protected $kernel;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var ScopeMatcher
*/
protected $scopeMatcher;
/**
* @var NormalizerHelper
*/
protected $normalizer;
/**
* @var array
*/
protected $bundleConfig;
/**
* @var bool
*/
protected $enableTemplateLoader = false;

protected array $templates = [];
protected TwigTemplateLocator $templateLocator;
protected string $rootDir;
protected EventDispatcherInterface $eventDispatcher;
protected KernelInterface $kernel;
protected RequestStack $requestStack;
protected ScopeMatcher $scopeMatcher;
protected NormalizerHelper $normalizer;
protected array $bundleConfig;
protected bool $enableTemplateLoader = false;
protected TwigTemplateRenderer $twigTemplateRenderer;
private ContainerInterface $container;
private Environment $twig;

/**
* RenderListener constructor.
*/
public function __construct(ContainerInterface $container, TwigTemplateLocator $templateLocator, EventDispatcherInterface $eventDispatcher, RequestStack $requestStack, ScopeMatcher $scopeMatcher, NormalizerHelper $normalizer, array $bundleConfig, TwigTemplateRenderer $twigTemplateRenderer)
public function __construct(ContainerInterface $container, TwigTemplateLocator $templateLocator, EventDispatcherInterface $eventDispatcher, RequestStack $requestStack, ScopeMatcher $scopeMatcher, NormalizerHelper $normalizer, array $bundleConfig, TwigTemplateRenderer $twigTemplateRenderer, Environment $twig)
{
$this->templateLocator = $templateLocator;
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -92,6 +66,7 @@ public function __construct(ContainerInterface $container, TwigTemplateLocator $
$this->enableTemplateLoader = true;
}
$this->container = $container;
$this->twig = $twig;
}

/**
Expand Down Expand Up @@ -256,15 +231,18 @@ public static function getSubscribedServices(): array
}

/**
* Check if the template is in the skipped template list.
* Check if the template should be skipped
*/
protected function isSkippedTemplate(string $template): bool
{
if (!isset($this->bundleConfig['skip_templates']) || empty($this->bundleConfig['skip_templates'])) {
return false;
// skip template loaded by contao twig engine
$templateCandidate = "@Contao/$template.html.twig";
if ($this->twig->getLoader()->exists($templateCandidate))
{
return true;
}

return \in_array($template, $this->bundleConfig['skip_templates']);
return \in_array($template, $this->bundleConfig['skip_templates'] ?? []);
}

private function getTemplates(): array
Expand Down
61 changes: 44 additions & 17 deletions tests/EventListener/RenderListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

class RenderListenerTest extends ContaoTestCase
{
Expand Down Expand Up @@ -71,6 +72,7 @@ public function createTestInstance(array $parameters = [], ?MockBuilder $mockBui
}

$templateRenderer = $parameters['templateRenderer'] ?? $this->createMock(TwigTemplateRenderer::class);
$parameters['twig'] = $parameters['twig'] ?? $this->createMock(Environment::class);

if ($mockBuilder) {
$instance = $mockBuilder->setConstructorArgs([
Expand All @@ -82,6 +84,7 @@ public function createTestInstance(array $parameters = [], ?MockBuilder $mockBui
$parameters['normalizer'],
$parameters['bundleConfig'],
$templateRenderer,
$parameters['twig'],
])->getMock();
} else {
$instance = new RenderListener(
Expand All @@ -92,7 +95,8 @@ public function createTestInstance(array $parameters = [], ?MockBuilder $mockBui
$parameters['scopeMatcher'],
$parameters['normalizer'],
$parameters['bundleConfig'],
$templateRenderer
$templateRenderer,
$parameters['twig']
);
}

Expand Down Expand Up @@ -132,35 +136,58 @@ public function testRender()
$instance->render($contaoTemplate);
}

public function testOnParseTemplateSkipTemplates()
public function testOnParseTemplate()
{
$mockBuilder = $this->getMockBuilder(RenderListener::class)
->setMethods(['prepareContaoTemplate']);
$templateLocator = $this->createMock(TwigTemplateLocator::class);
$templateLocator->method('getTemplates')->willReturn(['test' => 'test']);

// Template loader disabled
$instance = $this->createTestInstance([
'templateLocator' => $templateLocator,
]);
$template = $this->mockTemplateObject(FrontendTemplate::class, 'test');
$instance->onParseTemplate($template);
$this->assertSame('test', $template->getName());

// Template loader enabled, template is skipped by config
$instance = $this->createTestInstance([
'bundleConfig' => [
'enable_template_loader' => true,
'skip_templates' => ['test'],
],
], $mockBuilder);
$instance->expects($this->once())->method('prepareContaoTemplate');
$template = $this->createMock(Template::class);
$template->method('getName')->willReturn('template');
'templateLocator' => $templateLocator,
]);
$template = $this->mockTemplateObject(FrontendTemplate::class, 'test');
$instance->onParseTemplate($template);
$this->assertSame('test', $template->getName());

$mockBuilder = $this->getMockBuilder(RenderListener::class)
->setMethods(['prepareContaoTemplate']);
// Template loader enabled, template is known by contao template engine
$loader = $this->createMock(FilesystemLoader::class);
$loader->method('exists')->willReturn(true);
$twig = $this->createMock(Environment::class);
$twig->method('getLoader')->willReturn($loader);
$instance = $this->createTestInstance([
'bundleConfig' => [
'enable_template_loader' => true,
'skip_templates' => ['image'],
],
], $mockBuilder);
$instance->expects($this->once())->method('prepareContaoTemplate');
$template = $this->createMock(Template::class);
$template->method('getName')->willReturn('template');
'twig' => $twig,
'templateLocator' => $templateLocator,
]);
$template = $this->mockTemplateObject(FrontendTemplate::class, 'test');
$instance->onParseTemplate($template);
$template = $this->createMock(Template::class);
$template->method('getName')->willReturn('image');
$this->assertSame('test', $template->getName());

// Template loader enabled
$instance = $this->createTestInstance([
'bundleConfig' => [
'enable_template_loader' => true,
],
'templateLocator' => $templateLocator,
]);

$template = $this->mockTemplateObject(FrontendTemplate::class, 'test');
$instance->onParseTemplate($template);
$this->assertSame('twig_template_proxy', $template->getName());
}

public function testOnParseWidgetSkipTemplates()
Expand Down

0 comments on commit 82b83d0

Please sign in to comment.