-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
207 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
src/TwigExtra/src/Symfony/DependencyInjection/Configuration.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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigExtra\Symfony\DependencyInjection; | ||
|
||
use Sylius\TwigHooks\Hookable\DisabledHookable; | ||
use Sylius\TwigHooks\Hookable\HookableComponent; | ||
use Sylius\TwigHooks\Hookable\HookableTemplate; | ||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
final class Configuration implements ConfigurationInterface | ||
{ | ||
public function getConfigTreeBuilder(): TreeBuilder | ||
{ | ||
$treeBuilder = new TreeBuilder('sylius_twig_extra'); | ||
|
||
$rootNode = $treeBuilder->getRootNode(); | ||
|
||
$this->addTwigUxConfiguration($rootNode); | ||
|
||
return $treeBuilder; | ||
} | ||
|
||
private function addTwigUxConfiguration(ArrayNodeDefinition $rootNode): void | ||
{ | ||
$rootNode | ||
->children() | ||
->arrayNode('twig_ux') | ||
->children() | ||
->arrayNode('anonymous_component_template_prefixes') | ||
->useAttributeAsKey('prefix_name') | ||
->scalarPrototype()->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\TwigExtra\Twig\Ux; | ||
|
||
use Symfony\UX\TwigComponent\ComponentTemplateFinderInterface; | ||
use Twig\Loader\LoaderInterface; | ||
|
||
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface | ||
{ | ||
/** @param array<string, string> $anonymousComponentTemplatePrefixes */ | ||
public function __construct( | ||
private readonly ComponentTemplateFinderInterface $decorated, | ||
private readonly LoaderInterface $loader, | ||
private readonly array $anonymousComponentTemplatePrefixes, | ||
) { | ||
} | ||
|
||
public function findAnonymousComponentTemplate(string $name): ?string | ||
{ | ||
foreach ($this->anonymousComponentTemplatePrefixes as $prefixName => $prefixTemplatePath) { | ||
$prefixName = sprintf('%s:', $prefixName); | ||
if (str_starts_with($name, $prefixName)) { | ||
return $this->getTemplatePath($name, $prefixName, $prefixTemplatePath); | ||
} | ||
} | ||
|
||
return $this->decorated->findAnonymousComponentTemplate($name); | ||
} | ||
|
||
private function getTemplatePath(string $name, string $prefixName, string $prefixTemplatePath): ?string | ||
{ | ||
$templatePath = sprintf('%s/%s.html.twig', $prefixTemplatePath, $this->normalizeName($name, $prefixName)); | ||
|
||
if ($this->loader->exists($templatePath)) { | ||
return $templatePath; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function normalizeName(string $name, string $prefixName): string | ||
{ | ||
return str_replace(':', '/', str_replace($prefixName, '', $name)); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/TwigExtra/tests/Integration/DependencyInjection/TwigExtraExtensionTest.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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\TwigExtra\Integration\DependencyInjection; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; | ||
use Sylius\TwigExtra\Symfony\DependencyInjection\TwigExtraExtension; | ||
use Sylius\TwigExtra\Twig\Ux\ComponentTemplateFinder; | ||
|
||
final class TwigExtraExtensionTest extends AbstractExtensionTestCase | ||
{ | ||
public function testItRegistersTwigUxComponentTemplateFinder(): void | ||
{ | ||
$this->load(); | ||
|
||
$this->assertContainerBuilderHasService('sylius_twig_extra.twig.ux.component_template_finder', ComponentTemplateFinder::class); | ||
} | ||
|
||
public function testItRegistersTwigUxAnonymousComponentTemplatePrefixesParameter(): void | ||
{ | ||
$this->load([ | ||
'twig_ux' => [ | ||
'anonymous_component_template_prefixes' => [ | ||
'sylius_admin_ui:component' => '@SyliusAdminUi/components', | ||
], | ||
], | ||
]); | ||
|
||
$this->assertContainerBuilderHasParameter('sylius_twig_extra.twig_ux.anonymous_component_template_prefixes', [ | ||
'sylius_admin_ui:component' => '@SyliusAdminUi/components', | ||
]); | ||
} | ||
|
||
protected function getContainerExtensions(): array | ||
{ | ||
return [ | ||
new TwigExtraExtension(), | ||
]; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/TwigExtra/tests/Unit/Twig/Ux/ComponentTemplateFinderTest.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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Sylius\TwigExtra\Unit\Twig\Ux; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\TwigExtra\Twig\Ux\ComponentTemplateFinder; | ||
use Symfony\UX\TwigComponent\ComponentTemplateFinder as DecoratedComponentTemplateFinder; | ||
use Twig\Loader\ArrayLoader; | ||
use Twig\Loader\LoaderInterface; | ||
|
||
final class ComponentTemplateFinderTest extends TestCase | ||
{ | ||
public function testItCallsDecoratedFinderIfNoPrefixMatches(): void | ||
{ | ||
$componentTemplateFinder = $this->createTemplateFinder( | ||
['components/sylius_admin_ui/component.html.twig' => ''], | ||
[], | ||
); | ||
|
||
$this->assertEquals( | ||
'components/sylius_admin_ui/component.html.twig', | ||
$componentTemplateFinder->findAnonymousComponentTemplate('sylius_admin_ui:component'), | ||
); | ||
} | ||
|
||
public function testItFindsAnonymousComponentTemplate(): void | ||
{ | ||
$componentTemplateFinder = $this->createTemplateFinder( | ||
['@SyliusBootstrapUi/components/some_sub_component.html.twig' => ''], | ||
['sylius_admin_ui:component' => '@SyliusBootstrapUi/components'], | ||
); | ||
|
||
$this->assertEquals( | ||
'@SyliusBootstrapUi/components/some_sub_component.html.twig', | ||
$componentTemplateFinder->findAnonymousComponentTemplate('sylius_admin_ui:component:some_sub_component'), | ||
); | ||
} | ||
|
||
private function createTemplateFinder(array $templates, array $anonymousComponentTemplatePrefixes): ComponentTemplateFinder | ||
{ | ||
return new ComponentTemplateFinder( | ||
new DecoratedComponentTemplateFinder( | ||
$this->createLoader($templates), | ||
'components', | ||
), | ||
$this->createLoader($templates), | ||
$anonymousComponentTemplatePrefixes, | ||
); | ||
} | ||
|
||
private function createLoader(array $templates): LoaderInterface | ||
{ | ||
return new ArrayLoader($templates); | ||
} | ||
} |