0

I am using Messenger in Symfony to send request to create archive of documents

In controller action:

$bus->dispatch(
    new ArchiveRequest(
        \App\Archiving\SomeSpecialArchiveBuilder::class,
        $this->getRequestJsonContent()
    )
);

In ArchiveRequestHandler I want somehow to instantiate this special class and autowire services to it.

class ArchiveRequestHandler implements MessageHandlerInterface
{
    public function __invoke(
        ArchiveRequest $archiveRequest
    )
    {
        // How can I instantiate special implementation of ArchiveBuilderInterface here and autowire services from container?
    }
}
yivi
  • 42,438
  • 18
  • 116
  • 138
Thelambofgoat
  • 609
  • 1
  • 11
  • 24
  • why don't inject the instance directly in `ArchiveRequest`? – DonCallisto Oct 15 '21 at 09:58
  • If I try to typehint certain archive builder in action like SomeSpecialArchiveBuilder $builder and use like $bus->dispatch( new ArchiveRequest( $builder, $this->getRequestJsonContent() ) ); I get "exception":"[object] (Exception(code: 0): Serialization of 'Closure' is not allowed" – Thelambofgoat Oct 15 '21 at 10:16
  • I think I won't be able to serialize the $builder cause it's very complex – Thelambofgoat Oct 15 '21 at 10:19
  • 1
    If you want to instantiate the class within the handler, then you cannot use autowiring. You could use some sort of service locator, though. The handler should have the service locator injected, and get the service from there. – yivi Oct 15 '21 at 10:36

0 Answers0