Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug:component command #1088

Merged
merged 1 commit into from
Sep 22, 2023

Conversation

StevenRenaux
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
Tickets #102
License MIT

Related to the Future Ideas section of #102
I added a debug:component command.

Debugging component

The debug:component command lists all your application components (TwigComponent and LiveComponent) who lives in templates/components directory:

$ php bin/console debug:component

+---------------+-----------------------------+------------------------------------+
| Component     | Class                       | Template                           |
+---------------+-----------------------------+------------------------------------+
| Coucou        | App\Components\Alert        | components/Coucou.html.twig        |
| RandomNumber  | App\Components\RandomNumber | components/RandomNumber.html.twig  |
| Test          | App\Components\foo\Test     | components/foo/Test.html.twig      |
| Button        | Anonymous component         | components/Button.html.twig        |
| foo:Anonymous | Anonymous component         | components/foo/Anonymous.html.twig |
+---------------+-----------------------------+------------------------------------+

Pass the name of some component to this argument to print the component details:

$ php bin/console debug:component Test

+---------------------------------------------------+-------------------------------+
| Property                                          | Value                         |
+---------------------------------------------------+-------------------------------+
| Component                                         | Test                          |
| Class                                             | App\Components\foo\Test       |
| Template                                          | components/foo/Test.html.twig |
| Properties (type / name / default value if exist) | string $type = success        |
|                                                   | string $message               |
+---------------------------------------------------+-------------------------------+

To get the details about an anonymous component who is rendered with sub directory, just add it to the name:

    <div>
        <twig:foo:Anonymous label="Click Me!" :disabled="true" />
    </div>
$ php bin/console debug:component foo:Anonymous

+--------------------------------------------+------------------------------------+
| Property                                   | Value                              |
+--------------------------------------------+------------------------------------+
| Component                                  | foo:Anonymous                      |
| Class                                      | Anonymous component                |
| Template                                   | components/foo/Anonymous.html.twig |
| Properties (name / default value if exist) | label                              |
|                                            | name = toto                        |
+--------------------------------------------+------------------------------------+

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great work @StevenRenaux!

Couple things:

  1. In the listing of components, could you add a Live? (yes/no) column?
  2. When showing details about a component, can you indicate whether it is live or not?
  3. Also in the details, can you list the live props?

@kbond
Copy link
Member

kbond commented Sep 1, 2023

  1. Could you also add a section to the docs (twig & live)

@StevenRenaux
Copy link
Contributor Author

StevenRenaux commented Sep 1, 2023

@kbond I will try to work on your suggestions

  1. In the listing of components, could you add a Live?
    I suggest to add a column Type directly with AsTwigComponent, AsLiveComponent or empty if Anonymous.
    I can get it with the ReflectionClass and the method getAttributes.

  2. When showing details about a component, can you indicate whether it is live or not?
    As I suggest for the 1. I can add a property Type

  3. Also in the details, can you list the live props?
    I can try to divide in two sections the Props part like Properties (name / default value if exist) and after Live Properties

  4. Could you also add a section to the docs (twig & live)
    Yeah it will be a TODO ;)

@kbond
Copy link
Member

kbond commented Sep 1, 2023

Sounds good!

I can get it with the ReflectionClass and the method getAttributes.

I think this is available in the metadata (has a live key) - this would be a more robust way to check.

@StevenRenaux
Copy link
Contributor Author

StevenRenaux commented Sep 4, 2023

Hi @kbond

I updated the code with your suggestions.
To know if it's a LiveComponent I used $metadata->get('live') as you suggested to get one asked component.
But to get the list of all of them I was originaly in a loop after a $reflectionClass->getAttributes() so I took the position to just make a check if the attribute depend on AsTwigComponent::class or AsLiveComponent::class. Let me know if I also need to update this part 😉

For one component :

$ php bin/console debug:component RandomNumber
+---------------------------------------------------+-----------------------------------+
| Property                                          | Value                             |
+---------------------------------------------------+-----------------------------------+
| Component                                         | RandomNumber                      |
| Type                                              | AsLiveComponent                   |
| Class                                             | App\Components\RandomNumber       |
| Template                                          | components/RandomNumber.html.twig |
| Properties (type / name / default value if exist) | string $name = toto               |
|                                                   | string $type = test               |
| Live Properties                                   | int $max = 1000                   |
|                                                   | int $min = 10                     |
+---------------------------------------------------+-----------------------------------+

And for all :

$ php bin/console debug:component
+---------------+-----------------------------+------------------------------------+-----------------+
| Component     | Class                       | Template                           | Type            |
+---------------+-----------------------------+------------------------------------+-----------------+
| Coucou        | App\Components\Alert        | components/Coucou.html.twig        | AsTwigComponent |
| RandomNumber  | App\Components\RandomNumber | components/RandomNumber.html.twig  | AsLiveComponent |
| Test          | App\Components\foo\Test     | components/foo/Test.html.twig      | AsTwigComponent |
| Button        | Anonymous component         | components/Button.html.twig        |                 |
| foo:Anonymous | Anonymous component         | components/foo/Anonymous.html.twig |                 |
+---------------+-----------------------------+------------------------------------+-----------------+

