1616use Symfony \Component \Console \Helper \Table ;
1717use Symfony \Component \Console \Input \InputArgument ;
1818use Symfony \Component \Console \Input \InputInterface ;
19+ use Symfony \Component \Console \Input \InputOption ;
1920use Symfony \Component \Console \Output \OutputInterface ;
2021use Symfony \Component \Console \Style \SymfonyStyle ;
2122use Symfony \Component \Finder \Finder ;
22- use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
2323use Symfony \UX \LiveComponent \Attribute \LiveAction ;
2424use Symfony \UX \LiveComponent \Attribute \LiveProp ;
25- use Symfony \UX \TwigComponent \Attribute \AsTwigComponent ;
2625use Symfony \UX \TwigComponent \Attribute \PostMount ;
2726use Symfony \UX \TwigComponent \Attribute \PreMount ;
2827use Symfony \UX \TwigComponent \ComponentFactory ;
2928use Symfony \UX \TwigComponent \Twig \PropsNode ;
3029use Twig \Environment ;
3130
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 ' )]
3332class ComponentDebugCommand extends Command
3433{
3534 public function __construct (private string $ twigTemplatesPath , private ComponentFactory $ componentFactory , private Environment $ twigEnvironment , private iterable $ components )
@@ -42,11 +41,16 @@ protected function configure(): void
4241 $ this
4342 ->setDefinition ([
4443 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 ' ),
4545 ])
4646 ->setHelp (<<<'EOF'
4747 The <info>%command.name%</info> display current components and them usages for an application:
4848
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>
5054
5155 EOF
5256 )
@@ -57,6 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5761 {
5862 $ io = new SymfonyStyle ($ input , $ output );
5963 $ name = $ input ->getArgument ('name ' );
64+ $ componentsDir = $ input ->getOption ('dir ' );
6065
6166 if (null !== $ name ) {
6267 try {
@@ -199,38 +204,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int
199204 $ attributes = $ reflectionClass ->getAttributes ();
200205
201206 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 );
221219 } 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 ));
224222 }
223+ } else {
224+ $ templateFile = "{$ reflectionClass ->getShortName ()}.html.twig " ;
225+ $ metadata = $ this ->componentFactory ->metadataFor ($ reflectionClass ->getShortName ());
226+ }
225227
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+ ];
230232
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 ]);
234235 }
235236 }
236237 }
@@ -245,12 +246,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
245246 foreach ($ allComponents as $ component ) {
246247 $ metadata = $ this ->componentFactory ->metadataFor ($ component ['name ' ]);
247248
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+ }
254257 }
255258
256259 $ table = new Table ($ output );
0 commit comments