From de6609eb17205749d4ad537f936faa031f8d6042 Mon Sep 17 00:00:00 2001 From: Vasyl Liulka Date: Thu, 4 Jan 2024 14:03:45 +0100 Subject: [PATCH] OXDEV-6895 Remove deprecated TemplateFileResolverInterface usages --- CHANGELOG-3.x.md | 1 + services.yaml | 1 - .../TemplateType/TemplateTypeFactory.php | 12 ------ src/TwigEngine.php | 27 ++++++------ .../Resolver/TemplateFileResolverTest.php | 43 ------------------- .../ModulesTemplateChainTest.php | 4 +- tests/Integration/TwigEngineTest.php | 8 ---- .../TemplateType/TemplateTypeFactoryTest.php | 16 +------ 8 files changed, 18 insertions(+), 94 deletions(-) delete mode 100644 tests/Integration/Resolver/TemplateFileResolverTest.php diff --git a/CHANGELOG-3.x.md b/CHANGELOG-3.x.md index 2054148..eb6f0cb 100644 --- a/CHANGELOG-3.x.md +++ b/CHANGELOG-3.x.md @@ -4,3 +4,4 @@ ### Removed - `SmartyCycleExtension` was removed +- Short template names (without file extensions `".html.twig"`) are not longer supported. diff --git a/services.yaml b/services.yaml index 90fdeb9..ef904e9 100644 --- a/services.yaml +++ b/services.yaml @@ -256,7 +256,6 @@ services: class: OxidEsales\Twig\TwigEngine arguments: $engine: '@twig' - $fileExtension: '%oxid_esales.templating.engine_template_extension%' $twigExtensions: !tagged twig.extension $twigEscaper: !tagged twig.escaper public: false diff --git a/src/Resolver/TemplateChain/TemplateType/TemplateTypeFactory.php b/src/Resolver/TemplateChain/TemplateType/TemplateTypeFactory.php index 777ccb3..010fbd9 100644 --- a/src/Resolver/TemplateChain/TemplateType/TemplateTypeFactory.php +++ b/src/Resolver/TemplateChain/TemplateType/TemplateTypeFactory.php @@ -9,7 +9,6 @@ namespace OxidEsales\Twig\Resolver\TemplateChain\TemplateType; -use OxidEsales\EshopCommunity\Internal\Framework\Templating\Resolver\TemplateFileResolverInterface; use OxidEsales\Twig\Resolver\TemplateChain\TemplateType\DataObject\ModuleExtensionTemplateType; use OxidEsales\Twig\Resolver\TemplateChain\TemplateType\DataObject\ModuleTemplateType; use OxidEsales\Twig\Resolver\TemplateChain\TemplateType\DataObject\ShopExtensionTemplateType; @@ -27,14 +26,8 @@ class TemplateTypeFactory implements TemplateTypeFactoryInterface private const SHOP_EXTENSION_TEMPLATE_TYPE_PATTERN = '%^@([^\s/]+)/extensions/themes/([^\s/]+)/(.+)$%i'; private const BASE_TEMPLATE_TYPE_PATTERN = '%^(?:@([^\s/]+)/)?(.*)$%'; - public function __construct( - private TemplateFileResolverInterface $templateFileResolver, - ) { - } - public function createFromTemplateName(string $templateName): TemplateTypeInterface { - $templateName = $this->getFullNameWithFileExtension($templateName); $this->validateTemplateFilename($templateName); if ($this->isModuleExtensionFullyQualifiedName($templateName)) { @@ -94,11 +87,6 @@ private function isShopNamespace(string $namespace): bool return !$namespace || $namespace === FilesystemLoader::MAIN_NAMESPACE; } - private function getFullNameWithFileExtension(string $templateName): string - { - return $this->templateFileResolver->getFilename($templateName); - } - private function validateTemplateFilename(string $templateName): void { if (!str_ends_with($templateName, self::TWIG_FILE_EXTENSION)) { diff --git a/src/TwigEngine.php b/src/TwigEngine.php index ace453e..95e5a2f 100644 --- a/src/TwigEngine.php +++ b/src/TwigEngine.php @@ -19,9 +19,8 @@ class TwigEngine implements TemplateEngineInterface { public function __construct( - private Environment $engine, - private string $fileExtension, - private TemplateChainResolverInterface $templateChainResolver, + private readonly Environment $engine, + private readonly TemplateChainResolverInterface $templateChainResolver, iterable $twigExtensions = [], iterable $twigEscaper = [] ) { @@ -40,18 +39,18 @@ public function __construct( public function render(string $name, array $context = []): string { - return $this->engine->render( - $this->templateChainResolver->getLastChild($name), - $context - ); + return $this->engine + ->render( + $this->templateChainResolver->getLastChild($name), + $context + ); } public function exists(string $name): bool { - if (!str_ends_with($name, $this->fileExtension)) { - $name .= ".$this->fileExtension"; - } - return $this->engine->getLoader()->exists($name); + return $this->engine + ->getLoader() + ->exists($name); } public function renderFragment(string $fragment, string $fragmentId, array $context = []): string @@ -63,12 +62,14 @@ public function renderFragment(string $fragment, string $fragmentId, array $cont public function addGlobal(string $name, $value) { - $this->engine->addGlobal($name, $value); + $this->engine + ->addGlobal($name, $value); } public function getGlobals(): array { - return $this->engine->getGlobals(); + return $this->engine + ->getGlobals(); } public function addEscaper(EscaperInterface $escaper) diff --git a/tests/Integration/Resolver/TemplateFileResolverTest.php b/tests/Integration/Resolver/TemplateFileResolverTest.php deleted file mode 100644 index c7436a5..0000000 --- a/tests/Integration/Resolver/TemplateFileResolverTest.php +++ /dev/null @@ -1,43 +0,0 @@ -get(TemplateFileResolverInterface::class)->getFilename($templateName); - - $this->assertEquals($expectedFilename, $filename); - } - - public static function templateNameFileDataProvider(): array - { - return [ - [ - 'template', - 'template.html.twig' - ], - [ - 'some/path/template_name.html.twig', - 'some/path/template_name.html.twig' - ], - ]; - } -} diff --git a/tests/Integration/TwigEngine/TemplateChain/ModulesTemplateChainTest.php b/tests/Integration/TwigEngine/TemplateChain/ModulesTemplateChainTest.php index b06eaff..2d60f78 100644 --- a/tests/Integration/TwigEngine/TemplateChain/ModulesTemplateChainTest.php +++ b/tests/Integration/TwigEngine/TemplateChain/ModulesTemplateChainTest.php @@ -30,7 +30,7 @@ final class ModulesTemplateChainTest extends TestCase ]; private const THEME = 'testTheme'; private const THEME_2 = 'testTheme2'; - private const TEMPLATE_NO_EXTENDS = 'template-no-extends'; + private const TEMPLATE_NO_EXTENDS = 'template-no-extends.html.twig'; private const TEMPLATE_WITH_EXTENDS = 'template-with-extends.html.twig'; private const TEMPLATE_WITH_NON_HTML_FILE = 'template-non-html-with-extends.xml.twig'; private const TEMPLATE_WITH_INVALID_EXTENDS = 'template-with-invalid-extends.html.twig'; @@ -38,7 +38,7 @@ final class ModulesTemplateChainTest extends TestCase private const TEMPLATE_WITH_ARRAY_EXTENDS = 'template-with-array-extends.html.twig'; private const TEMPLATE_WITH_INCLUDE = 'template-with-include.html.twig'; private const TEMPLATE_WITH_INCLUDE_DYNAMIC = 'template-with-include-dynamic.html.twig'; - private const TEMPLATE_INCLUDE_NON_TEMPLATE_FILE = 'template-include-non-template-file'; + private const TEMPLATE_INCLUDE_NON_TEMPLATE_FILE = 'template-include-non-template-file.html.twig'; protected function setUp(): void { diff --git a/tests/Integration/TwigEngineTest.php b/tests/Integration/TwigEngineTest.php index 647a6ea..78d3727 100644 --- a/tests/Integration/TwigEngineTest.php +++ b/tests/Integration/TwigEngineTest.php @@ -39,13 +39,6 @@ protected function setUp(): void $this->templateDirPath = vfsStream::url($this->getTemplateDir()); } - public function testExists(): void - { - $engine = $this->getEngine(); - $this->assertTrue($engine->exists($this->template)); - $this->assertFalse($engine->exists('foo')); - } - public function testExistsWithFileExtension(): void { $this->assertTrue( @@ -103,7 +96,6 @@ private function getEngine(): TwigEngine return new TwigEngine( $engine, - $this->extension, $this->templateChainResolver->reveal() ); } diff --git a/tests/Unit/Resolver/TemplateChain/TemplateType/TemplateTypeFactoryTest.php b/tests/Unit/Resolver/TemplateChain/TemplateType/TemplateTypeFactoryTest.php index cdbd82c..f6020c0 100644 --- a/tests/Unit/Resolver/TemplateChain/TemplateType/TemplateTypeFactoryTest.php +++ b/tests/Unit/Resolver/TemplateChain/TemplateType/TemplateTypeFactoryTest.php @@ -9,7 +9,6 @@ namespace OxidEsales\Twig\Tests\Unit\Resolver\TemplateChain\TemplateType; -use OxidEsales\EshopCommunity\Internal\Framework\Templating\Resolver\TemplateFileResolver; use OxidEsales\Twig\Resolver\TemplateChain\TemplateType\DataObject\ModuleExtensionTemplateType; use OxidEsales\Twig\Resolver\TemplateChain\TemplateType\DataObject\ModuleTemplateType; use OxidEsales\Twig\Resolver\TemplateChain\TemplateType\DataObject\ShopExtensionTemplateType; @@ -22,15 +21,6 @@ final class TemplateTypeFactoryTest extends TestCase { private string $extension = 'html.twig'; - public function testCreateFromTemplateNameWithFileExtensionOmitted(): void - { - $templatePath = 'start/hello'; - - $template = $this->getTemplateType($templatePath); - - $this->assertInstanceOf(ShopTemplateType::class, $template); - } - public function testCreateFromTemplateNameWithShopTemplate(): void { $templatePath = "start/hello.$this->extension"; @@ -114,10 +104,6 @@ public function testCreateFromTemplateNameWithModuleExtension(): void private function getTemplateType(string $templatePath): TemplateTypeInterface { - $templateTypeFactory = new TemplateTypeFactory( - new TemplateFileResolver($this->extension) - ); - - return $templateTypeFactory->createFromTemplateName($templatePath); + return (new TemplateTypeFactory())->createFromTemplateName($templatePath); } }