19
19
use Symfony \Component \Console \Output \OutputInterface ;
20
20
use Symfony \Component \Console \Style \SymfonyStyle ;
21
21
use Symfony \Component \Finder \Finder ;
22
+ use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
23
+ use Symfony \UX \LiveComponent \Attribute \LiveProp ;
24
+ use Symfony \UX \TwigComponent \Attribute \AsTwigComponent ;
22
25
use Symfony \UX \TwigComponent \ComponentFactory ;
23
26
use Symfony \UX \TwigComponent \Twig \PropsNode ;
24
27
use Twig \Environment ;
@@ -62,24 +65,41 @@ protected function execute(InputInterface $input, OutputInterface $output): int
62
65
}
63
66
64
67
$ class = $ metadata ->get ('class ' );
68
+ $ type = null ;
65
69
$ allProperties = [];
66
70
67
71
if ($ class ) {
68
72
$ propertyLabel = 'Properties (type / name / default value if exist) ' ;
73
+ $ type = 'AsTwigComponent ' ;
74
+
75
+ if ($ metadata ->get ('live ' )) {
76
+ $ type = 'AsLiveComponent ' ;
77
+ }
69
78
70
79
$ reflectionClass = new \ReflectionClass ($ class );
71
80
$ properties = $ reflectionClass ->getProperties ();
81
+ $ allLiveProperties = [];
72
82
73
83
foreach ($ properties as $ property ) {
74
84
if ($ property ->isPublic ()) {
75
85
$ visibility = $ property ->getType ()?->getName();
76
86
$ propertyName = $ property ->getName ();
77
87
$ value = $ property ->getDefaultValue ();
88
+ $ propertyAttributes = $ property ->getAttributes (LiveProp::class);
78
89
79
- $ allProperties = [
80
- ...$ allProperties ,
81
- $ visibility .' $ ' .$ propertyName .(null !== $ value ? ' = ' .$ value : '' ),
82
- ];
90
+ $ propertyDisplay = $ visibility .' $ ' .$ propertyName .(null !== $ value ? ' = ' .$ value : '' );
91
+
92
+ if (\count ($ propertyAttributes ) > 0 ) {
93
+ $ allLiveProperties = [
94
+ ...$ allLiveProperties ,
95
+ $ propertyDisplay ,
96
+ ];
97
+ } else {
98
+ $ allProperties = [
99
+ ...$ allProperties ,
100
+ $ propertyDisplay ,
101
+ ];
102
+ }
83
103
}
84
104
}
85
105
} else {
@@ -124,11 +144,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
124
144
125
145
$ componentInfos = [
126
146
['Component ' , $ name ],
147
+ ['Type ' , $ type ],
127
148
['Class ' , $ class ?? 'Anonymous component ' ],
128
149
['Template ' , $ metadata ->getTemplate ()],
129
150
[$ propertyLabel , \count ($ allProperties ) > 0 ? implode ("\n" , $ allProperties ) : null ],
130
151
];
131
152
153
+ if (isset ($ allLiveProperties ) && \count ($ allLiveProperties ) > 0 ) {
154
+ $ componentInfos [] = ['Live Properties ' , implode ("\n" , $ allLiveProperties )];
155
+ }
156
+
132
157
$ table = new Table ($ output );
133
158
$ table ->setHeaders (['Property ' , 'Value ' ]);
134
159
@@ -155,53 +180,64 @@ protected function execute(InputInterface $input, OutputInterface $output): int
155
180
$ attributes = $ reflectionClass ->getAttributes ();
156
181
157
182
foreach ($ attributes as $ attribute ) {
158
- $ arguments = $ attribute ->getArguments ();
159
-
160
- $ name = $ arguments ['name ' ] ?? $ arguments [0 ] ?? null ;
161
- $ template = $ arguments ['template ' ] ?? $ arguments [1 ] ?? null ;
162
-
163
- if (null !== $ template || null !== $ name ) {
164
- if (null !== $ template && null !== $ name ) {
165
- $ templateFile = str_replace ('components/ ' , '' , $ template );
166
- $ metadata = $ this ->componentFactory ->metadataFor ($ name );
167
- } elseif (null !== $ name ) {
168
- $ templateFile = str_replace (': ' , '/ ' , "{$ name }.html.twig " );
169
- $ metadata = $ this ->componentFactory ->metadataFor ($ name );
183
+ $ attributeName = $ attribute ->getName ();
184
+
185
+ if (\in_array ($ attributeName , [AsTwigComponent::class, AsLiveComponent::class])) {
186
+ $ arguments = $ attribute ->getArguments ();
187
+
188
+ $ name = $ arguments ['name ' ] ?? $ arguments [0 ] ?? null ;
189
+ $ template = $ arguments ['template ' ] ?? $ arguments [1 ] ?? null ;
190
+
191
+ if (null !== $ template || null !== $ name ) {
192
+ if (null !== $ template && null !== $ name ) {
193
+ $ templateFile = str_replace ('components/ ' , '' , $ template );
194
+ $ metadata = $ this ->componentFactory ->metadataFor ($ name );
195
+ } elseif (null !== $ name ) {
196
+ $ templateFile = str_replace (': ' , '/ ' , "{$ name }.html.twig " );
197
+ $ metadata = $ this ->componentFactory ->metadataFor ($ name );
198
+ } else {
199
+ $ templateFile = str_replace ('components/ ' , '' , $ template );
200
+ $ metadata = $ this ->componentFactory ->metadataFor (str_replace ('.html.twig ' , '' , $ templateFile ));
201
+ }
170
202
} else {
171
- $ templateFile = str_replace ( ' components/ ' , '' , $ template ) ;
172
- $ metadata = $ this ->componentFactory ->metadataFor (str_replace ( ' .html.twig ' , '' , $ templateFile ));
203
+ $ templateFile = "{ $ reflectionClass -> getShortName ()} .html.twig " ;
204
+ $ metadata = $ this ->componentFactory ->metadataFor ($ reflectionClass -> getShortName ( ));
173
205
}
174
- } else {
175
- $ templateFile = "{$ reflectionClass ->getShortName ()}.html.twig " ;
176
- $ metadata = $ this ->componentFactory ->metadataFor ($ reflectionClass ->getShortName ());
177
- }
178
206
179
- $ componentsWithClass [] = $ metadata ->getName ();
207
+ $ componentsWithClass [] = [
208
+ 'name ' => $ metadata ->getName (),
209
+ 'type ' => substr ($ attributeName , strrpos ($ attributeName , '\\' ) + 1 ),
210
+ ];
180
211
181
- if (($ key = array_search ($ templateFile , $ anonymousTemplatesComponents )) !== false ) {
182
- unset($ anonymousTemplatesComponents [$ key ]);
212
+ if (($ key = array_search ($ templateFile , $ anonymousTemplatesComponents )) !== false ) {
213
+ unset($ anonymousTemplatesComponents [$ key ]);
214
+ }
183
215
}
184
216
}
185
217
}
186
218
187
- $ anonymousComponents = array_map (fn ($ template ): string => str_replace ('/ ' , ': ' , str_replace ('.html.twig ' , '' , $ template )), $ anonymousTemplatesComponents );
219
+ $ anonymousComponents = array_map (fn ($ template ): array => [
220
+ 'name ' => str_replace ('/ ' , ': ' , str_replace ('.html.twig ' , '' , $ template )),
221
+ 'type ' => null ,
222
+ ], $ anonymousTemplatesComponents );
188
223
189
224
$ allComponents = array_merge ($ componentsWithClass , $ anonymousComponents );
190
225
$ dataToRender = [];
191
226
foreach ($ allComponents as $ component ) {
192
- $ metadata = $ this ->componentFactory ->metadataFor ($ component );
227
+ $ metadata = $ this ->componentFactory ->metadataFor ($ component[ ' name ' ] );
193
228
194
229
$ dataToRender = [...$ dataToRender ,
195
230
[
196
231
$ metadata ->getName (),
197
232
$ metadata ->get ('class ' ) ?? 'Anonymous component ' ,
198
233
$ metadata ->getTemplate (),
234
+ $ component ['type ' ],
199
235
],
200
236
];
201
237
}
202
238
203
239
$ table = new Table ($ output );
204
- $ table ->setHeaders (['Component ' , 'Class ' , 'Template ' ]);
240
+ $ table ->setHeaders (['Component ' , 'Class ' , 'Template ' , ' Type ' ]);
205
241
206
242
foreach ($ dataToRender as $ data ) {
207
243
$ table ->addRow ($ data );
0 commit comments