Skip to content

Commit

Permalink
Merge pull request #340 from CPS-IT/0.8/template-paths
Browse files Browse the repository at this point in the history
[BUGFIX] Pass view configuration to TemplatePaths
  • Loading branch information
eliashaeussler authored Sep 18, 2024
2 parents 66aea9a + 4de0f0d commit 25a62b8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
24 changes: 10 additions & 14 deletions Classes/Renderer/Template/TemplatePaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace Fr\Typo3Handlebars\Renderer\Template;

use Fr\Typo3Handlebars\Configuration\Extension;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;

Expand All @@ -44,10 +43,15 @@ class TemplatePaths
*/
protected ?array $templatePaths = null;

/**
* @param array{
* partial_root_paths?: array<int, string>,
* template_root_paths?: array<int, string>,
* } $viewConfiguration
*/
public function __construct(
protected readonly ConfigurationManagerInterface $configurationManager,
// @todo check if this works as expected
protected readonly ParameterBagInterface $parameterBag,
protected readonly array $viewConfiguration = [],
protected readonly string $type = self::TEMPLATES,
) {}

Expand All @@ -69,25 +73,17 @@ public function get(): array
protected function resolveTemplatePaths(): void
{
$this->templatePaths = $this->mergeTemplatePaths(
$this->getTemplatePathsFromContainer($this->type),
$this->getTemplatePathsFromViewConfiguration($this->type),
$this->getTemplatePathsFromTypoScriptConfiguration($this->type)
);
}

/**
* @return string[]
*/
protected function getTemplatePathsFromContainer(string $type): array
protected function getTemplatePathsFromViewConfiguration(string $type): array
{
$parameterName = 'handlebars.' . $type;

if (!$this->parameterBag->has($parameterName)) {
return [];
}

$templatePathsParameter = $this->parameterBag->get($parameterName);

return \is_array($templatePathsParameter) ? $templatePathsParameter : [$templatePathsParameter];
return $this->viewConfiguration[$type] ?? [];
}

/**
Expand Down
4 changes: 4 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ services:

handlebars.template_paths:
class: 'Fr\Typo3Handlebars\Renderer\Template\TemplatePaths'
arguments:
$viewConfiguration:
template_root_paths: '%handlebars.template_root_paths%'
partial_root_paths: '%handlebars.partial_root_paths%'
handlebars.template_paths.template_root_paths:
parent: 'handlebars.template_paths'
arguments:
Expand Down
15 changes: 8 additions & 7 deletions Tests/Unit/HandlebarsTemplateResolverTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
use Fr\Typo3Handlebars\Renderer\Template\TemplateResolverInterface;
use Fr\Typo3Handlebars\Tests\Unit\Fixtures\Classes\DummyConfigurationManager;
use Fr\Typo3Handlebars\Tests\Unit\Fixtures\Classes\Renderer\Template\DummyTemplatePaths;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* HandlebarsTemplateResolverTrait
Expand Down Expand Up @@ -77,16 +75,19 @@ protected function getPartialResolver(): TemplateResolverInterface

protected function getTemplatePaths(string $type = TemplatePaths::TEMPLATES): DummyTemplatePaths
{
return new DummyTemplatePaths(new DummyConfigurationManager(), $this->getParameterBag($type), $type);
return new DummyTemplatePaths(new DummyConfigurationManager(), $this->getViewConfiguration($type), $type);
}

protected function getParameterBag(string $type = TemplatePaths::TEMPLATES): ParameterBagInterface
/**
* @return array<string, array<int, string>>
*/
protected function getViewConfiguration(string $type = TemplatePaths::TEMPLATES): array
{
$templateRootPath = $type === TemplatePaths::PARTIALS ? $this->getPartialRootPath() : $this->getTemplateRootPath();

return new ParameterBag([
'handlebars.' . $type => [10 => $templateRootPath],
]);
return [
$type => [10 => $templateRootPath],
];
}

public function getTemplateRootPath(): string
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/Renderer/Template/TemplatePathsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function setUp(): void
{
parent::setUp();
$this->configurationManager = new DummyConfigurationManager();
$this->subject = new TemplatePaths($this->configurationManager, $this->getParameterBag());
$this->subject = new TemplatePaths($this->configurationManager, $this->getViewConfiguration());
}

/**
Expand Down

0 comments on commit 25a62b8

Please sign in to comment.