schmittjoh / JMSDiExtraBundle

Provides Advanced Dependency Injection Features for Symfony2
http://jmsyst.com/bundles/JMSDiExtraBundle
330 stars 130 forks source link

Use String as Parameter #196

Open ad3n opened 9 years ago

ad3n commented 9 years ago
services:
    app.manager.propinsi:
        class: AppBundle\Manager\PropinsiManager
        arguments:
            - @doctrine.orm.entity_manager
            - @app.cache.cache_key_generator
            - AppBundle\Entity\Propinsi

That is my service, i convert that service using DIExtraBundle and i do like it:

@InjectParams({"class"=@Inject("AppBundle\Entity\Propinsi")})

And got error, service appbundle\entity\propinsi is not exist

wodka commented 9 years ago

Won't work unless you move that value into a parameter.

to get this working something like https://github.com/schmittjoh/JMSDiExtraBundle/pull/207 would need to be merged.

wodka commented 9 years ago

I needed it for SonataAdminBundle.

why don't you create a custom annotation for your needs? It's quite easy and gives you more flexibility https://github.com/wodka/SonataAdminBundle/blob/jms-annotation/Annotation/Admin.php

wodka commented 8 years ago

@Ener-Getick perhaps we can change how argument injection works something like:

/**
 * @DI\Call({
 *     "service" = @DI\Argument("@foo"),
 *     "parameter" = @DI\Argument("%some.parameter%"),
 *     "string" = @DI\Argument("bar"),
 *     "array" = @DI\Argument({"first", "second","third"})
 * })
 */
function doSomething(FooInterface $service, $parameter, $string, array $array)
{}
GuilhemN commented 8 years ago

what do you think @schmittjoh ?

schmittjoh commented 8 years ago

Why not add this in the code directly?

function doSomething(FooInterface $service, $parameter, $string = "bar", array $array = array('abc', 'def'))
{}
wodka commented 8 years ago

Here you have a good example of why with some new features a default value does not help:

use JMS\DiExtraBundle\Annotation as DI;

/**
 * @DI\Service("api.first")
 * @DI\Service("api.second")
 */
class RemoteApi
{
    /**
     * @DI\Call(
     *     {
     *         "service" = @DI\Argument("@foo"),
     *         "host" = @DI\Argument("https://first")
     *     },
     *     services = {"api.first"}
     * )
     *
     * @DI\Call(
     *     {
     *         "service" = @DI\Argument("@foo"),
     *         "host" = @DI\Argument("https://second")
     *     },
     *     services = {"api.second"}
     * )
     */
    public function doSomething($service, $host)
    {}
}