JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

Injecting a collection (array) in constuctor

Open smurfy opened this issue 12 years ago • 9 comments

Hi,

i'm trying injecting an array via InjectParams but cant get it to work (if there is a way its not described in the docs)

Here is what i'm trying to do

/**
 * @param array $options
 * @InjectParams({
 *     "options" = {@Inject("%something%"), @Inject("%something else%")}
 * })
public function __construct(array $options)

This will throw:

[Doctrine\Common\Annotations\AnnotationException]
[Type Error] Attribute "value" of @InjectParams declared on method MyClass::__construct() expects either a(n) JMS\DiExtraBundle\Annotation\Inject, or an array of JMS\DiExtraBundle\Annotation\Injects, but got array.

I tried different syntax variations too but always get an error.

smurfy avatar Jun 26 '12 10:06 smurfy

This is not supported atm.

schmittjoh avatar Jul 09 '12 14:07 schmittjoh

Would be useful I guess. I am extending the FOSUser Mailer class and I have to inject this array:

<argument type="collection">
    <argument key="template" type="collection">
        <argument key="confirmation">%fos_user.registration.confirmation.template%</argument>
        <argument key="resetting">%fos_user.resetting.email.template%</argument>
    </argument>
    <argument key="from_email" type="collection">
        <argument key="confirmation">%fos_user.registration.confirmation.from_email%</argument>
        <argument key="resetting">%fos_user.resetting.email.from_email%</argument>
    </argument>
</argument>

ruudk avatar Aug 17 '12 13:08 ruudk

+1

I would need this also for some adaption of FOS UserBundle

KeKs0r avatar Jul 10 '13 20:07 KeKs0r

Would this work?

config.yml

parameters:
    my_array:
        - first
        - second

Your class:

/**
 * @param array $options
 * @InjectParams({
 *     "options" = @Inject("%my_array%")
 * })
public function __construct(array $options)

ruudk avatar Jul 11 '13 08:07 ruudk

I guess that will work, but the idea behind all is to use annotation rather than working with a yml file :)

smurfy avatar Jul 11 '13 12:07 smurfy

+1 I'm also need it for FOSUserBundle Authentication Success Handler

khal3d avatar Nov 13 '14 14:11 khal3d

+1 "I guess that will work, but the idea behind all is to use annotation rather than working with a yml file"

imanzuk avatar Jul 14 '15 15:07 imanzuk

+1

6be709c0 avatar Aug 11 '15 08:08 6be709c0

  • 1

The-Don-Himself avatar May 10 '16 10:05 The-Don-Himself