Skip to content

Commit

Permalink
Add the "sylius.live_component" tag and the translation layer
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz authored and GSadee committed May 13, 2024
1 parent e694fbd commit 6b26f4a
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 1 deletion.
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,
]);
}
}
}
}
2 changes: 2 additions & 0 deletions src/Sylius/Bundle/UiBundle/SyliusUiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\Bundle\UiBundle;

use Sylius\Bundle\UiBundle\DependencyInjection\Compiler\LegacySonataBlockPass;
use Sylius\Bundle\UiBundle\DependencyInjection\Compiler\LiveComponentTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -25,5 +26,6 @@ final class SyliusUiBundle extends Bundle
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new LegacySonataBlockPass());
$container->addCompilerPass(new LiveComponentTagPass());
}
}
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());
}
}
3 changes: 2 additions & 1 deletion src/Sylius/Bundle/UiBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"symfony/security-core": "^6.4.0",
"symfony/security-bundle": "^6.4.0",
"symfony/templating": "^6.4.0",
"symfony/ux-twig-component": "^2.16",
"symfony/ux-live-component": "^2.17",
"symfony/ux-twig-component": "^2.17",
"symfony/webpack-encore-bundle": "^1.17.1",
"laminas/laminas-stdlib": "^3.3.1"
},
Expand Down

0 comments on commit 6b26f4a

Please sign in to comment.