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

[FEATURE] Introduce #[AsHelper] attribute for helper DI registration #374

Merged
merged 4 commits into from
Dec 19, 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
41 changes: 41 additions & 0 deletions Classes/Attribute/AsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS extension "handlebars".
*
* Copyright (C) 2024 Elias Häußler <e.haeussler@familie-redlich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Fr\Typo3Handlebars\Attribute;

/**
* AsHelper
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
final readonly class AsHelper
{
public const TAG_NAME = 'handlebars.helper';

public function __construct(
public string $identifier,
public ?string $method = null,
) {}
}
4 changes: 4 additions & 0 deletions Classes/Cache/HandlebarsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace Fr\Typo3Handlebars\Cache;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;

/**
Expand All @@ -31,9 +33,11 @@
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
#[AsAlias('handlebars.cache')]
class HandlebarsCache implements CacheInterface
{
public function __construct(
#[Autowire('@cache.handlebars')]
protected readonly FrontendInterface $cache,
) {}

Expand Down
2 changes: 2 additions & 0 deletions Classes/Compatibility/View/HandlebarsViewResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use Fr\Typo3Handlebars\DataProcessing\DataProcessorInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\View\GenericViewResolver;
use TYPO3Fluid\Fluid\View\ViewInterface;
Expand All @@ -35,6 +36,7 @@
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
#[Autoconfigure(public: true)]
class HandlebarsViewResolver extends GenericViewResolver
{
/**
Expand Down
2 changes: 2 additions & 0 deletions Classes/DataProcessing/SimpleProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Fr\Typo3Handlebars\Renderer\RendererInterface;
use Fr\Typo3Handlebars\Traits\ErrorHandlingTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
Expand All @@ -35,6 +36,7 @@
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
#[Autoconfigure(public: true)]
class SimpleProcessor implements DataProcessorInterface
{
use ErrorHandlingTrait;
Expand Down
9 changes: 9 additions & 0 deletions Classes/Renderer/HandlebarsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
use LightnCandy\Runtime;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

Expand All @@ -47,6 +50,8 @@
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
#[AsAlias('handlebars.renderer')]
#[Autoconfigure(tags: ['handlebars.renderer'])]
class HandlebarsRenderer implements RendererInterface, HelperAwareInterface
{
use HandlebarsHelperTrait;
Expand All @@ -57,11 +62,15 @@ class HandlebarsRenderer implements RendererInterface, HelperAwareInterface
* @param array<string|int, mixed> $defaultData
*/
public function __construct(
#[Autowire('@handlebars.cache')]
protected readonly CacheInterface $cache,
protected readonly EventDispatcherInterface $eventDispatcher,
protected readonly LoggerInterface $logger,
#[Autowire('@handlebars.template_resolver')]
protected readonly TemplateResolverInterface $templateResolver,
#[Autowire('@handlebars.partial_resolver')]
protected readonly ?TemplateResolverInterface $partialResolver = null,
#[Autowire('%handlebars.default_data%')]
protected array $defaultData = [],
) {
$this->debugMode = $this->isDebugModeEnabled();
Expand Down
4 changes: 3 additions & 1 deletion Classes/Renderer/Helper/BlockHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace Fr\Typo3Handlebars\Renderer\Helper;

use Fr\Typo3Handlebars\Attribute;
use Fr\Typo3Handlebars\Exception;
use Fr\Typo3Handlebars\Renderer;

Expand All @@ -33,12 +34,13 @@
* @license GPL-2.0-or-later
* @see https://github.com/shannonmoeller/handlebars-layouts#block-name
*/
class BlockHelper implements HelperInterface
final readonly class BlockHelper implements HelperInterface
{
/**
* @param array<string, mixed> $options
* @throws Exception\UnsupportedTypeException
*/
#[Attribute\AsHelper('block')]
public function evaluate(string $name, array $options): string
{
$data = $options['_this'];
Expand Down
6 changes: 4 additions & 2 deletions Classes/Renderer/Helper/ContentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace Fr\Typo3Handlebars\Renderer\Helper;

use Fr\Typo3Handlebars\Attribute;
use Fr\Typo3Handlebars\Renderer;
use Psr\Log;

Expand All @@ -33,16 +34,17 @@
* @license GPL-2.0-or-later
* @see https://github.com/shannonmoeller/handlebars-layouts#content-name-modeappendprependreplace
*/
class ContentHelper implements HelperInterface
final readonly class ContentHelper implements HelperInterface
{
public function __construct(
protected readonly Log\LoggerInterface $logger,
private Log\LoggerInterface $logger,
) {}

/**
* @param array<string, mixed> $options
* @return string|bool
*/
#[Attribute\AsHelper('content')]
public function evaluate(string $name, array $options)
{
$data = $options['_this'];
Expand Down
6 changes: 4 additions & 2 deletions Classes/Renderer/Helper/ExtendHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace Fr\Typo3Handlebars\Renderer\Helper;

use Fr\Typo3Handlebars\Attribute;
use Fr\Typo3Handlebars\Renderer;

/**
Expand All @@ -32,12 +33,13 @@
* @license GPL-2.0-or-later
* @see https://github.com/shannonmoeller/handlebars-layouts#extend-partial-context-keyvalue-
*/
class ExtendHelper implements HelperInterface
final readonly class ExtendHelper implements HelperInterface
{
public function __construct(
protected readonly Renderer\RendererInterface $renderer,
private Renderer\RendererInterface $renderer,
) {}

#[Attribute\AsHelper('extend')]
public function evaluate(string $name): string
{
// Get helper options
Expand Down
10 changes: 6 additions & 4 deletions Classes/Renderer/Helper/RenderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace Fr\Typo3Handlebars\Renderer\Helper;

use Fr\Typo3Handlebars\Attribute;
use Fr\Typo3Handlebars\DataProcessing;
use Fr\Typo3Handlebars\Exception;
use Fr\Typo3Handlebars\Renderer;
Expand All @@ -37,17 +38,18 @@
* @license GPL-2.0-or-later
* @see https://github.com/frctl/fractal/blob/main/packages/handlebars/src/helpers/render.js
*/
class RenderHelper implements HelperInterface
final readonly class RenderHelper implements HelperInterface
{
public function __construct(
protected readonly Renderer\RendererInterface $renderer,
protected readonly Core\TypoScript\TypoScriptService $typoScriptService,
protected readonly Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer,
private Renderer\RendererInterface $renderer,
private Core\TypoScript\TypoScriptService $typoScriptService,
private Frontend\ContentObject\ContentObjectRenderer $contentObjectRenderer,
) {}

/**
* @throws Exception\InvalidConfigurationException
*/
#[Attribute\AsHelper('render')]
public function evaluate(string $name): SafeString
{
// Get helper options
Expand Down
8 changes: 5 additions & 3 deletions Classes/Renderer/Helper/VarDumpHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@

namespace Fr\Typo3Handlebars\Renderer\Helper;

use TYPO3\CMS\Core\Utility\DebugUtility;
use Fr\Typo3Handlebars\Attribute;
use TYPO3\CMS\Core;

/**
* VarDumpHelper
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-2.0-or-later
*/
class VarDumpHelper implements HelperInterface
final readonly class VarDumpHelper implements HelperInterface
{
/**
* @param array<string|int, mixed> $context
*/
#[Attribute\AsHelper('varDump')]
public static function evaluate(array $context): string
{
\ob_start();

DebugUtility::debug($context['_this']);
Core\Utility\DebugUtility::debug($context['_this']);

return (string)\ob_get_clean();
}
Expand Down
5 changes: 5 additions & 0 deletions Classes/Renderer/Template/TemplatePaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace Fr\Typo3Handlebars\Renderer\Template;

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

Expand Down Expand Up @@ -51,6 +52,10 @@ class TemplatePaths
*/
public function __construct(
protected readonly ConfigurationManagerInterface $configurationManager,
#[Autowire([
self::TEMPLATES => '%handlebars.template_root_paths%',
self::PARTIALS => '%handlebars.partial_root_paths%',
])]
protected readonly array $viewConfiguration = [],
protected readonly string $type = self::TEMPLATES,
) {}
Expand Down
17 changes: 16 additions & 1 deletion Configuration/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,28 @@

namespace Fr\Typo3Handlebars\DependencyInjection;

use Fr\Typo3Handlebars\Attribute\AsHelper;
use Fr\Typo3Handlebars\DependencyInjection\Extension\HandlebarsExtension;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $container): void {
$container->registerExtension(new HandlebarsExtension());
$container->addCompilerPass(new DataProcessorPass('handlebars.processor', 'handlebars.compatibility_layer'));
$container->addCompilerPass(new HandlebarsHelperPass('handlebars.helper', 'handlebars.renderer'));
$container->addCompilerPass(new HandlebarsHelperPass(AsHelper::TAG_NAME, 'handlebars.renderer'));
$container->addCompilerPass(new FeatureRegistrationPass(), priority: 30);

$container->registerAttributeForAutoconfiguration(
AsHelper::class,
static function (ChildDefinition $definition, AsHelper $attribute, \Reflector $reflector): void {
$definition->addTag(
AsHelper::TAG_NAME,
[
'identifier' => $attribute->identifier,
'method' => $attribute->method ?? ($reflector instanceof \ReflectionMethod ? $reflector->getName() : '__invoke'),
],
);
},
);
};
44 changes: 4 additions & 40 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ services:
exclude:
- '../Classes/DependencyInjection/*'

# Renderer
handlebars.renderer:
class: 'Fr\Typo3Handlebars\Renderer\HandlebarsRenderer'
arguments:
$templateResolver: '@handlebars.template_resolver'
$partialResolver: '@handlebars.partial_resolver'
$cache: '@handlebars.cache'
$defaultData: '%handlebars.default_data%'
tags: ['handlebars.renderer']

Fr\Typo3Handlebars\Cache\CacheInterface:
alias: 'handlebars.cache'
Fr\Typo3Handlebars\Renderer\RendererInterface:
alias: 'handlebars.renderer'
Fr\Typo3Handlebars\Renderer\Template\TemplateResolverInterface:
alias: 'handlebars.template_resolver'

# Template
handlebars.template_resolver:
Expand All @@ -33,15 +27,8 @@ services:
arguments:
$templateRootPaths: '@handlebars.template_paths.partial_root_paths'

Fr\Typo3Handlebars\Renderer\Template\TemplateResolverInterface:
alias: 'handlebars.template_resolver'

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 All @@ -51,35 +38,12 @@ services:
arguments:
$type: 'partial_root_paths'

# Data processor
Fr\Typo3Handlebars\DataProcessing\SimpleProcessor:
public: true

# Handlebars Helper
Fr\Typo3Handlebars\Renderer\Helper\VarDumpHelper:
tags:
- name: handlebars.helper
identifier: 'varDump'
method: 'evaluate'

# Cache
handlebars.cache:
class: 'Fr\Typo3Handlebars\Cache\HandlebarsCache'
arguments:
$cache: '@cache.handlebars'

Fr\Typo3Handlebars\Cache\CacheInterface:
alias: 'handlebars.cache'

cache.handlebars:
class: TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
factory: ['@TYPO3\CMS\Core\Cache\CacheManager', 'getCache']
arguments: ['handlebars']

# Compatibility
Fr\Typo3Handlebars\Compatibility\View\HandlebarsViewResolver:
public: true

handlebars:
default_data: []
template:
Expand Down
Loading
Loading