Skip to content

Commit c88dbe7

Browse files
committed
minor #322 [LiveComponent] Move defaultAction validation to compiler pass (norkunas)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Move defaultAction validation to compiler pass | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | N/A | License | MIT Completes `todo` note which was in source code :) Commits ------- 367da01 [LiveComponent] Move defaultAction validation to compiler pass
2 parents 5bdcb17 + 367da01 commit c88dbe7

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LiveComponent\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* @author Kevin Bond <[email protected]>
19+
*
20+
* @experimental
21+
*
22+
* @internal
23+
*/
24+
final class ComponentDefaultActionPass implements CompilerPassInterface
25+
{
26+
public function process(ContainerBuilder $container): void
27+
{
28+
foreach ($container->findTaggedServiceIds('twig.component') as $class => $component) {
29+
if (!($component[0]['live'] ?? false)) {
30+
continue;
31+
}
32+
33+
$defaultAction = trim($component[0]['default_action'] ?? '__invoke', '()');
34+
35+
if (!method_exists($class, $defaultAction)) {
36+
throw new \LogicException(sprintf('Live component "%s" (%s) requires the default action method "%s".%s', $class, $component[0]['key'], $defaultAction, '__invoke' === $defaultAction ? ' Either add this method or use the DefaultActionTrait' : ''));
37+
}
38+
}
39+
}
40+
}

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ public function onKernelRequest(RequestEvent $event): void
8989
if ('get' === $action) {
9090
$defaultAction = trim($metadata->get('default_action', '__invoke'), '()');
9191

92-
if (!method_exists($metadata->getClass(), $defaultAction)) {
93-
// todo should this check be in a compiler pass to ensure fails at compile time?
94-
throw new \LogicException(sprintf('Live component "%s" (%s) requires the default action method "%s".%s', $metadata->getClass(), $componentName, $defaultAction, '__invoke' === $defaultAction ? ' Either add this method or use the DefaultActionTrait' : ''));
95-
}
96-
9792
// set default controller for "default" action
9893
$request->attributes->set('_controller', sprintf('%s::%s', $metadata->getServiceId(), $defaultAction));
9994
$request->attributes->set('_component_default_action', true);

src/LiveComponent/src/LiveComponentBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\HttpKernel\Bundle\Bundle;
17+
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\ComponentDefaultActionPass;
1718
use Symfony\UX\LiveComponent\DependencyInjection\Compiler\OptionalDependencyPass;
1819

1920
/**
@@ -27,5 +28,6 @@ public function build(ContainerBuilder $container): void
2728
{
2829
// must run before Symfony\Component\Serializer\DependencyInjection\SerializerPass
2930
$container->addCompilerPass(new OptionalDependencyPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
31+
$container->addCompilerPass(new ComponentDefaultActionPass());
3032
}
3133
}

src/LiveComponent/tests/Fixtures/Component/ComponentWithArrayProp.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
66
use Symfony\UX\LiveComponent\Attribute\LiveProp;
7+
use Symfony\UX\LiveComponent\DefaultActionTrait;
78

89
/**
910
* @author Kevin Bond <[email protected]>
1011
*/
1112
#[AsLiveComponent('component_with_array_prop')]
1213
final class ComponentWithArrayProp
1314
{
15+
use DefaultActionTrait;
16+
1417
#[LiveProp]
1518
public array $prop = [];
1619
}

src/LiveComponent/tests/Fixtures/Component/CustomAttributes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
44

55
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
6+
use Symfony\UX\LiveComponent\DefaultActionTrait;
67

78
/**
89
* @author Kevin Bond <[email protected]>
910
*/
1011
#[AsLiveComponent('custom_attributes', attributesVar: '_custom_attributes')]
1112
final class CustomAttributes
1213
{
13-
14+
use DefaultActionTrait;
1415
}

0 commit comments

Comments
 (0)