forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the "sylius.live_component" tag and the translation layer
- Loading branch information
1 parent
e694fbd
commit 6b26f4a
Showing
4 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/Sylius/Bundle/UiBundle/DependencyInjection/Compiler/LiveComponentTagPass.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,43 @@ | ||
<?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\Bundle\UiBundle\DependencyInjection\Compiler; | ||
|
||
use InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
final class LiveComponentTagPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
foreach ($container->findTaggedServiceIds('sylius.live_component') as $id => $tags) { | ||
foreach ($tags as $tag) { | ||
$liveComponentService = $container->getDefinition($id); | ||
$liveComponentService->addTag('twig.component', [ | ||
'key' => $tag['key'] ?? throw new InvalidArgumentException('The "key" attribute is required for the "sylius.live_component" tag'), | ||
'template' => $tag['template'] ?? throw new InvalidArgumentException('The "template" attribute is required for the "sylius.live_component" tag'), | ||
'expose_public_props' => $tag['expose_public_props'] ?? true, | ||
'attributes_var' => $tag['attributes_var'] ?? 'attributes', | ||
'default_action' => $tag['default_action'] ?? null, | ||
'live' => true, | ||
'csrf' => $tag['csrf'] ?? true, | ||
'route' => $tag['route'] ?? 'ux_live_component', | ||
'method' => $tag['method'] ?? 'post', | ||
'url_reference_type' => $tag['url_reference_type'] ?? UrlGeneratorInterface::ABSOLUTE_PATH, | ||
]); | ||
} | ||
} | ||
} | ||
} |
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
118 changes: 118 additions & 0 deletions
118
src/Sylius/Bundle/UiBundle/Tests/DependencyInjection/Compiler/LiveComponentTagPassTest.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,118 @@ | ||
<?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 DependencyInjection\Compiler; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; | ||
use Sylius\Bundle\UiBundle\DependencyInjection\Compiler\LiveComponentTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
final class LiveComponentTagPassTest extends AbstractCompilerPassTestCase | ||
{ | ||
public function testAddingTwigComponentTagToServicesTaggedWithLiveComponentTag(): void | ||
{ | ||
$liveComponent = new Definition(); | ||
$liveComponent->addTag('sylius.live_component', ['key' => 'foo', 'template' => 'bar']); | ||
|
||
$this->setDefinition('my_live_component', $liveComponent); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasServiceDefinitionWithTag( | ||
'my_live_component', | ||
'twig.component', | ||
[ | ||
'key' => 'foo', | ||
'template' => 'bar', | ||
'expose_public_props' => true, | ||
'attributes_var' => 'attributes', | ||
'default_action' => null, | ||
'live' => true, | ||
'csrf' => true, | ||
'route' => 'ux_live_component', | ||
'method' => 'post', | ||
'url_reference_type' => UrlGeneratorInterface::ABSOLUTE_PATH, | ||
] | ||
); | ||
} | ||
|
||
public function testOverridingTagAttributesWithLiveComponentTag(): void | ||
{ | ||
$liveComponent = new Definition(); | ||
$liveComponent->addTag('sylius.live_component', [ | ||
'key' => 'foo', | ||
'template' => 'bar', | ||
'expose_public_props' => false, | ||
'attributes_var' => 'custom_attributes', | ||
'default_action' => 'customAction', | ||
'csrf' => false, | ||
'route' => 'custom_route', | ||
'method' => 'get', | ||
'url_reference_type' => UrlGeneratorInterface::ABSOLUTE_URL, | ||
]); | ||
|
||
$this->setDefinition('my_live_component', $liveComponent); | ||
|
||
$this->compile(); | ||
|
||
$this->assertContainerBuilderHasServiceDefinitionWithTag( | ||
'my_live_component', | ||
'twig.component', | ||
[ | ||
'key' => 'foo', | ||
'template' => 'bar', | ||
'expose_public_props' => false, | ||
'attributes_var' => 'custom_attributes', | ||
'default_action' => 'customAction', | ||
'live' => true, | ||
'csrf' => false, | ||
'route' => 'custom_route', | ||
'method' => 'get', | ||
'url_reference_type' => UrlGeneratorInterface::ABSOLUTE_URL, | ||
] | ||
); | ||
} | ||
|
||
public function testThrowingExceptionWhenKeyIsNotPresentOnLiveComponentTag(): void | ||
{ | ||
$liveComponent = new Definition(); | ||
$liveComponent->addTag('sylius.live_component', ['template' => 'bar']); | ||
|
||
$this->setDefinition('my_live_component', $liveComponent); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The "key" attribute is required for the "sylius.live_component" tag'); | ||
|
||
$this->compile(); | ||
} | ||
|
||
public function testThrowingExceptionWhenTemplateIsNotPresentOnLiveComponentTag(): void | ||
{ | ||
$liveComponent = new Definition(); | ||
$liveComponent->addTag('sylius.live_component', ['key' => 'foo']); | ||
|
||
$this->setDefinition('my_live_component', $liveComponent); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The "template" attribute is required for the "sylius.live_component" tag'); | ||
|
||
$this->compile(); | ||
} | ||
|
||
protected function registerCompilerPass(ContainerBuilder $container): void | ||
{ | ||
$container->addCompilerPass(new LiveComponentTagPass()); | ||
} | ||
} |
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