Skip to content

Conversation

@daylerees
Copy link
Contributor

This pull request allows for commands and handlers (whether in a queue, or command bus) to be separate as an alternative to self-handling jobs.

I have explained the purpose of this fix (and discussed it in detail) with Taylor, but I'll explain it here for the benefit of those reviewing.

Having the separation between commands and handlers makes the most sense in 'multi-app' environments that use a single queue consumer project. Consider the following:

APP1     APP2      APP3

    all fire queued jobs to:

          APP4
  (running queue:listen)

Consider the following job:

<?php

use Some\Other\Package\FooService;

class DoSomething
{
    protected $someId;

    public function __construct($someId)
    {
        $this->someId = $id;
    }

    public function handle(FooService $foo)
    {
        // Do something with foo service.
    }
}

Apps 1-3 might wish to fire this job, but to do so they will need to import the package for FooService using composer, even though it's never used from those projects.

By separating the command and handler (splitting into two classes, one a value object, the other with a handle method) and allowing the user to map commands to handlers we can fire the jobs from APP1-3 without the FooService requirement.

Mapping commands looks like the following:

Bus::map(CommandClassHere::class, HandlerClassHere::class);

Firing jobs should use the 'command':

$this->dispatch(CommandClassHere::class)

Stand alone handlers should inject their dependencies through the constructor, where existing self-handling jobs will inject their dependencies through the handle method.

This change should be backwards compatible and still allow for self-handling jobs to function as before.

Thanks for reading!

@daylerees
Copy link
Contributor Author

@GrahamCampbell want to explain where you think this might fall short? I checked the other comment but I'm not sure I follow. Can you link to some source? Cheers buddy.

@GrahamCampbell
Copy link
Collaborator

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