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

Can't define more than one service for a class #232

Open
johnpancoast opened this issue Jan 15, 2016 · 10 comments
Open

Can't define more than one service for a class #232

johnpancoast opened this issue Jan 15, 2016 · 10 comments

Comments

@johnpancoast
Copy link

Using this bundle how can I define 2 services with different BarInterface implementations in this example?

class Foo
{
    public function __construct(BarInterface $bar) { … }
}
@wodka
Copy link
Contributor

wodka commented Feb 10, 2016

can you explain how you try to add the service?

each implementation should look like this:

use JMS\DiExtraBundle\Annotation as DI;

/**
 * @DI\Service("impl1")
 */
class HighBar implements BarInterface
{}
use JMS\DiExtraBundle\Annotation as DI;

/**
 * @DI\Service("impl2")
 */
class LowBar implements BarInterface
{}

@johnpancoast
Copy link
Author

Yea, that's how'd you'd define different implementations/services for BarInterface.

So now let's say I want to define 2 Foo services, one that accepts your HighBar service and another that gets LowBar. This is not too uncommon and I can with Symfony but I don't see a way (yet) to do it with this lib. It expects that the arguments you inject are for the one and only service you can create for the class.

Ideally we'd be able to create more than one service for a class. We'd just need a way to specify which service each argument is for.

@wodka
Copy link
Contributor

wodka commented Feb 11, 2016

ah :)

this is still quite easy - http://jmsyst.com/bundles/JMSDiExtraBundle/master/usage#constructor-setter-injection

use JMS\DiExtraBundle\Annotation as DI;

class Controller
{
    private $impl1;
    private $impl2;

    /**
     * @DI\InjectParams({
     *     "impl1" = @DI\Inject("impl1"),
     *     "impl2" = @DI\Inject("impl2")
     * })
     */
    public function __construct($impl1, $impl2)
    {
        $this->impl1 = $impl1;
        $this->impl2 = $impl2;
    }
    // ... some actions
}

@GuilhemN
Copy link
Collaborator

I guess we can close this issue?

@johnpancoast
Copy link
Author

You're just injecting two dependencies into one service. I'm saying I don't think you can define more than one service for a class.

In Symfony I can do this for example

services:
    bar_service_high:
        class: HighBar
    bar_service_low:
        class: LowBar

    foo_service_one:
        class: Foo
        arguments: [@bar_service_high]
    foo_service_two
        class: Foo
        arguments: [@bar_service_low]

I do not see a way to duplicate that behavior in this lib.

@GuilhemN
Copy link
Collaborator

@johnpancoast I see what you mean, this is actually not supported by this lib and we need to update the Metadata management to support it.
Feel free to create a PR, I don't think i'll have the time to work on it.

@johnpancoast
Copy link
Author

Cool! I'll try but I'm strapped for time too. I just wanted to make sure I wasn't missing some functionality I wasn't aware of.

wodka added a commit to wodka/JMSDiExtraBundle that referenced this issue Feb 14, 2016
wodka added a commit to wodka/JMSDiExtraBundle that referenced this issue Feb 14, 2016
@wodka
Copy link
Contributor

wodka commented Feb 14, 2016

I just tried to implement it, we just need a nice way to inject data for each service :)

@johnpancoast
Copy link
Author

Yup. A way to group them by name would work although i haven't looked at this code yet. I'll open it up today.

wodka added a commit to wodka/JMSDiExtraBundle that referenced this issue Feb 15, 2016
@johnpancoast
Copy link
Author

@wodka, awesome. Looks like you're already on this. Let me know if I can help.

wodka added a commit to wodka/JMSDiExtraBundle that referenced this issue Feb 16, 2016
add deprecation warnings
add tests
fix schmittjoh#232
wodka added a commit to wodka/JMSDiExtraBundle that referenced this issue Feb 16, 2016
add deprecation warnings
add tests
fix schmittjoh#232
wodka added a commit to wodka/JMSDiExtraBundle that referenced this issue Feb 16, 2016
add deprecation warnings
add tests
fix schmittjoh#232
wodka added a commit to wodka/JMSDiExtraBundle that referenced this issue Feb 16, 2016
add deprecation warnings
add services support to AfterSetup
add tests
fix schmittjoh#232
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

No branches or pull requests

3 participants