Copy link
Collaborator

@WebMamba WebMamba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoooo thanks a lot for it @StevenRenaux! 🧡

src/TwigComponent/src/Command/ComponentDebugCommand.php Outdated Show resolved Hide resolved
src/TwigComponent/src/Command/ComponentDebugCommand.php Outdated Show resolved Hide resolved
@@ -34,6 +34,7 @@
"twig/twig": "^2.14.7|^3.0.4"
},
"require-dev": {
"symfony/console": "^5.4|^6.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the right place to add this command? Maybe in the StimulusBundle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't know either and it's the reason why i putted here

$class = $metadata->get('class');
$allProperties = [];

if ($class) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be useful to display:

  • properties with the ExposeInTemplate attribute, and if the name is set show the name.
  • if the component has a mount, premount, and postmount hooks

And for LiveConponents:

  • as @kbond said show if the components is live
  • show live props
  • show live actions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @WebMamba and @kbond, an update with Events and LiveAction, it could be look like this for a Live component:

 $ php bin/console debug:component RandomNumber
+--------------------+-----------------------------------+
| Property           | Value                             |
+--------------------+-----------------------------------+
| Component          | RandomNumber                      |
| Type               | AsLiveComponent                   |
| Class              | App\Components\RandomNumber       |
| Template           | components/RandomNumber.html.twig |
| Properties         | string $name = toto               |
|                    | string $type = test               |
| Live Properties    | int $max = 1000                   |
|                    | int $min = 10                     |
| Events             | Mount                             |
|                    | PreMount                          |
| LiveAction Methods | resetMax                          |
+--------------------+-----------------------------------+

And for a TwigComponent

$ php bin/console debug:component Coucou
+------------+-----------------------------+
| Property   | Value                       |
+------------+-----------------------------+
| Component  | Coucou                      |
| Type       | AsTwigComponent             |
| Class      | App\Components\Alert        |
| Template   | components/Coucou.html.twig |
| Properties | string $type = success      |
|            | string $message             |
| Events     | Mount                       |
|            | PreMount                    |
+------------+-----------------------------+

But for the #[ExposeInTemplate] what could I render for you?
The name of the property in the class or the one one displayed in the template? And with the getter or not?

@StevenRenaux StevenRenaux force-pushed the add-component-debug-command branch 2 times, most recently from 31fc8b8 to dd1121f Compare September 7, 2023 07:48
@kbond
Copy link
Member

kbond commented Sep 7, 2023

But to get the list of all of them I was originaly in a loop after a $reflectionClass->getAttributes() so I took the position to just make a check if the attribute depend on AsTwigComponent::class or AsLiveComponent::class. Let me know if I also need to update this part 😉

The issue with using these attributes is components can be registered without (ie 3rd party bundle registering components purely with a tag).

@StevenRenaux
Copy link
Contributor Author

But to get the list of all of them I was originaly in a loop after a $reflectionClass->getAttributes() so I took the position to just make a check if the attribute depend on AsTwigComponent::class or AsLiveComponent::class. Let me know if I also need to update this part 😉

The issue with using these attributes is components can be registered without (ie 3rd party bundle registering components purely with a tag).

Updated as you submitted ;)

@StevenRenaux
Copy link
Contributor Author

I was thinking about this part too... What about a command option to customize (with /components being the default)?

If you want components with a specific dir:


php bin/console debug:twig-component --dir=bar
+----------------+-------------------------------+------------------------------+-----------------+
| Component      | Class                         | Template                     | Type            |
+----------------+-------------------------------+------------------------------+-----------------+
| OtherDirectory | App\Components\OtherDirectory | bar/OtherDirectory.html.twig | AsTwigComponent |
+----------------+-------------------------------+------------------------------+-----------------+

@OskarStark OskarStark changed the title Add debug:component command Add debug:component command Sep 12, 2023
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool! Though, tbh, it makes me wish we had more centralized ways to get metadata about a component. It feels like we are reimplementing a lot of logic - like for finding what public properties will be made into variables, etc.

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with Ryan there's a lot of duplication but this can be refactored to some kind of metadata registry.

We should probably do this sooner, rather than later so things don't get out of sync. For now, it's out of the scope of this PR.

@WebMamba
Copy link
Collaborator

Yes I was thinking the same thing when I read for the first time this PR. Thanks @StevenRenaux for digging into all of this

@weaverryan
Copy link
Member

Ok then. But I'll be watching for some PR's to centralize some of that stuff ;).

@StevenRenaux Can you fix the conflict? I can usually do that with our merge tool, but the rebase is looking oddly ugly.

@StevenRenaux
Copy link
Contributor Author

@weaverryan Conflict resolution done and let me know when it will be done and I will update this command 😉

@weaverryan
Copy link
Member

Thank you for your hard work Steven and congrats on this great PR. I forgot that I hadn't merged it yet, and I was needing it yesterday and trying to use it. So today my work will be better ;)

@weaverryan weaverryan merged commit 44d5d6b into symfony:2.x Sep 22, 2023
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants