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

[Messenger] Restore message handlers laziness #29558

Merged
merged 1 commit into from Dec 13, 2018

Conversation

Projects
None yet
4 participants
@chalasr
Copy link
Member

chalasr commented Dec 10, 2018

Q A
Branch? 4.2
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

The HandlersLocator introduced in 4.2 currently needs to instantiate all message handlers of the bus in order to be wired, while they were lazily loaded in 4.1 via ContainerHandlerLocator (removed).

Example:

framework:
    messenger:
        buses:
            messenger.bus.command: ~
services:
    App\Messenger\:
        resource: '../src/Messenger/*Handler.php'
        tags:
            - { name: messenger.message_handler, bus: messenger.bus.command }
namespace App\Messenger;

class BarCommand {}
class BarCommandHandler { public function __invoke(BarCommand $command): void {} }

class FooCommand {}
class FooCommandHandler { public function __invoke(FooCommand $command): void {} }

(Dumped HandleMessageMiddleware factory) Before:

return $this->privates['messenger.bus.command.middleware.handle_message'] = new \Symfony\Component\Messenger\Middleware\HandleMessageMiddleware(new \Symfony\Component\Messenger\Handler\HandlersLocator(array(
    'App\\Messenger\\BarCommand' => \current(array(0 => array(0 => new \App\Messenger\BarCommandHandler()))), 
    'App\\Messenger\\FooCommand' => \current(array(0 => array(0 => new \App\Messenger\FooCommandHandler())))
)));

After:

return $this->privates['messenger.bus.command.middleware.handle_message'] = new \Symfony\Component\Messenger\Middleware\HandleMessageMiddleware(new \Symfony\Component\Messenger\Handler\HandlersLocator(array(
    'App\\Messenger\\BarCommand' => new RewindableGenerator(function () {
        yield 0 => ($this->privates['App\Messenger\BarCommandHandler'] ?? ($this->privates['App\Messenger\BarCommandHandler'] = new \App\Messenger\BarCommandHandler()));
    }, 1), 
    'App\\Messenger\\FooCommand' => new RewindableGenerator(function () {
        yield 0 => ($this->privates['App\Messenger\FooCommandHandler'] ?? ($this->privates['App\Messenger\FooCommandHandler'] = new \App\Messenger\FooCommandHandler()));
    }, 1)
)));

@chalasr chalasr added this to the 4.2 milestone Dec 10, 2018

@chalasr chalasr force-pushed the chalasr:lazy-handler-map branch from 0acdfd6 to 879d73e Dec 10, 2018

@nicolas-grekas
Copy link
Member

nicolas-grekas left a comment

(SendersLocator is fine?)

@fabpot fabpot changed the title [Messenger] Restaure message handlers laziness [Messenger] Restore message handlers laziness Dec 11, 2018

@chalasr chalasr force-pushed the chalasr:lazy-handler-map branch 2 times, most recently from 88ef942 to 1d7455b Dec 11, 2018

@chalasr

This comment has been minimized.

Copy link
Member

chalasr commented Dec 11, 2018

@nicolas-grekas it looked fine, but updated for consistency and simplicity.

@chalasr chalasr force-pushed the chalasr:lazy-handler-map branch from 1d7455b to c608e73 Dec 11, 2018

@nicolas-grekas

This comment has been minimized.

Copy link
Member

nicolas-grekas commented Dec 13, 2018

Thank you @chalasr.

@nicolas-grekas nicolas-grekas merged commit c608e73 into symfony:4.2 Dec 13, 2018

2 of 3 checks passed

continuous-integration/appveyor/pr AppVeyor build failed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
fabbot.io Your code looks good.
Details

nicolas-grekas added a commit that referenced this pull request Dec 13, 2018

bug #29558 [Messenger] Restore message handlers laziness (chalasr)
This PR was merged into the 4.2 branch.

Discussion
----------

[Messenger] Restore message handlers laziness

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

The `HandlersLocator` introduced in 4.2 currently needs to instantiate all message handlers of the bus in order to be wired, while they were lazily loaded in 4.1 via ContainerHandlerLocator (removed).

Example:
```yaml
framework:
    messenger:
        buses:
            messenger.bus.command: ~
services:
    App\Messenger\:
        resource: '../src/Messenger/*Handler.php'
        tags:
            - { name: messenger.message_handler, bus: messenger.bus.command }
```

```php
namespace App\Messenger;

class BarCommand {}
class BarCommandHandler { public function __invoke(BarCommand $command): void {} }

class FooCommand {}
class FooCommandHandler { public function __invoke(FooCommand $command): void {} }
```
(Dumped `HandleMessageMiddleware` factory) Before:

```php
return $this->privates['messenger.bus.command.middleware.handle_message'] = new \Symfony\Component\Messenger\Middleware\HandleMessageMiddleware(new \Symfony\Component\Messenger\Handler\HandlersLocator(array(
    'App\\Messenger\\BarCommand' => \current(array(0 => array(0 => new \App\Messenger\BarCommandHandler()))),
    'App\\Messenger\\FooCommand' => \current(array(0 => array(0 => new \App\Messenger\FooCommandHandler())))
)));
```

After:
```php
return $this->privates['messenger.bus.command.middleware.handle_message'] = new \Symfony\Component\Messenger\Middleware\HandleMessageMiddleware(new \Symfony\Component\Messenger\Handler\HandlersLocator(array(
    'App\\Messenger\\BarCommand' => new RewindableGenerator(function () {
        yield 0 => ($this->privates['App\Messenger\BarCommandHandler'] ?? ($this->privates['App\Messenger\BarCommandHandler'] = new \App\Messenger\BarCommandHandler()));
    }, 1),
    'App\\Messenger\\FooCommand' => new RewindableGenerator(function () {
        yield 0 => ($this->privates['App\Messenger\FooCommandHandler'] ?? ($this->privates['App\Messenger\FooCommandHandler'] = new \App\Messenger\FooCommandHandler()));
    }, 1)
)));
```

Commits
-------

c608e73 [Messenger] Restore message handlers laziness

@chalasr chalasr deleted the chalasr:lazy-handler-map branch Dec 13, 2018

@fabpot fabpot referenced this pull request Jan 6, 2019

Merged

Release v4.2.2 #29803

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