[fw-general] Re: [ZF2] Best practice to get service in controller ?

2013-12-17 Thread MichaelB
Thanks Matthew for your answer!

Little other questions about it..

Is it a performance difference between creating a controller factory in a
different class OR creating all in "getControllerConfig()" ?  
(http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/)

And last question.. ;-) Form should be injected too ?


Thanks for your answer!
Michael



--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/ZF2-Best-practice-to-get-service-in-controller-tp4661350p4661363.html
Sent from the Zend Framework mailing list archive at Nabble.com.

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com




Re: [fw-general] Re: [ZF2] Best practice to get service in controller ?

2013-12-17 Thread Matthew Weier O'Phinney
On Tue, Dec 17, 2013 at 12:57 PM, MichaelB  wrote:
> Thanks Matthew for your answer!
>
> Little other questions about it..
>
> Is it a performance difference between creating a controller factory in a
> different class OR creating all in "getControllerConfig()" ?
> (http://www.zfdaily.com/2012/07/getting-dependencies-into-zf2-controllers/)

Putting it in a class actually is better, for a few reasons.

- First, if you never use the factory, no code is loaded. If you
define it in getControllerConfig(), you're defining a closure in each
and every request.
- Second, it gives you the option of extending or otherwise re-using
the factory later.

> And last question.. ;-) Form should be injected too ?

Typically, yes -- particularly as you can now seed the form elements,
hydrators, input filters and inputs, filters, and validators via
plugin managers, you'll likely want access to those plugin managers to
create your form -- so why not use a factory? :)


-- 
Matthew Weier O'Phinney
Project Lead| matt...@zend.com
Zend Framework  | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com




Re: [fw-general] Do factories get passed through initializers?

2013-12-17 Thread Matthew Weier O'Phinney
On Tue, Dec 17, 2013 at 8:52 AM, Marco Pivetta  wrote:
> Sorry for the late reply.
>
> No, factories are NOT passed to initializers, and shouldn't.
>
> Think of a factory as a named callback. Nothing else.

Actually, I'm going to clarify a bit further.

What gets passed to initializers are any objects the ServiceManager
_creates_. That means:

- an "invokable" class
- the result of a factory
- the result of an abstract factory

Essentially, as soon as the SM decides it doesn't have an instance
already, but has a definition for one, it calls the routine necessary
for creating the object, and then passes that result to each
initializer. It then stores it -- which means that the next time you
retrieve it, the instance will *not* be passed to the initializers.

> On 9 December 2013 17:06, Xander Guzman  wrote:
>
>> It does
>>
>> /**
>>  * Defines an initializer that knows how to inject a configuration object
>>  */
>> class ConfigInitializer implements InitializerInterface
>> {
>> /**
>>  * Initialize
>>  *
>>  * @param $instance
>>  * @param ServiceLocatorInterface $serviceLocator
>>  *
>>  * @return mixed
>>  */
>> public function initialize($instance, ServiceLocatorInterface
>> $serviceLocator)
>> {
>> if ($instance instanceof ConfigAwareInterface) {
>> $config = $serviceLocator->get('Config');
>> $instance->setConfig($config);
>> }
>> }
>> }
>>
>>
>> On Mon, Dec 9, 2013 at 8:52 AM, franz de leon  wrote:
>>
>> > Your factory should implement the interface in your initializer.
>> >
>> > array(
>> > 'factories' => array(
>> > 'solrconfig' => 'Solr\Config\ConfigInitializer',
>> > ),
>> > 'initializers' => array(
>> >  function ($instance, $sm) {
>> >   if ($instance instanceof Configaware) {
>> >$instance->setConfig($sm->get('solrconfig'));
>> >   }
>> >  }
>> > )
>> > )
>> >
>> >
>> > On Mon, Dec 9, 2013 at 10:24 AM, Xander Guzman
>> > wrote:
>> >
>> > > Like the database configurations I have multiple Solr configurations so
>> > I'm
>> > > trying to create a factory that can return the connection to the one
>> > > specified.
>> > >
>> > > return array(
>> > > "solr" => array(
>> > > 'media' => array( /** config */ ),
>> > > 'vendors' => array( /** config */ ),
>> > > ),
>> > > );
>> > >
>> > > Right now, in my module, I have the Following configuration for the
>> > > getServiceConfig method
>> > >
>> > > return array(
>> > > 'factories' => array(
>> > > 'Solr\Service' => 'Solr\Service\Factory',
>> > > 'Solr\Client' => 'Solr\Client\Factory',
>> > > ),
>> > > 'initializers' => array(
>> > > 'SolrConfig' => 'Solr\Config\ConfigInitializer',
>> > > ),
>> > > );
>> > >
>> > > Here is how things are working.
>> > >
>> > > The factories work and are getting called and the initializer is being
>> > > called, but the initializer isn't being called on the factories.
>> > >
>> > > So first, why? Is this by design? Am I misunderstanding how this
>> > > relationship works?
>> > >
>> > > How can I get the same kind of multiple configurations to work I tried
>> > > digging through the DB ones but I don't think I've found the class that
>> > yet
>> > > reads the config to pass off to a specific adapter.
>> > >
>> > > Any help is appreciated.
>> > >
>> > > Cheers!
>> > >
>> > > Xander
>> > >
>> >
>>



