-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
AsLiveComponent.php
142 lines (128 loc) · 4.59 KB
/
AsLiveComponent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/*
* 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\Attribute;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
/**
* An attribute to register a LiveComponent.
*
* @see https://symfony.com/bundles/ux-live-component
*
* @author Kevin Bond <kevinbond@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsLiveComponent extends AsTwigComponent
{
/**
* @param string|null $name The component name (ie: TodoList)
* @param string|null $template The template path of the component (ie: components/TodoList.html.twig).
* @param string|null $defaultAction The default action to call when the component is mounted (ie: __invoke)
* @param bool $exposePublicProps Whether to expose every public property as a Twig variable
* @param string $attributesVar The name of the special "attributes" variable in the template
* @param bool $csrf Whether to enable CSRF protection (default: true)
* @param string $route The route used to render the component & handle actions (default: ux_live_component)
* @param int $urlReferenceType Which type of URL should be generated for the given route. Use the constants from UrlGeneratorInterface (default: absolute path, e.g. "/dir/file").
*/
public function __construct(
?string $name = null,
?string $template = null,
private ?string $defaultAction = null,
bool $exposePublicProps = true,
string $attributesVar = 'attributes',
public bool $csrf = true,
public string $route = 'ux_live_component',
public string $method = 'post',
public int $urlReferenceType = UrlGeneratorInterface::ABSOLUTE_PATH,
) {
parent::__construct($name, $template, $exposePublicProps, $attributesVar);
$this->method = strtolower($this->method);
if (!\in_array($this->method, ['get', 'post'])) {
throw new \UnexpectedValueException('$method must be either \'get\' or \'post\'');
}
}
/**
* @internal
*/
public function serviceConfig(): array
{
return array_merge(parent::serviceConfig(), [
'default_action' => $this->defaultAction,
'live' => true,
'csrf' => $this->csrf,
'route' => $this->route,
'method' => $this->method,
'url_reference_type' => $this->urlReferenceType,
]);
}
/**
* @internal
*
* @param object|class-string $component
*/
public static function isActionAllowed(object|string $component, string $action): bool
{
foreach (self::attributeMethodsFor(LiveAction::class, $component) as $method) {
if ($action === $method->getName()) {
return true;
}
}
return false;
}
/**
* @internal
*
* @param object|class-string $component
*
* @return \ReflectionMethod[]
*/
public static function preReRenderMethods(object|string $component): iterable
{
return self::attributeMethodsByPriorityFor($component, PreReRender::class);
}
/**
* @internal
*
* @param object|class-string $component
*
* @return \ReflectionMethod[]
*/
public static function postHydrateMethods(object|string $component): iterable
{
return self::attributeMethodsByPriorityFor($component, PostHydrate::class);
}
/**
* @internal
*
* @param object|class-string $component
*
* @return \ReflectionMethod[]
*/
public static function preDehydrateMethods(object|string $component): iterable
{
return self::attributeMethodsByPriorityFor($component, PreDehydrate::class);
}
/**
* @internal
*
* @param object|class-string $component
*
* @return array<array{action: string, event: string}>
*/
public static function liveListeners(object|string $component): array
{
$listeners = [];
foreach (self::attributeMethodsFor(LiveListener::class, $component) as $method) {
foreach ($method->getAttributes(LiveListener::class) as $attribute) {
$listeners[] = ['action' => $method->getName(), 'event' => $attribute->newInstance()->getEventName()];
}
}
return $listeners;
}
}