Skip to content

Commit

Permalink
[Twig Hooks] Fix handling hook names with a section
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Apr 29, 2024
1 parent 5094c0e commit b3143e0
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 25 deletions.
41 changes: 31 additions & 10 deletions src/TwigHooks/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Sylius\TwigHooks\Hook\Normalizer\CompositeNameNormalizer;
use Sylius\TwigHooks\Hook\Normalizer\NameNormalizerInterface;
use Sylius\TwigHooks\Hook\Normalizer\RemoveSectionPartNormalizer;
use Sylius\TwigHooks\Hook\Normalizer\Name\CompositeNameNormalizer;
use Sylius\TwigHooks\Hook\Normalizer\Name\NameNormalizerInterface;
use Sylius\TwigHooks\Hook\Normalizer\Prefix\CompositePrefixNormalizer;
use Sylius\TwigHooks\Hook\Normalizer\Prefix\PrefixNormalizerInterface;
use Sylius\TwigHooks\Hook\Normalizer\Prefix\RemoveSectionPartNormalizer;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactory;
use Sylius\TwigHooks\Provider\ComponentPropsProvider;
use Sylius\TwigHooks\Provider\DefaultConfigurationProvider;
use Sylius\TwigHooks\Provider\DefaultContextProvider;
Expand Down Expand Up @@ -36,20 +39,37 @@
;

$services
->set('twig_hooks.hook.normalizer.composite', CompositeNameNormalizer::class)
->set('twig_hooks.hook.normalizer.name.composite', CompositeNameNormalizer::class)
->args([
tagged_iterator('twig_hooks.hook_normalizer'),
tagged_iterator('twig_hooks.hook.name_normalizer'),
])
->alias(NameNormalizerInterface::class, 'twig_hooks.hook.normalizer.composite')
->alias(NameNormalizerInterface::class, 'twig_hooks.hook.normalizer.name.composite')
;

$services
->set('twig_hooks.hook.normalizer.remove_section_part', RemoveSectionPartNormalizer::class)
->set('twig_hooks.hook.normalizer.prefix.composite', CompositePrefixNormalizer::class)
->args([
tagged_iterator('twig_hooks.hook.prefix_normalizer'),
])
->alias(PrefixNormalizerInterface::class, 'twig_hooks.hook.normalizer.prefix.composite')
;

$services
->set('twig_hooks.hook.normalizer.prefix.remove_section_part', RemoveSectionPartNormalizer::class)
->args([
param('twig_hooks.hook_name_section_separator'),
])
->tag('twig_hooks.hook_normalizer')
->alias(sprintf('%s $removeSectionPartNormalizer', NameNormalizerInterface::class), 'twig_hooks.hook.normalizer.remove_section_part')
->tag('twig_hooks.hook.prefix_normalizer')
->alias(
sprintf('%s $removeSectionPartNormalizer', PrefixNormalizerInterface::class),
'twig_hooks.hook.normalizer.prefix.remove_section_part',
)
;

$services->set('twig_hooks.factory.hookable_metadata', HookableMetadataFactory::class)
->args([
service('twig_hooks.hook.normalizer.prefix.composite'),
])
;

$services->set(HooksExtension::class)
Expand All @@ -66,7 +86,8 @@
$services->set(HooksRuntime::class)
->args([
service('twig_hooks.renderer.hook'),
service('twig_hooks.hook.normalizer.composite'),
service('twig_hooks.hook.normalizer.name.composite'),
service('twig_hooks.hook.normalizer.prefix.composite'),
param('twig_hooks.enable_autoprefixing'),
])
->tag('twig.runtime')
Expand Down
1 change: 1 addition & 0 deletions src/TwigHooks/config/services/hook_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
service('twig_hooks.renderer.hookable'),
service('twig_hooks.provider.default_context'),
service('twig_hooks.provider.default_configuration'),
service('twig_hooks.factory.hookable_metadata'),
])
->alias(HookRendererInterface::class, 'twig_hooks.renderer.hook')
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Sylius\TwigHooks\Hook\Normalizer;
namespace Sylius\TwigHooks\Hook\Normalizer\Name;

final class CompositeNameNormalizer implements NameNormalizerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Sylius\TwigHooks\Hook\Normalizer;
namespace Sylius\TwigHooks\Hook\Normalizer\Name;

interface NameNormalizerInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Hook\Normalizer\Prefix;

use Sylius\TwigHooks\Hook\Normalizer\Name\NameNormalizerInterface;

