voryx / ThruwayBundle

Bundle for building Real-time Apps in Symfony
98 stars 47 forks source link

Example without container #94

Closed tacman closed 5 years ago

tacman commented 5 years ago

Symfony 4 strongly discourages passing the container. I'm trying to autowire the client in a Symfony 4 command, but can't quite figure out how to do it. I've tried

use Thruway\Peer\ClientInterface;
    public function __construct(EntityManagerInterface $entityManager,
                                ClientInterface $thruwayClient,
                                $name = null)
    {
}

Got an error about not being able to autowire,

    Cannot autowire service "App\Command\DownloadCommand": argument "$thruwayClient" of method "__construct()" references interface "Thruway\Peer\ClientInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "voryx.thruway.client", "voryx.thruway.authentication.manager", "voryx.thruway.wamp.cra.auth.client", "voryx.thruway.topic.state.handler".                              

so I added an alias in services.yaml:

    Thruway\Peer\ClientInterface:
        alias: voryx.thruway.client

But I think I'm passing in the Manager, not the Client, since there is no publish command. Is it possible to autowire the thruway services, to avoid using the container?

tacman commented 5 years ago

Figured it out:

in services.yaml:

    Voryx\ThruwayBundle\Client\ClientManager:
        alias: thruway.client

The constructor:

use Voryx\ThruwayBundle\Client\ClientManager;

    public function __construct(EntityManagerInterface $entityManager,
                                ClientManager $thruwayClient,..