-- 
Matthew Weier O'Phinney
Project Lead| matt...@zend.com
Zend Framework  | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

-- 
List: fw-general@lists.zend.com
Info: http://framework.zend.com/archives
Unsubscribe: fw-general-unsubscr...@lists.zend.com




Re: [fw-general] Do factories get passed through initializers?

2013-12-17 Thread Marco Pivetta
Sorry for the late reply.

No, factories are NOT passed to initializers, and shouldn't.

Think of a factory as a named callback. Nothing else.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On 9 December 2013 17:06, Xander Guzman  wrote:

> It does
>
> /**
>  * Defines an initializer that knows how to inject a configuration object
>  */
> class ConfigInitializer implements InitializerInterface
> {
> /**
>  * Initialize
>  *
>  * @param $instance
>  * @param ServiceLocatorInterface $serviceLocator
>  *
>  * @return mixed
>  */
> public function initialize($instance, ServiceLocatorInterface
> $serviceLocator)
> {
> if ($instance instanceof ConfigAwareInterface) {
> $config = $serviceLocator->get('Config');
> $instance->setConfig($config);
> }
> }
> }
>
>
> On Mon, Dec 9, 2013 at 8:52 AM, franz de leon  wrote:
>
> > Your factory should implement the interface in your initializer.
> >
> > array(
> > 'factories' => array(
> > 'solrconfig' => 'Solr\Config\ConfigInitializer',
> > ),
> > 'initializers' => array(
> >  function ($instance, $sm) {
> >   if ($instance instanceof Configaware) {
> >$instance->setConfig($sm->get('solrconfig'));
> >   }
> >  }
> > )
> > )
> >
> >
> > On Mon, Dec 9, 2013 at 10:24 AM, Xander Guzman
> > wrote:
> >
> > > Like the database configurations I have multiple Solr configurations so
> > I'm
> > > trying to create a factory that can return the connection to the one
> > > specified.
> > >
> > > return array(
> > > "solr" => array(
> > > 'media' => array( /** config */ ),
> > > 'vendors' => array( /** config */ ),
> > > ),
> > > );
> > >
> > > Right now, in my module, I have the Following configuration for the
> > > getServiceConfig method
> > >
> > > return array(
> > > 'factories' => array(
> > > 'Solr\Service' => 'Solr\Service\Factory',
> > > 'Solr\Client' => 'Solr\Client\Factory',
> > > ),
> > > 'initializers' => array(
> > > 'SolrConfig' => 'Solr\Config\ConfigInitializer',
> > > ),
> > > );
> > >
> > > Here is how things are working.
> > >
> > > The factories work and are getting called and the initializer is being
> > > called, but the initializer isn't being called on the factories.
> > >
> > > So first, why? Is this by design? Am I misunderstanding how this
> > > relationship works?
> > >
> > > How can I get the same kind of multiple configurations to work I tried
> > > digging through the DB ones but I don't think I've found the class that
> > yet
> > > reads the config to pass off to a specific adapter.
> > >
> > > Any help is appreciated.
> > >
> > > Cheers!
> > >
> > > Xander
> > >
> >
>