final class CompositePrefixNormalizer implements PrefixNormalizerInterface
{
/** @var array<PrefixNormalizerInterface> */
private readonly array $prefixNormalizers;

/**
* @param iterable<PrefixNormalizerInterface> $prefixNormalizers
*/
public function __construct(iterable $prefixNormalizers)
{
$this->prefixNormalizers = $prefixNormalizers instanceof \Traversable ? iterator_to_array($prefixNormalizers) : $prefixNormalizers;
}

public function normalize(string $prefix): string
{
$normalizedPrefix = $prefix;

foreach ($this->prefixNormalizers as $prefixNormalizer) {
$normalizedPrefix = $prefixNormalizer->normalize($normalizedPrefix);
}

return $normalizedPrefix;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Hook\Normalizer\Prefix;

interface PrefixNormalizerInterface
{
public function normalize(string $prefix): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Sylius\TwigHooks\Hook\Normalizer;
namespace Sylius\TwigHooks\Hook\Normalizer\Prefix;

final class RemoveSectionPartNormalizer implements NameNormalizerInterface
final class RemoveSectionPartNormalizer implements PrefixNormalizerInterface
{
/**
* @param non-empty-string|false $separator
Expand All @@ -13,13 +13,13 @@ public function __construct(private readonly string|false $separator)
{
}

public function normalize(string $name): string
public function normalize(string $prefix): string
{
if (false === $this->separator) {
return $name;
return $prefix;
}

$parts = explode('.', $name);
$parts = explode('.', $prefix);
$result = [];

foreach ($parts as $part) {
Expand Down
10 changes: 8 additions & 2 deletions src/TwigHooks/src/Hook/Renderer/HookRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Sylius\TwigHooks\Bag\DataBag;
use Sylius\TwigHooks\Bag\ScalarDataBag;
use Sylius\TwigHooks\Hook\Metadata\HookMetadata;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactoryInterface;
use Sylius\TwigHooks\Hookable\Renderer\HookableRendererInterface;
use Sylius\TwigHooks\Provider\ConfigurationProviderInterface;
use Sylius\TwigHooks\Provider\ContextProviderInterface;
Expand All @@ -20,6 +20,7 @@ public function __construct(
private readonly HookableRendererInterface $compositeHookableRenderer,
private readonly ContextProviderInterface $contextProvider,
private readonly ConfigurationProviderInterface $configurationProvider,
private readonly HookableMetadataFactoryInterface $hookableMetadataFactory,
) {
}

Expand All @@ -38,7 +39,12 @@ public function render(array $hookNames, array $hookContext = []): string
$context = $this->contextProvider->provide($hookable, $hookContext);
$configuration = $this->configurationProvider->provide($hookable);

$hookableMetadata = new HookableMetadata($hookMetadata, new DataBag($context), new ScalarDataBag($configuration), $hookNames);
$hookableMetadata = $this->hookableMetadataFactory->create(
$hookMetadata,
new DataBag($context),
new ScalarDataBag($configuration),
$hookNames,
);

$renderedHookables[] = $this->compositeHookableRenderer->render($hookable, $hookableMetadata);
}
Expand Down
29 changes: 29 additions & 0 deletions src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Hookable\Metadata;

use Sylius\TwigHooks\Bag\DataBagInterface;
use Sylius\TwigHooks\Bag\ScalarDataBagInterface;
use Sylius\TwigHooks\Hook\Metadata\HookMetadata;
use Sylius\TwigHooks\Hook\Normalizer\Prefix\PrefixNormalizerInterface;

final class HookableMetadataFactory implements HookableMetadataFactoryInterface
{
public function __construct(
private readonly PrefixNormalizerInterface $prefixNormalizer
) {
}

public function create(

Check failure on line 19 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactory.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^5.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactory::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 19 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactory.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^6.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactory::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 19 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactory.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^5.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactory::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 19 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactory.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^6.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactory::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 19 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactory.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^5.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactory::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 19 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactory.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^6.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactory::create() has parameter $prefixes with no value type specified in iterable type array.
HookMetadata $hookMetadata,
DataBagInterface $context,
ScalarDataBagInterface $configuration,
array $prefixes = [],
): HookableMetadata {
$prefixes = array_map([$this->prefixNormalizer, 'normalize'], $prefixes);

return new HookableMetadata($hookMetadata, $context, $configuration, $prefixes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Hookable\Metadata;

use Sylius\TwigHooks\Bag\DataBagInterface;
use Sylius\TwigHooks\Bag\ScalarDataBagInterface;
use Sylius\TwigHooks\Hook\Metadata\HookMetadata;

interface HookableMetadataFactoryInterface
{
public function create(HookMetadata $hookMetadata, DataBagInterface $context, ScalarDataBagInterface $configuration, array $prefixes = [],): HookableMetadata;

Check failure on line 13 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactoryInterface.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^5.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactoryInterface::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 13 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactoryInterface.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^6.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactoryInterface::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 13 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactoryInterface.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^5.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactoryInterface::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 13 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactoryInterface.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^6.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactoryInterface::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 13 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactoryInterface.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^5.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactoryInterface::create() has parameter $prefixes with no value type specified in iterable type array.

Check failure on line 13 in src/TwigHooks/src/Hookable/Metadata/HookableMetadataFactoryInterface.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^6.4)

Method Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactoryInterface::create() has parameter $prefixes with no value type specified in iterable type array.
}
9 changes: 6 additions & 3 deletions src/TwigHooks/src/Twig/Runtime/HooksRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Sylius\TwigHooks\Twig\Runtime;

use Sylius\TwigHooks\Bag\DataBagInterface;
use Sylius\TwigHooks\Hook\Normalizer\NameNormalizerInterface;
use Sylius\TwigHooks\Hook\Normalizer\Name\NameNormalizerInterface;
use Sylius\TwigHooks\Hook\Normalizer\Prefix\PrefixNormalizerInterface;
use Sylius\TwigHooks\Hook\Renderer\HookRendererInterface;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Twig\Error\RuntimeError;
Expand All @@ -18,6 +19,7 @@ final class HooksRuntime implements RuntimeExtensionInterface
public function __construct (
private readonly HookRendererInterface $hookRenderer,
private readonly NameNormalizerInterface $nameNormalizer,
private readonly PrefixNormalizerInterface $prefixNormalizer,
private readonly bool $enableAutoprefixing,
) {
}
Expand Down Expand Up @@ -80,8 +82,7 @@ public function renderHook(

foreach ($hookNames as $hookName) {
foreach ($prefixes as $prefix) {
$normalizedPrefix = $this->nameNormalizer->normalize($prefix);
$prefixedHookNames[] = implode('.', [$normalizedPrefix, $hookName]);
$prefixedHookNames[] = sprintf('%s.%s', $prefix, $hookName);
}
}

Expand All @@ -105,6 +106,8 @@ private function getPrefixes(array $hookContext, ?HookableMetadata $hookableMeta
$prefixes = $hookContext['_prefixes'];
}

$prefixes = array_map([$this->prefixNormalizer, 'normalize'], $prefixes);

return $prefixes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Tests\Sylius\TwigHooks\Unit\Hook\Normalizer;

use PHPUnit\Framework\TestCase;
use Sylius\TwigHooks\Hook\Normalizer\CompositeNameNormalizer;
use Sylius\TwigHooks\Hook\Normalizer\NameNormalizerInterface;
use Sylius\TwigHooks\Hook\Normalizer\Name\CompositeNameNormalizer;
use Sylius\TwigHooks\Hook\Normalizer\Name\NameNormalizerInterface;

final class CompositeNameNormalizerTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tests\Sylius\TwigHooks\Unit\Hook\Normalizer;

use PHPUnit\Framework\TestCase;
use Sylius\TwigHooks\Hook\Normalizer\RemoveSectionPartNormalizer;
use Sylius\TwigHooks\Hook\Normalizer\Prefix\RemoveSectionPartNormalizer;

final class RemoveSectionPartNormalizerTest extends TestCase
{
Expand Down
6 changes: 6 additions & 0 deletions src/TwigHooks/tests/Unit/Hook/Renderer/HookRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use Sylius\TwigHooks\Hook\Renderer\HookRenderer;
use Sylius\TwigHooks\Hookable\AbstractHookable;
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadataFactoryInterface;
use Sylius\TwigHooks\Hookable\Renderer\HookableRendererInterface;
use Sylius\TwigHooks\Provider\ConfigurationProviderInterface;
use Sylius\TwigHooks\Provider\ContextProviderInterface;
Expand All @@ -28,12 +29,16 @@ final class HookRendererTest extends TestCase
/** @var ConfigurationProviderInterface&MockObject */
private ConfigurationProviderInterface $configurationProvider;

/** @var HookableMetadataFactoryInterface&MockObject */
private HookableMetadataFactoryInterface $hookableMetadataFactory;

protected function setUp(): void
{
$this->hookablesRegistry = $this->createMock(HookablesRegistry::class);
$this->hookableRenderer = $this->createMock(HookableRendererInterface::class);
$this->contextProvider = $this->createMock(ContextProviderInterface::class);
$this->configurationProvider = $this->createMock(ConfigurationProviderInterface::class);
$this->hookableMetadataFactory = $this->createMock(HookableMetadataFactoryInterface::class);
}

public function testItReturnsRenderedHookables(): void
Expand Down Expand Up @@ -79,6 +84,7 @@ private function getTestSubject(): HookRenderer
$this->hookableRenderer,
$this->contextProvider,
$this->configurationProvider,
$this->hookableMetadataFactory,
);
}
}

0 comments on commit b3143e0

Please sign in to comment.