Skip to content

Commit

Permalink
Extract generating hook name outside the HooksRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Feb 6, 2024
1 parent a154813 commit 623bcc6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 26 deletions.
10 changes: 10 additions & 0 deletions src/TwigHooks/src/Hook/NameGenerator/NameGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Hook\NameGenerator;

interface NameGeneratorInterface
{
public function generate(string $input, string ...$parts): string;
}
39 changes: 39 additions & 0 deletions src/TwigHooks/src/Hook/NameGenerator/TemplateNameGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Sylius\TwigHooks\Hook\NameGenerator;

final class TemplateNameGenerator implements NameGeneratorInterface
{
public function generate(string $input, string ...$parts): string
{
$templatePath = $this->normalizeTemplatePath($input);
$normalizedParts = array_map($this->normalizeString(...), $parts);

return implode('.', array_merge([$templatePath], $normalizedParts));
}

private function normalizeTemplatePath(string $templatePath): string
{
$parts = explode('/', $templatePath);
$resultParts = [];

foreach ($parts as $part) {
$resultPart = str_replace(['@', '.html.twig'], '', $part);
$resultPart = $this->normalizeString($resultPart);
$resultParts[] = $resultPart;
}

return implode('.', $resultParts);
}

private function normalizeString(string $string): string
{
$result = trim($string, '_');
/** @var string $result */
$result = preg_replace('/(?<!^)[A-Z]/', '_$0', $result);

return strtolower($result);
}
}
29 changes: 3 additions & 26 deletions src/TwigHooks/src/Twig/Runtime/HooksRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sylius\TwigHooks\Twig\Runtime;

use Sylius\TwigHooks\Hook\NameGenerator\NameGeneratorInterface;
use Sylius\TwigHooks\Hook\Renderer\HookRendererInterface;
use Twig\Extension\RuntimeExtensionInterface;

Expand All @@ -15,37 +16,13 @@ final class HooksRuntime implements RuntimeExtensionInterface

public function __construct (
private readonly HookRendererInterface $hookRenderer,
private readonly NameGeneratorInterface $nameGenerator,
) {
}

public function createHookName(string $base, string ...$parts): string
{
if ([] === $parts) {
return $this->formatBaseString($base);
}

return sprintf(
'%s.%s',
$this->formatBaseString($base),
implode('.', $parts),
);
}

private function formatBaseString(string $base): string
{
$parts = explode('/', $base);
$resultParts = [];

foreach ($parts as $part) {
$resultPart = trim($part, '_');
$resultPart = str_replace(['@', '.html.twig'], '', $resultPart);
/** @var string $resultPart */
$resultPart = preg_replace('/(?<!^)[A-Z]/', '_$0', $resultPart);
$resultPart = strtolower($resultPart);
$resultParts[] = $resultPart;
}

return implode('.', $resultParts);
return $this->nameGenerator->generate($base, ...$parts);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\TwigHooks\Unit\Hook\NameGenerator;

use PHPUnit\Framework\TestCase;
use Sylius\TwigHooks\Hook\NameGenerator\TemplateNameGenerator;

final class TemplateNameGeneratorTest extends TestCase
{
/**
* @dataProvider itGeneratesNameDataProvider
*/
public function testItGeneratesName(string $input, array $extraParts, string $expectedResult): void
{
$subject = $this->createTestSubject();

self::assertSame($expectedResult, $subject->generate($input, ...$extraParts));
}

public function itGeneratesNameDataProvider(): array
{
return [
['@SyliusShop/layout.html.twig', ['header'], 'sylius_shop.layout.header'],
['@SyliusShop/layout.html.twig', ['header', 'menu'], 'sylius_shop.layout.header.menu'],
['@SyliusShop/layout.html.twig', ['header', 'menu', 'footer'], 'sylius_shop.layout.header.menu.footer'],
['@SyliusShop/_template.html.twig', [], 'sylius_shop.template'],
['@SyliusShop/_template.html.twig', ['header'], 'sylius_shop.template.header'],
['@SyliusShop/_template.html.twig', ['header', 'hamburger_menu'], 'sylius_shop.template.header.hamburger_menu'],
['@SyliusShop/_template.html.twig', ['header', 'hamburgerMenu'], 'sylius_shop.template.header.hamburger_menu'],
];
}

private function createTestSubject(): TemplateNameGenerator
{
return new TemplateNameGenerator();
}
}

0 comments on commit 623bcc6

Please sign in to comment.