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

feat: Add possibility to handle Event (Register/Listener) with AsListener #221

Merged
merged 2 commits into from
Jan 10, 2024
Merged

feat: Add possibility to handle Event (Register/Listener) with AsListener #221

merged 2 commits into from
Jan 10, 2024

Conversation

TheoD02
Copy link
Contributor

@TheoD02 TheoD02 commented Jan 9, 2024

After I made a big mistake manipulating the PR: #218, here's the continuation, I'm sorry for that :/

I fixed most feedbacks of the PR

Event Handling and Custom User Events

Description

This pull request introduces a comprehensive feature for event handling and custom user events. It includes the implementation of an event listener system based on Symfony component and documentation to guide developers on creating, dispatching, and handling events.

Changes Made

Event Listener System

  1. Implemented Event Listener Function:

    #[AsListener(event: AfterApplicationInitializationEvent::class, priority: 1)]
    function after_application_init_listener(AfterApplicationInitializationEvent $event): void
    {
        // Custom logic to execute after application initialization
    }

Event Classes

  1. Added Event Classes:

    • Introduced event classes to represent different points in the application lifecycle.
    • Documented each event class (AfterApplicationInitializationEvent, AfterConfigureTaskEvent, AfterExecuteTaskEvent, BeforeConfigureTaskEvent, BeforeExecuteTaskEvent) with their properties and usage.
    // Example: AfterApplicationInitializationEvent
    class AfterApplicationInitializationEvent
    {
        public function __construct(
            private readonly Application $application,
            private array $tasks,
        ) {
        }
    }

Allow Custom User Events

  1. Custom User Event Class (MyEvent):

    • Created a custom user event class MyEvent with a public property $name.
    class MyEvent
    {
        public function __construct(
            public string $name,
        ) {
        }
    }
  2. Implemented Task to Dispatch Custom User Event:

    • A task function my_task_that_dispatches_an_event to dispatch an instance of the MyEvent using the dispatch_event function.
    #[AsTask(description: 'Task that dispatches an event')]
    function my_task_that_dispatches_an_event(): void
    {
        $name = 'John Doe';
        $event = new MyEvent($name);
        dispatch_event(eventName: MyEvent::class, event: $event);
    }
  3. Added Event Listener Function (my_event_listener):

    • Created an event listener function my_event_listener to handle the MyEvent events.
    #[AsListener(event: MyEvent::class, priority: 1)]
    function my_event_listener(MyEvent $event): void
    {
        // Custom logic to handle the MyEvent
        $name = $event->name;
        echo "Received MyEvent with name: $name\n";
    }

Related Issues

#217

bin/generate-tests.php Show resolved Hide resolved
doc/15-event-listener.md Outdated Show resolved Hide resolved
src/EventManager.php Outdated Show resolved Hide resolved
src/Console/Command/TaskCommand.php Show resolved Hide resolved
src/Console/Application.php Show resolved Hide resolved
doc/15-event-listener.md Outdated Show resolved Hide resolved
doc/15-event-listener.md Outdated Show resolved Hide resolved
doc/15-event-listener.md Outdated Show resolved Hide resolved
@TheoD02
Copy link
Contributor Author

TheoD02 commented Jan 10, 2024

I have fixed all the feedbacks :) You can check it 😎

@lyrixx
Copy link
Member

lyrixx commented Jan 10, 2024

Thanks for the hard work. I'll finish your PR, and push here. So don't push anything from now on :)

@TheoD02
Copy link
Contributor Author

TheoD02 commented Jan 10, 2024

Thanks for the hard work. I'll finish your PR, and push here. So don't push anything from now on :)

No worries with pleasure :)

Oh, I hadn't seen the message when I pushed the CI fix and rebase!

@lyrixx
Copy link
Member

lyrixx commented Jan 10, 2024

Awesome work! I changed few things, but is was really minor. Thanks for your hard work! This feature will be very useful!!!

It can be merged 👍🏼

Copy link
Member

@pyrech pyrech left a comment

Choose a reason for hiding this comment

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

Just a small nitpick but LGTM

src/Console/Application.php Outdated Show resolved Hide resolved
src/Console/Application.php Outdated Show resolved Hide resolved
src/Console/Application.php Outdated Show resolved Hide resolved
src/Console/Application.php Outdated Show resolved Hide resolved
src/GlobalHelper.php Outdated Show resolved Hide resolved
src/GlobalHelper.php Outdated Show resolved Hide resolved
src/Console/Command/TaskCommand.php Outdated Show resolved Hide resolved
@lyrixx lyrixx merged commit 0c87886 into jolicode:main Jan 10, 2024
3 checks passed
@lyrixx
Copy link
Member

lyrixx commented Jan 10, 2024

Thanks @TheoD02 !

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.

3 participants