[fw-core] DI Confusion

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[fw-core] DI Confusion

JamesWilliams
Hi All,

 

(this is my first time using a mailing list.. so if mess it up.. Sorry!)

 

I've decided to take the plunge into the zend framework 2.. I have some
experience of ZF1 from developing magento extensions.

I did have a little go with ZF1 but decided to wait till ZF2 came out before
I started.

Anyways.. I've been really struggling to understand how the ZF2 DI works.
I've been playing about with it for a few days now and am starting to get to
grips with it (a lot of my problem was always looking for a setFoo function
in every class.. Not realizing DI seems to automagically know if you should
be passing the info to the constructor or a setFoo type function)

The issue I've been stuck on most of yesterday / today has been the below:

'Zend\View\PhpRenderer' => array(

                'parameters' => array(

                    'resolver' => 'Zend\View\TemplatePathStack',

                    'options'  => array(

                        'script_paths' => array(

                            'application' => __DIR__ . '/../views',

                        ),

                    ),

                    'broker' => 'Zend\View\HelperBroker',

                ),


Now I know 'Zend\View\PhpRenderer' has a setResolver and a setBroker method
so I assume 'resolver' => 'Zend\View\TemplatePathStack',

&   'broker' => 'Zend\View\HelperBroker will both be passed to the setBroker
/ setResolver methods in 'Zend\View\PhpRenderer'...

Now my issue:

 

  'options'  => array(

         'script_paths' => array(

             'application' => __DIR__ . '/../views',

                     ),

 

From some testing I've worked out that the options parameter is passed to
'Zend\View\TemplatePathStack'... What I don't understand is "how" DI knows
to do this?

What's confusing me is resolver and broker are on the same array level as
options. So I would expect it to be passing options to setOptions in
Zend\View\PhpRenderer.

I guess common sense would say 'Zend\View\TemplatePathStack' has 1
constructor argument so the next array element after setting the resolver
would be the constructors argument..

so with my thinking above if we had:

    public function __construct($options = null, $foo = null)

we would have something like this:


'Zend\View\PhpRenderer' => array(

                'parameters' => array(

                    'resolver' => 'Zend\View\TemplatePathStack',

                    'options'  => array(

                        'script_paths' => array(

                            'application' => __DIR__ . '/../views',

                        ),

                    'foo'  => 'foo_something'

                    ),

                    'broker' => 'Zend\View\HelperBroker',

                ),

 

Now am I thinking along the right lines.. Or am I way way off? (after typing
this I'm getting the feeling I am way off... As $options is a optional
argument of 'Zend\View\TemplatePathStack' constructor .. So by my thinking
if we didn't put 'options' in we would end up with 'broker' being passed to
the constructor.....)


Thanks for any help!

 

James





Reply | Threaded
Open this post in threaded view
|

Re: [fw-core] DI Confusion

JamesWilliams
Ok after an evening of messing about some more I seem to have got a better idea.. But I'm still very confused..

Going back to:

  'Zend\View\PhpRenderer' => array(
                'parameters' => array(
                    'resolver' => 'Zend\View\TemplatePathStack',
                    'options'  => array(
                        'script_paths' => array(
                            'application' => __DIR__ . '/../views',
                        ),
                    ),
                    'broker' => 'Zend\View\HelperBroker',
                ),

The setResolver method of Zend\View\PhpRenderer looks like this:

public function setResolver($resolver, $options = null)

So i know realize the options part of the above config gets passed to this $options parameter (tested with a print_r)

ok that all makes sense now..

So i decided to do some more testing. I noticed that if i move the array above around it makes no difference (e.g. put options below broker).

I thought this was strange as i couldn't then workout how it knows that option array should be passed to setResolver....

So I modified setBroker from:


    public function setBroker($broker)

to:

  public function setBroker($broker, $options = null)

Now to my surprise that options array get's passed to both  setBroker AND setResolver. This seems very odd to me... What if setBroker and setResolver both actually had a $options parameter? How would you specify which 'options' array should go to which method?

Thanks,

James