-
-
Notifications
You must be signed in to change notification settings - Fork 298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LiveComponent] Add the live.component
service tag
#1693
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
"doctrine/doctrine-bundle": "^2.4.3", | ||
"doctrine/orm": "^2.9.4", | ||
"doctrine/persistence": "^2.5.2|^3.0", | ||
"matthiasnoback/symfony-dependency-injection-test": "^5.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this absolutely necessary ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course it isn't. But I decided to add it as:
If you want, I can remove it and test the compiler pass other way, but if we plan to create more tests covering extensions/compiler passes, it might be cool to leave it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like this package - as config/compiler passes get more complex it's super useful - let's keep it! |
||
"phpdocumentor/reflection-docblock": "5.x-dev", | ||
"symfony/dependency-injection": "^5.4|^6.0|^7.0", | ||
"symfony/expression-language": "^5.4|^6.0|^7.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\LiveComponent\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Form\Exception\InvalidArgumentException; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
/** | ||
* @author Jacob Tobiasz <jakub.tobiasz@icloud.com> | ||
* | ||
* @internal | ||
*/ | ||
final class LiveComponentTagPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
foreach ($container->findTaggedServiceIds('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 "live.component" tag.'), | ||
'template' => $tag['template'] ?? throw new InvalidArgumentException('The "template" attribute is required for the "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, | ||
]); | ||
$liveComponentService->addTag('controller.service_arguments'); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Integration\DependencyInjection\Compiler; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\LiveComponentTagPass; | ||
|
||
final class LiveComponentTagPassTest extends AbstractCompilerPassTestCase | ||
{ | ||
public function testAddingTwigComponentTagToServicesTaggedWithLiveComponentTag(): void | ||
{ | ||
$liveComponent = new Definition(); | ||
$liveComponent->addTag('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('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('live.component', ['template' => 'bar']); | ||
|
||
$this->setDefinition('my_live_component', $liveComponent); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The "key" attribute is required for the "live.component" tag'); | ||
|
||
$this->compile(); | ||
} | ||
|
||
public function testThrowingExceptionWhenTemplateIsNotPresentOnLiveComponentTag(): void | ||
{ | ||
$liveComponent = new Definition(); | ||
$liveComponent->addTag('live.component', ['key' => 'foo']); | ||
|
||
$this->setDefinition('my_live_component', $liveComponent); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The "template" attribute is required for the "live.component" tag'); | ||
|
||
$this->compile(); | ||
} | ||
|
||
protected function registerCompilerPass(ContainerBuilder $container): void | ||
{ | ||
$container->addCompilerPass(new LiveComponentTagPass()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you create a separate PR for this change ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, the question is whether we want (or must) do it this way. It's a little hack, but on the other hand, the question is whether we need this information in the CI.