-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Twig Hooks] Fix hookables overriding
- Loading branch information
1 parent
ed0487c
commit 91db7f0
Showing
11 changed files
with
266 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Sylius\TwigHooks\Hookable\Merger\HookableMerger; | ||
use Sylius\TwigHooks\Hookable\Merger\HookableMergerInterface; | ||
|
||
return static function (ContainerConfigurator $configurator): void { | ||
$services = $configurator->services(); | ||
|
||
$services->set('twig_hooks.merger.hookable', HookableMerger::class) | ||
->alias(HookableMergerInterface::class, 'twig_hooks.renderer.hookable') | ||
; | ||
}; |
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
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigHooks\Hookable\Merger; | ||
|
||
use Sylius\TwigHooks\Hookable\AbstractHookable; | ||
|
||
final class HookableMerger implements HookableMergerInterface | ||
{ | ||
/** | ||
* @throws \ReflectionException | ||
*/ | ||
public function merge(AbstractHookable ...$hookables): AbstractHookable | ||
{ | ||
if ([] === $hookables) { | ||
throw new \InvalidArgumentException('At least one hookable must be passed to merge.'); | ||
} | ||
|
||
/** @var class-string<AbstractHookable> $class */ | ||
$class = get_class(end($hookables)); | ||
|
||
$serializedHookables = array_map( | ||
static fn (AbstractHookable $hookable): array => $hookable->toArray(), | ||
$hookables, | ||
); | ||
|
||
$inputs = array_merge(...$serializedHookables); | ||
$arguments = $this->createConstructorArguments($class, $inputs); | ||
|
||
return new $class(...$arguments); | ||
} | ||
|
||
|
||
/** | ||
* @param class-string $class | ||
* @param array<string, mixed> $inputs | ||
* | ||
* @return array<string> | ||
* @throws \ReflectionException | ||
*/ | ||
private function createConstructorArguments(string $class, array $inputs): array | ||
{ | ||
$reflection = new \ReflectionClass($class); | ||
/** @var \ReflectionMethod $constructor */ | ||
$constructor = $reflection->getConstructor(); | ||
$parameters = array_map( | ||
static fn (\ReflectionParameter $parameter): string => $parameter->getName(), | ||
$constructor->getParameters(), | ||
); | ||
|
||
$arguments = []; | ||
|
||
foreach ($inputs as $inputName => $input) { | ||
if (!in_array($inputName, $parameters, true)) { | ||
continue; | ||
} | ||
|
||
$arguments[$inputName] = $input; | ||
} | ||
|
||
return $arguments; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/TwigHooks/src/Hookable/Merger/HookableMergerInterface.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigHooks\Hookable\Merger; | ||
|
||
use Sylius\TwigHooks\Hookable\AbstractHookable; | ||
|
||
interface HookableMergerInterface | ||
{ | ||
public function merge(AbstractHookable ...$hookables): AbstractHookable; | ||
} |
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
100 changes: 100 additions & 0 deletions
100
src/TwigHooks/tests/Unit/Hookable/Merger/HookableMergerTest.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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\TwigHooks\Unit\Hookable\Merger; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\TwigHooks\Hookable\HookableComponent; | ||
use Sylius\TwigHooks\Hookable\HookableTemplate; | ||
use Sylius\TwigHooks\Hookable\Merger\HookableMerger; | ||
use Sylius\TwigHooks\Hookable\Merger\HookableMergerInterface; | ||
use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableComponentMotherObject; | ||
use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableTemplateMotherObject; | ||
|
||
final class HookableMergerTest extends TestCase | ||
{ | ||
public function testItReturnsUnchangedHookableWhenOnlyOneHookableIsPassed(): void | ||
{ | ||
$hookable = HookableTemplateMotherObject::with([ | ||
'hookName' => 'some_hook', | ||
'name' => 'some_template_hookable', | ||
'target' => 'index.html.twig', | ||
]); | ||
|
||
$result = $this->getTestSubject()->merge($hookable); | ||
|
||
$this->assertInstanceOf(HookableTemplate::class, $result); | ||
$this->assertSame('some_hook', $result->hookName); | ||
$this->assertSame('some_template_hookable', $result->name); | ||
$this->assertSame('index.html.twig', $result->target); | ||
} | ||
|
||
public function testItOverridesEarlierHookableWithLaterHookableWithSameTypes(): void | ||
{ | ||
$earlierHookable = HookableTemplateMotherObject::with([ | ||
'hookName' => 'common.some_hook', | ||
'name' => 'some_template', | ||
'target' => 'index.html.twig', | ||
]); | ||
$laterHookable = HookableTemplateMotherObject::with([ | ||
'hookName' => 'app.some_hook', | ||
'name' => 'some_template', | ||
'target' => 'base.html.twig', | ||
]); | ||
|
||
$result = $this->getTestSubject()->merge($earlierHookable, $laterHookable); | ||
|
||
$this->assertInstanceOf(HookableTemplate::class, $result); | ||
$this->assertSame('app.some_hook', $result->hookName); | ||
$this->assertSame('some_template', $result->name); | ||
$this->assertSame('base.html.twig', $result->target); | ||
} | ||
|
||
public function testItOverridesEarlierHookableWithLaterHookableWithDifferentTypesAndTheComponentIsTheLastOne(): void | ||
{ | ||
$earlierHookable = HookableTemplateMotherObject::with([ | ||
'hookName' => 'common.some_hook', | ||
'name' => 'some_template', | ||
'target' => 'index.html.twig', | ||
]); | ||
$laterHookable = HookableComponentMotherObject::with([ | ||
'hookName' => 'app.some_hook', | ||
'name' => 'some_template', | ||
'target' => 'app:form', | ||
]); | ||
|
||
$result = $this->getTestSubject()->merge($earlierHookable, $laterHookable); | ||
|
||
$this->assertInstanceOf(HookableComponent::class, $result); | ||
$this->assertSame('app.some_hook', $result->hookName); | ||
$this->assertSame('some_template', $result->name); | ||
$this->assertSame('app:form', $result->target); | ||
} | ||
|
||
public function testItOverridesEarlierHookableWithLaterHookableWithDifferentTypesAndTheTemplateIsTheLastOne(): void | ||
{ | ||
$earlierHookable = HookableComponentMotherObject::with([ | ||
'hookName' => 'common.some_hook', | ||
'name' => 'some_template', | ||
'target' => 'app:form', | ||
]); | ||
$laterHookable = HookableTemplateMotherObject::with([ | ||
'hookName' => 'app.some_hook', | ||
'name' => 'some_template', | ||
'target' => 'index.html.twig', | ||
]); | ||
|
||
$result = $this->getTestSubject()->merge($earlierHookable, $laterHookable); | ||
|
||
$this->assertInstanceOf(HookableTemplate::class, $result); | ||
$this->assertSame('app.some_hook', $result->hookName); | ||
$this->assertSame('some_template', $result->name); | ||
$this->assertSame('index.html.twig', $result->target); | ||
} | ||
|
||
private function getTestSubject(): HookableMergerInterface | ||
{ | ||
return new HookableMerger(); | ||
} | ||
} |
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