JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

How pass arguments without the need of override the constructor

Open rafrsr opened this issue 9 years ago • 5 comments

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;

rafrsr avatar Feb 25 '16 14:02 rafrsr

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

GuilhemN avatar Feb 25 '16 20:02 GuilhemN

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.

wodka avatar Feb 29 '16 08:02 wodka

@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 avatar Feb 29 '16 13:02 rafrsr

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.

rafrsr avatar Feb 29 '16 13:02 rafrsr

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

wodka avatar Feb 29 '16 14:02 wodka