Skip to content

Conversation

@StevenRenaux
Copy link
Contributor

@StevenRenaux StevenRenaux commented Oct 3, 2023

Related to #1301 I also suggest to add a global maker for both, Listener and Subscriber.

I worked with the based class MakeSubscriber and updated it to make this solution.

In the terminal you can run make:event and a question will be displayed like that to know which event class you want to create.

$ php bin/console make:event FooBar kernel.request


 What event class to generate(enter ? to see all types) [Subscriber]:
 > ?

Event Types Class
  * Subscriber
  * Listener

 What event class to generate(enter ? to see all types) [Subscriber]:
 > 

And if you write well what you want as FooBarSubscriber or FooBarListener and the event to listen, your file will be generated as Listener or Subscriber.

namespace App\EventListener;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

final class FooBarListener
{
    #[AsEventListener(event: KernelEvents::REQUEST)]
    public function onKernelRequest(RequestEvent $event): void
    {
        // ...
    }
}

@StevenRenaux StevenRenaux requested a review from Jean-Beru October 6, 2023 09:46
@OskarStark OskarStark added the Feature New Feature label Oct 9, 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 a cool idea 👍

if (method_exists($class, 'getCommandAlias')) {
$alias = $class::getCommandAlias();
$commandDefinition->addTag('console.command', ['command' => $alias, 'description' => 'Deprecated alias of "make:event"']);
}
Copy link
Member

Choose a reason for hiding this comment

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

Should/could we leverage the getCommandAliases() referenced above since that logic already exists?

Choose a reason for hiding this comment

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

+1, I found it a little bit weird to have methods like getCommandAliases whose existence leads to messages for very specific classes.

Maybe something like this?

if (method_exists($class, 'getDeprecatedCommandAliases')) {
    foreach ($class::getDeprecatedCommandAliases() as $alias) {
        $commandDefinition->addTag('console.command', ['command' => $alias, 'description' => \sprintf('Deprecated alias of "%s"', $class::getCommandName())]);
    }
}

@StevenRenaux
Copy link
Contributor Author

StevenRenaux commented Oct 11, 2023

@weaverryan Command renamed make:listener with listener as default type.

To resume:
If Listener or Subscriber was not found in the suffix

$ php bin/console make:listener foo


 What event class to generate(enter ? to see all types) [Listener]:
 > 

If Listener or Subscriber was found in the suffix we display the originals events to choose, as the legacy command did.

$ php bin/console make:listener FooListener

 Suggested Events:
 * Generator::class ()
 * Symfony\Component\Mailer\Event\MessageEvent (Symfony\Component\Mailer\Event\MessageEvent)
 * Symfony\UX\TwigComponent\Event\PostRenderEvent (Symfony\UX\TwigComponent\Event\PostRenderEvent)
 * Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent (Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent)
/* ...*/

  What event do you want to listen to?:
 > 

And if no name was filled, a question is displayed:

$ php bin/console make:listener

 Choose a class name for your event listener or subscriber (e.g. ExceptionListener or ExceptionSubscriber):
 > 

I didn't really understand your comment, if I was right to notify a deprecation about make:subscriber:

Hmm, ok fair enough. We are free to change what these commands due between minor releases. So let's give them the better experience.

$ php bin/console make:subscriber FooSubscriber

                                                                                                                        
 [WARNING] The "make:subscriber" command is deprecated, use "make:listener" instead.                                    
                                                                                                                        


 Suggested Events:
 * Generator::class ()
 * Symfony\Component\Mailer\Event\MessageEvent (Symfony\Component\Mailer\Event\MessageEvent)
 * Symfony\UX\TwigComponent\Event\PostRenderEvent (Symfony\UX\TwigComponent\Event\PostRenderEvent)
 * Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent (Symfony\UX\TwigComponent\Event
/*...*/

  What event do you want to listen to?:
 > 

If it good for you, maybe we would need to create a new Issue for the next major version to clean all of this deprecation as:

PS: not related to this PR but we also have deprecated code about make:functinal-test and make:unit-test .

@StevenRenaux StevenRenaux changed the title [make:event] Add global command for listener and subscriber [make:listener] Add global command for listener and subscriber Oct 11, 2023
@weaverryan
Copy link
Member

I didn't really understand your comment, if I was right to notify a deprecation about make:subscriber:

It looks correct what you did 👍

If it good for you, maybe we would need to create a new Issue for the next major version to clean all of this deprecation as:

Yes - though no issue needed, this will naturally happen. I don't know when we might release a new major version, but the main thing we will do when that happens is hunt down all of the deprecated code paths and remove them. Assuming we've done a good job marking this with @deprecated or trigger_deprecation(), they should be easy to find.

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.

Don't forget to check the php-cs CI job for some coding standards stuff :)

@StevenRenaux
Copy link
Contributor Author

Need help @weaverryan , I don't understand why it failed in the appveyor. It only failed when Listener are generated. But in local or during the other tests in the CI it work.
Example on local, on the left side the file generated and in the right the one compare with. I don't understant why we loose an indention during appveyor test.

image

By the way I updated the deprecated messages and finally removed it from MakerExtension.php and added it in the maker.xml

@weaverryan weaverryan force-pushed the add-global-maker-listener-subscriber branch from 0307eac to 1bda950 Compare October 16, 2023 18:05
@weaverryan
Copy link
Member

Thank you Steven!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature New Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants