auraphp / Aura.Di

Dependency Injection System
MIT License
349 stars 63 forks source link

Add a new LazyArray type for injecting arrays of callables #138

Closed andrewshell closed 8 years ago

andrewshell commented 8 years ago

I'm working with Symfony Forms and there is currently no way to assign via setter injection multiple times (like calling addExtension three times).

However, they do have an addExtensions method that takes an array of extensions. However, when I assign an array to it with several lazy objects, the lazy objects do not get called to create the real objects before being passed into the method.

I created a LazyArray class that is a flat array of values and when invoked I go through the array and if it's a LazyInstance I call it and replace it with the real value. Then I just return the array of real instances.

If you have a nested array you can nest LazyArrays and it will resolve correctly.

harikt commented 8 years ago

Thank you @andrewshell . It looks good to me for there is no alternative way I can suggest. We can wait for @pmjones thoughts .

UPDATE : Could you please add docs also for the same ?

Looks you just pushed the docs.

pmjones commented 8 years ago

I'll test locally, and likely remove the is_array() check. Thanks!