From 623bcc64eb3bfff0538dadd341f69847a343d4b2 Mon Sep 17 00:00:00 2001 From: Jacob Tobiasz Date: Tue, 6 Feb 2024 08:33:15 +0100 Subject: [PATCH] Extract generating hook name outside the HooksRuntime --- .../NameGenerator/NameGeneratorInterface.php | 10 +++++ .../NameGenerator/TemplateNameGenerator.php | 39 +++++++++++++++++++ .../src/Twig/Runtime/HooksRuntime.php | 29 ++------------ .../TemplateNameGeneratorTest.php | 39 +++++++++++++++++++ 4 files changed, 91 insertions(+), 26 deletions(-) create mode 100644 src/TwigHooks/src/Hook/NameGenerator/NameGeneratorInterface.php create mode 100644 src/TwigHooks/src/Hook/NameGenerator/TemplateNameGenerator.php create mode 100644 src/TwigHooks/tests/Unit/Hook/NameGenerator/TemplateNameGeneratorTest.php diff --git a/src/TwigHooks/src/Hook/NameGenerator/NameGeneratorInterface.php b/src/TwigHooks/src/Hook/NameGenerator/NameGeneratorInterface.php new file mode 100644 index 00000000..3f9a8bbf --- /dev/null +++ b/src/TwigHooks/src/Hook/NameGenerator/NameGeneratorInterface.php @@ -0,0 +1,10 @@ +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('/(?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('/(?nameGenerator->generate($base, ...$parts); } /** diff --git a/src/TwigHooks/tests/Unit/Hook/NameGenerator/TemplateNameGeneratorTest.php b/src/TwigHooks/tests/Unit/Hook/NameGenerator/TemplateNameGeneratorTest.php new file mode 100644 index 00000000..c10a0a8d --- /dev/null +++ b/src/TwigHooks/tests/Unit/Hook/NameGenerator/TemplateNameGeneratorTest.php @@ -0,0 +1,39 @@ +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(); + } +}