-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract generating hook name outside the HooksRuntime
- Loading branch information
1 parent
a154813
commit 623bcc6
Showing
4 changed files
with
91 additions
and
26 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/TwigHooks/src/Hook/NameGenerator/NameGeneratorInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
src/TwigHooks/src/Hook/NameGenerator/TemplateNameGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/TwigHooks/tests/Unit/Hook/NameGenerator/TemplateNameGeneratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |