-
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.
[TwigHooks] Template configuration provider with context (#208)
Fixes #11 ```yaml sylius_twig_hooks: hooks: 'sylius_admin.book.show.content.header.title_block': title: template: '@SyliusBootstrapAdminUi/shared/crud/common/content/header/title_block/title.html.twig' configuration: title: '@=_context.book.getTitle()' ```
- Loading branch information
Showing
12 changed files
with
202 additions
and
15 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
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
72 changes: 72 additions & 0 deletions
72
src/TwigHooks/src/Provider/TemplateConfigurationProvider.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,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigHooks\Provider; | ||
|
||
use Sylius\TwigHooks\Hookable\HookableTemplate; | ||
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata; | ||
use Sylius\TwigHooks\Provider\Exception\InvalidExpressionException; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
||
final class TemplateConfigurationProvider implements TemplateConfigurationProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly ExpressionLanguage $expressionLanguage, | ||
) { | ||
} | ||
|
||
public function provide(HookableTemplate $hookable, HookableMetadata $metadata): array | ||
{ | ||
$values = [ | ||
'_context' => $metadata->context, | ||
]; | ||
|
||
return $this->mapArrayRecursively(function (mixed $value) use ($values, $hookable): mixed { | ||
if (is_string($value) && str_starts_with($value, '@=')) { | ||
try { | ||
return $this->expressionLanguage->evaluate(substr($value, 2), $values); | ||
} catch (\Throwable $e) { | ||
throw new InvalidExpressionException( | ||
sprintf( | ||
'Failed to evaluate the "%s" expression while rendering the "%s" hookable in the "%s" hook. Error: %s".', | ||
$value, | ||
$hookable->name, | ||
$hookable->hookName, | ||
$e->getMessage(), | ||
), | ||
previous: $e, | ||
); | ||
} | ||
} | ||
|
||
return $value; | ||
}, $hookable->configuration); | ||
} | ||
|
||
/** | ||
* @param array<array-key, mixed> $array | ||
* | ||
* @return array<array-key, mixed> | ||
*/ | ||
private function mapArrayRecursively(callable $callback, array $array): array | ||
{ | ||
$result = []; | ||
foreach ($array as $key => $value) { | ||
$result[$key] = is_array($value) | ||
? $this->mapArrayRecursively($callback, $value) | ||
: $callback($value); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/TwigHooks/src/Provider/TemplateConfigurationProviderInterface.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,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigHooks\Provider; | ||
|
||
use Sylius\TwigHooks\Hookable\HookableTemplate; | ||
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata; | ||
use Sylius\TwigHooks\Provider\Exception\InvalidExpressionException; | ||
|
||
interface TemplateConfigurationProviderInterface | ||
{ | ||
/** | ||
* @throws InvalidExpressionException | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function provide(HookableTemplate $hookable, HookableMetadata $metadata): array; | ||
} |
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
63 changes: 63 additions & 0 deletions
63
src/TwigHooks/tests/Unit/Provider/TemplatesConfigurationProviderTest.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,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\TwigHooks\Unit\Provider; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\TwigHooks\Provider\TemplateConfigurationProvider; | ||
use Sylius\TwigHooks\Provider\TemplateConfigurationProviderInterface; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableMetadataMotherObject; | ||
use Tests\Sylius\TwigHooks\Utils\MotherObject\HookableTemplateMotherObject; | ||
|
||
final class TemplatesConfigurationProviderTest extends TestCase | ||
{ | ||
public function testItReturnsEmptyArrayWhenNoConfigurationDefined(): void | ||
{ | ||
$hookable = HookableTemplateMotherObject::withConfiguration([]); | ||
$metadata = HookableMetadataMotherObject::some(); | ||
|
||
$templateConfigurationProvider = $this->createTestSubject(); | ||
|
||
$this->assertSame([], $templateConfigurationProvider->provide($hookable, $metadata)); | ||
} | ||
|
||
public function testItReturnsConfiguration(): void | ||
{ | ||
$hookable = HookableTemplateMotherObject::withConfiguration(['message' => 'Hello, World!']); | ||
$metadata = HookableMetadataMotherObject::some(); | ||
|
||
$templateConfigurationProvider = $this->createTestSubject(); | ||
|
||
$this->assertSame(['message' => 'Hello, World!'], $templateConfigurationProvider->provide($hookable, $metadata)); | ||
} | ||
|
||
public function testItEvaluatesExpressions(): void | ||
{ | ||
$hookable = HookableTemplateMotherObject::withConfiguration([ | ||
'username' => '@=_context.username', | ||
]); | ||
$metadata = HookableMetadataMotherObject::withContext( | ||
['username' => 'Jacob'], | ||
); | ||
|
||
$templateConfigurationProvider = $this->createTestSubject(); | ||
|
||
$this->assertSame(['username' => 'Jacob'], $templateConfigurationProvider->provide($hookable, $metadata)); | ||
} | ||
|
||
private function createTestSubject(): TemplateConfigurationProviderInterface | ||
{ | ||
return new TemplateConfigurationProvider(new ExpressionLanguage()); | ||
} | ||
} |
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