Skip to content

Commit

Permalink
Add a twig function for generating twig hooks names
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Dec 19, 2023
1 parent ad06837 commit 878f4d9
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,10 @@
"symfony/runtime": true
},
"sort-packages": true
},
"extra": {
"symfony": {
"require": "6.4.*"
}
}
}
1 change: 1 addition & 0 deletions src/TwigHooks/src/Twig/HooksExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class HooksExtension extends AbstractExtension
public function getFunctions(): array
{
return [
new TwigFunction('hook_name', [HooksRuntime::class, 'createHookName']),
new TwigFunction('hookable_data', [HooksRuntime::class, 'getHookableData'], ['needs_context' => true]),
new TwigFunction('hookable_configuration', [HooksRuntime::class, 'getHookableConfiguration'], ['needs_context' => true]),
];
Expand Down
30 changes: 28 additions & 2 deletions src/TwigHooks/src/Twig/Runtime/HooksRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,35 @@ public function __construct (
}
}

public function createHookName(string $base, string ...$parts): string
{
if ([] === $parts) {
return $this->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);
$resultPart = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $resultPart));

Check failure on line 54 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^5.4)

Parameter #1 $string of function strtolower expects string, string|null given.

Check failure on line 54 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^6.4)

Parameter #1 $string of function strtolower expects string, string|null given.

Check failure on line 54 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^5.4)

Parameter #1 $string of function strtolower expects string, string|null given.

Check failure on line 54 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^6.4)

Parameter #1 $string of function strtolower expects string, string|null given.

Check failure on line 54 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^5.4)

Parameter #1 $string of function strtolower expects string, string|null given.

Check failure on line 54 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^6.4)

Parameter #1 $string of function strtolower expects string, string|null given.
$resultParts[] = $resultPart;
}

return implode('.', $resultParts);
}

/**
* @param array{hookable_data?: array<string, string>} $context
* @return array<string, string>
*/
public function getHookableData(array $context): array

Check failure on line 64 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^5.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableData() has parameter $context with no value type specified in iterable type array.

Check failure on line 64 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^6.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableData() has parameter $context with no value type specified in iterable type array.

Check failure on line 64 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^5.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableData() has parameter $context with no value type specified in iterable type array.

Check failure on line 64 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^6.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableData() has parameter $context with no value type specified in iterable type array.

Check failure on line 64 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^5.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableData() has parameter $context with no value type specified in iterable type array.

Check failure on line 64 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^6.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableData() has parameter $context with no value type specified in iterable type array.
Expand All @@ -40,7 +67,6 @@ public function getHookableData(array $context): array
}

/**
* @param array{hookable_configuration?: array<string, string>} $context
* @return array<string, string>
*/
public function getHookableConfiguration(array $context): array

Check failure on line 72 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^5.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableConfiguration() has parameter $context with no value type specified in iterable type array.

Check failure on line 72 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.1 / Symfony ^6.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableConfiguration() has parameter $context with no value type specified in iterable type array.

Check failure on line 72 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^5.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableConfiguration() has parameter $context with no value type specified in iterable type array.

Check failure on line 72 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.2 / Symfony ^6.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableConfiguration() has parameter $context with no value type specified in iterable type array.

Check failure on line 72 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^5.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableConfiguration() has parameter $context with no value type specified in iterable type array.

Check failure on line 72 in src/TwigHooks/src/Twig/Runtime/HooksRuntime.php

View workflow job for this annotation

GitHub Actions / Continuous Integration (PHP 8.3 / Symfony ^6.4)

Method Sylius\TwigHooks\Twig\Runtime\HooksRuntime::getHookableConfiguration() has parameter $context with no value type specified in iterable type array.
Expand Down
75 changes: 75 additions & 0 deletions src/TwigHooks/tests/Integration/Twig/Runtime/HooksRuntimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\TwigHooks\Integration\Twig\Runtime;

use Sylius\TwigHooks\Twig\Runtime\HooksRuntime;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class HooksRuntimeTest extends KernelTestCase
{
/**
* @dataProvider getHookNameConversionData
*/
public function testItConvertsStringsToHookNames(string $input, string $expectedOutput, array $suffix = []): void
{
$this->assertSame($expectedOutput, $this->getTestSubject()->createHookName($input, ...$suffix));
}

public function testItReturnsHookableData(): void
{
$context = [
'hookable_data' => ['foo' => 'bar'],
'other' => 'data',
];

$this->assertSame(
['foo' => 'bar'],
$this->getTestSubject()->getHookableData($context)
);
}

public function testItReturnsHookableConfiguration(): void
{
$context = [
'hookable_configuration' => ['foo' => 'bar'],
'other' => 'data',
];

$this->assertSame(
['foo' => 'bar'],
$this->getTestSubject()->getHookableConfiguration($context)
);
}

public function testItRendersHook(): void
{
$this->markTestSkipped('Not implemented yet.');
}

public function testItRendersHookable(): void
{
$this->markTestSkipped('Not implemented yet.');
}

/**
* @return array
*/
public function getHookNameConversionData(): array
{
return [
'simple' => ['@SyliusShop/_product.html.twig', 'sylius_shop.product'],
'with slash' => ['@SyliusShop/_product/variant.html.twig', 'sylius_shop.product.variant'],
'with camel case' => ['@SyliusShop/_product/variant/optionValue.html.twig', 'sylius_shop.product.variant.option_value'],
'without bundle prefix' => ['some_template.html.twig', 'some_template'],
'with single suffix part' => ['@SyliusShop/_product.html.twig', 'sylius_shop.product.suffix', ['suffix']],
'with multiple suffix parts' => ['@SyliusShop/_product.html.twig', 'sylius_shop.product.suffix.part', ['suffix', 'part']],
];
}

private function getTestSubject(): HooksRuntime
{
return $this->getContainer()->get(HooksRuntime::class);
}
}

0 comments on commit 878f4d9

Please sign in to comment.