Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Pass view configuration to TemplatePaths #340

Merged
merged 1 commit into from
Sep 18, 2024
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
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
Loading