schmittjoh / JMSDiExtraBundle

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

How pass arguments without the need of override the constructor #244

Open rafrsr opened 8 years ago

rafrsr commented 8 years ago

It's possible pass some arguments to a constructor without the need of override the constructor method?

When create a service based on another class in some cases is needed pass some arguments to the constructor.

Now:

/**
 * @Service()
 * @Tag(name="form.type_guesser")
 */
class SerialsTypeGuesser extends DoctrineOrmTypeGuesser
{
    /**
     * @param ManagerRegistry $registry
     *
     * @InjectParams({
     *     "registry" = @Inject("doctrine")
     * })
     */
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry);
    }

Something like this would be very useful

Suggestion:

/**
 * @Service()
 * @Tag(name="form.type_guesser")
 * @Arguments({
 *        "registry" = @Inject("doctrine")
 *    })
 */
class SerialsTypeGuesser extends DoctrineOrmTypeGuesser
{

In the other hand, something similar to call a setter without the need of override the parent class method would be very helpful to.

NOW:

/**
 * @Service()
 */
class ServiceClass
{
   use ContainerAwareTrait;

   /**
     * @inheritDoc
     * @InjectParams({
     *     "container" = @Inject("service_container")
     * })
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

Suggestion:

/**
 * @Service()
*  @Call(name="setContainer", params= {
*         "container" = @Inject("service_container")
*     })
 */
class ServiceClass
{
   use ContainerAwareTrait;
GuilhemN commented 8 years ago

Well this makes new annotations to support for a very small benefit...

wodka commented 8 years ago

Hi, for both cases you could create a custom annotation -> similar to https://github.com/wodka/SonataAdminBundle/blob/jms-annotation/Annotation/Admin.php

it would also be possible to extend your trait and define the annotation there.

rafrsr commented 8 years ago

@Ener-Getick is a small benefit depending on use cases, for some pepole like me is a good feature.

@wodka, didn't know this annotation, I use sonata and each administration is dificult to implement with JMSDiExtraBundle. Thanks, in the other hand, I think that the above annotations can be like a good feature for future versions. Its not really a good practice the need of implements a custom annotation for some cases.

rafrsr commented 8 years ago

I thinks that if the target of JMSDiExtraBundle is to use Dependency Injection in annotations. Knowing this arguments and call are widely used features in service definitions when use .yml or other configuration formats.

wodka commented 8 years ago

could be a nice addition to: https://github.com/schmittjoh/JMSDiExtraBundle/pull/240