-
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.
[Twig Hooks] Allow to define a section in the hook name that will be …
…discarded while creating a hook name
- Loading branch information
1 parent
37d0052
commit 6b0a93a
Showing
14 changed files
with
164 additions
and
102 deletions.
There are no files selected for viewing
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
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
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
10 changes: 0 additions & 10 deletions
10
src/TwigHooks/src/Hook/NameGenerator/NameGeneratorInterface.php
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
src/TwigHooks/src/Hook/NameGenerator/TemplateNameGenerator.php
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
src/TwigHooks/src/Hook/Normalizer/CompositeNameNormalizer.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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigHooks\Hook\Normalizer; | ||
|
||
final class CompositeNameNormalizer implements NameNormalizerInterface | ||
{ | ||
/** @var array<NameNormalizerInterface> */ | ||
private readonly array $nameNormalizers; | ||
|
||
/** | ||
* @param iterable<NameNormalizerInterface> $nameNormalizers | ||
*/ | ||
public function __construct(iterable $nameNormalizers) | ||
{ | ||
$this->nameNormalizers = $nameNormalizers instanceof \Traversable ? iterator_to_array($nameNormalizers) : $nameNormalizers; | ||
} | ||
|
||
public function normalize(string $name): string | ||
{ | ||
$normalizedHookName = $name; | ||
|
||
foreach ($this->nameNormalizers as $nameNormalizer) { | ||
$normalizedHookName = $nameNormalizer->normalize($normalizedHookName); | ||
} | ||
|
||
return $normalizedHookName; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/TwigHooks/src/Hook/Normalizer/NameNormalizerInterface.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\Normalizer; | ||
|
||
interface NameNormalizerInterface | ||
{ | ||
public function normalize(string $name): string; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/TwigHooks/src/Hook/Normalizer/RemoveSectionPartNormalizer.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigHooks\Hook\Normalizer; | ||
|
||
final class RemoveSectionPartNormalizer implements NameNormalizerInterface | ||
{ | ||
/** | ||
* @param non-empty-string|false $separator | ||
*/ | ||
public function __construct(private readonly string|false $separator) | ||
{ | ||
} | ||
|
||
public function normalize(string $name): string | ||
{ | ||
if (false === $this->separator) { | ||
return $name; | ||
} | ||
|
||
$parts = explode('.', $name); | ||
$result = []; | ||
|
||
foreach ($parts as $part) { | ||
$hookNameExplodedBySectionSeparator = explode($this->separator, $part); | ||
|
||
$result[] = current($hookNameExplodedBySectionSeparator); | ||
} | ||
|
||
return implode('.', $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
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
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
40 changes: 0 additions & 40 deletions
40
src/TwigHooks/tests/Unit/Hook/NameGenerator/TemplateNameGeneratorTest.php
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
src/TwigHooks/tests/Unit/Hook/Normalizer/CompositeNameNormalizerTest.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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\TwigHooks\Unit\Hook\Normalizer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\TwigHooks\Hook\Normalizer\CompositeNameNormalizer; | ||
use Sylius\TwigHooks\Hook\Normalizer\NameNormalizerInterface; | ||
|
||
final class CompositeNameNormalizerTest extends TestCase | ||
{ | ||
public function testItNormalizesNameUsingPassedNormalizers(): void | ||
{ | ||
$dummyNormalizer = $this->createMock(NameNormalizerInterface::class); | ||
$dummyNormalizer->expects($this->once())->method('normalize')->with('hook_name')->willReturn('hook_name_normalized'); | ||
$zummyNormalizer = $this->createMock(NameNormalizerInterface::class); | ||
$zummyNormalizer->expects($this->once())->method('normalize')->with('hook_name_normalized')->willReturn('hook_name_normalized_normalized'); | ||
|
||
$compositeNameNormalizer = new CompositeNameNormalizer([$dummyNormalizer, $zummyNormalizer]); | ||
$normalizedHookName = $compositeNameNormalizer->normalize('hook_name'); | ||
|
||
$this->assertSame('hook_name_normalized_normalized', $normalizedHookName); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/TwigHooks/tests/Unit/Hook/Normalizer/RemoveSectionPartNormalizerTest.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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\TwigHooks\Unit\Hook\Normalizer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\TwigHooks\Hook\Normalizer\RemoveSectionPartNormalizer; | ||
|
||
final class RemoveSectionPartNormalizerTest extends TestCase | ||
{ | ||
public function testItRemovesSectionPartFromName(): void | ||
{ | ||
$removeSectionPartNormalizer = new RemoveSectionPartNormalizer(separator: '#'); | ||
|
||
$this->assertSame('hook_name_section', $removeSectionPartNormalizer->normalize('hook_name_section')); | ||
$this->assertSame('hook_name', $removeSectionPartNormalizer->normalize('hook_name#section')); | ||
$this->assertSame('hook_name.section', $removeSectionPartNormalizer->normalize('hook_name.section')); | ||
} | ||
|
||
public function testItRemovesSectionPartFromNameWithUsingOtherSeparatorThanHash(): void | ||
{ | ||
$removeSectionPartNormalizer = new RemoveSectionPartNormalizer(separator: '|'); | ||
|
||
$this->assertSame('hook_name_section', $removeSectionPartNormalizer->normalize('hook_name_section')); | ||
$this->assertSame('hook_name', $removeSectionPartNormalizer->normalize('hook_name|section')); | ||
$this->assertSame('hook_name.section', $removeSectionPartNormalizer->normalize('hook_name.section')); | ||
} | ||
|
||
public function testItSkipsRemovingSectionPartIfSeparatorIsFalse(): void | ||
{ | ||
$removeSectionPartNormalizer = new RemoveSectionPartNormalizer(separator: false); | ||
|
||
$this->assertSame('hook_name_section', $removeSectionPartNormalizer->normalize('hook_name_section')); | ||
$this->assertSame('hook_name#section', $removeSectionPartNormalizer->normalize('hook_name#section')); | ||
$this->assertSame('hook_name.section', $removeSectionPartNormalizer->normalize('hook_name.section')); | ||
} | ||
} |