16
16
use Symfony \Component \Console \Helper \Table ;
17
17
use Symfony \Component \Console \Input \InputArgument ;
18
18
use Symfony \Component \Console \Input \InputInterface ;
19
+ use Symfony \Component \Console \Input \InputOption ;
19
20
use Symfony \Component \Console \Output \OutputInterface ;
20
21
use Symfony \Component \Console \Style \SymfonyStyle ;
21
22
use Symfony \Component \Finder \Finder ;
22
- use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
23
23
use Symfony \UX \LiveComponent \Attribute \LiveAction ;
24
24
use Symfony \UX \LiveComponent \Attribute \LiveProp ;
25
- use Symfony \UX \TwigComponent \Attribute \AsTwigComponent ;
26
25
use Symfony \UX \TwigComponent \Attribute \PostMount ;
27
26
use Symfony \UX \TwigComponent \Attribute \PreMount ;
28
27
use Symfony \UX \TwigComponent \ComponentFactory ;
29
28
use Symfony \UX \TwigComponent \Twig \PropsNode ;
30
29
use Twig \Environment ;
31
30
32
- #[AsCommand(name: 'debug:component ' , description: 'Display current components and them usages for an application ' )]
31
+ #[AsCommand(name: 'debug:twig- component ' , description: 'Display current components and them usages for an application ' )]
33
32
class ComponentDebugCommand extends Command
34
33
{
35
34
public function __construct (private string $ twigTemplatesPath , private ComponentFactory $ componentFactory , private Environment $ twigEnvironment , private iterable $ components )
@@ -42,11 +41,16 @@ protected function configure(): void
42
41
$ this
43
42
->setDefinition ([
44
43
new InputArgument ('name ' , InputArgument::OPTIONAL , 'A component name ' ),
44
+ new InputOption ('dir ' , null , InputOption::VALUE_REQUIRED , 'Show all components with a specific directory in templates ' , 'components ' ),
45
45
])
46
46
->setHelp (<<<'EOF'
47
47
The <info>%command.name%</info> display current components and them usages for an application:
48
48
49
- <info>php %command.full_name%</info>
49
+ <info>php %command.full_name%</info>
50
+
51
+ Find all components with a specific directory in templates by specifying the directory name with the <info>--dir</info> option:
52
+
53
+ <info>php %command.full_name% --dir=bar/foo</info>
50
54
51
55
EOF
52
56
)
@@ -57,6 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
57
61
{
58
62
$ io = new SymfonyStyle ($ input , $ output );
59
63
$ name = $ input ->getArgument ('name ' );
64
+ $ componentsDir = $ input ->getOption ('dir ' );
60
65
61
66
if (null !== $ name ) {
62
67
try {
@@ -199,38 +204,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int
199
204
$ attributes = $ reflectionClass ->getAttributes ();
200
205
201
206
foreach ($ attributes as $ attribute ) {
202
- $ attributeName = $ attribute ->getName ();
203
-
204
- if (\in_array ($ attributeName , [AsTwigComponent::class, AsLiveComponent::class])) {
205
- $ arguments = $ attribute ->getArguments ();
206
-
207
- $ name = $ arguments ['name ' ] ?? $ arguments [0 ] ?? null ;
208
- $ template = $ arguments ['template ' ] ?? $ arguments [1 ] ?? null ;
209
-
210
- if (null !== $ template || null !== $ name ) {
211
- if (null !== $ template && null !== $ name ) {
212
- $ templateFile = str_replace ('components/ ' , '' , $ template );
213
- $ metadata = $ this ->componentFactory ->metadataFor ($ name );
214
- } elseif (null !== $ name ) {
215
- $ templateFile = str_replace (': ' , '/ ' , "{$ name }.html.twig " );
216
- $ metadata = $ this ->componentFactory ->metadataFor ($ name );
217
- } else {
218
- $ templateFile = str_replace ('components/ ' , '' , $ template );
219
- $ metadata = $ this ->componentFactory ->metadataFor (str_replace ('.html.twig ' , '' , $ templateFile ));
220
- }
207
+ $ arguments = $ attribute ->getArguments ();
208
+
209
+ $ name = $ arguments ['name ' ] ?? $ arguments [0 ] ?? null ;
210
+ $ template = $ arguments ['template ' ] ?? $ arguments [1 ] ?? null ;
211
+
212
+ if (null !== $ template || null !== $ name ) {
213
+ if (null !== $ template && null !== $ name ) {
214
+ $ templateFile = str_replace ('components/ ' , '' , $ template );
215
+ $ metadata = $ this ->componentFactory ->metadataFor ($ name );
216
+ } elseif (null !== $ name ) {
217
+ $ templateFile = str_replace (': ' , '/ ' , "{$ name }.html.twig " );
218
+ $ metadata = $ this ->componentFactory ->metadataFor ($ name );
221
219
} else {
222
- $ templateFile = "{ $ reflectionClass -> getShortName ()} .html.twig " ;
223
- $ metadata = $ this ->componentFactory ->metadataFor ($ reflectionClass -> getShortName ( ));
220
+ $ templateFile = str_replace ( ' components/ ' , '' , $ template ) ;
221
+ $ metadata = $ this ->componentFactory ->metadataFor (str_replace ( ' .html.twig ' , '' , $ templateFile ));
224
222
}
223
+ } else {
224
+ $ templateFile = "{$ reflectionClass ->getShortName ()}.html.twig " ;
225
+ $ metadata = $ this ->componentFactory ->metadataFor ($ reflectionClass ->getShortName ());
226
+ }
225
227
226
- $ componentsWithClass [] = [
227
- 'name ' => $ metadata ->getName (),
228
- 'type ' => substr ( $ attributeName , strrpos ( $ attributeName , '\\ ' ) + 1 ) ,
229
- ];
228
+ $ componentsWithClass [] = [
229
+ 'name ' => $ metadata ->getName (),
230
+ 'type ' => null !== $ metadata -> get ( ' live ' ) ? ' AsLiveComponent ' : ' AsTwigComponent ' ,
231
+ ];
230
232
231
- if (($ key = array_search ($ templateFile , $ anonymousTemplatesComponents )) !== false ) {
232
- unset($ anonymousTemplatesComponents [$ key ]);
233
- }
233
+ if (($ key = array_search ($ templateFile , $ anonymousTemplatesComponents )) !== false ) {
234
+ unset($ anonymousTemplatesComponents [$ key ]);
234
235
}
235
236
}
236
237
}
@@ -245,12 +246,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
245
246
foreach ($ allComponents as $ component ) {
246
247
$ metadata = $ this ->componentFactory ->metadataFor ($ component ['name ' ]);
247
248
248
- $ dataToRender [] = [
249
- $ metadata ->getName (),
250
- $ metadata ->get ('class ' ) ?? 'Anonymous component ' ,
251
- $ metadata ->getTemplate (),
252
- $ component ['type ' ],
253
- ];
249
+ if (str_contains ($ metadata ->getTemplate (), $ componentsDir )) {
250
+ $ dataToRender [] = [
251
+ $ metadata ->getName (),
252
+ $ metadata ->get ('class ' ) ?? 'Anonymous component ' ,
253
+ $ metadata ->getTemplate (),
254
+ $ component ['type ' ],
255
+ ];
256
+ }
254
257
}
255
258
256
259
$ table = new Table ($ output );
0 commit comments