Skip to content

Commit

Permalink
OXDEV-6895 Remove deprecated TemplateFileResolverInterface usages
Browse files Browse the repository at this point in the history
  • Loading branch information
liulka-oxid committed Jan 4, 2024
1 parent eb8f099 commit de6609e
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 94 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

### Removed
- `SmartyCycleExtension` was removed
- Short template names (without file extensions `".html.twig"`) are not longer supported.
1 change: 0 additions & 1 deletion services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions src/Resolver/TemplateChain/TemplateType/TemplateTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down
27 changes: 14 additions & 13 deletions src/TwigEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
) {
Expand All @@ -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
Expand All @@ -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)
Expand Down
43 changes: 0 additions & 43 deletions tests/Integration/Resolver/TemplateFileResolverTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ 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';
private const TEMPLATE_WITH_CONDITIONAL_EXTENDS = 'template-with-conditional-extends.html.twig';
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
{
Expand Down
8 changes: 0 additions & 8 deletions tests/Integration/TwigEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -103,7 +96,6 @@ private function getEngine(): TwigEngine

return new TwigEngine(
$engine,
$this->extension,
$this->templateChainResolver->reveal()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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);
}
}

0 comments on commit de6609e

Please sign in to comment.