ServiceManager Factories and Callable functions

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

ServiceManager Factories and Callable functions

latterd
Hi All,

I am trying to get my head around the best way to configure the service
manager.

Am I correct in thinking that if I have a ServiceManager defined with
anonymous functions like so: (Added to 'factories' configuration key)

...
'Album\Controller\Foo' => function($sm) {

    $dependancy = new \stdClass();
    $dependancy->bar = 'hello Foo!';
    $controller = new FooController($dependancy);
    $controller->setFooService($dependancy);

},
...

it will be called upon every request?

As opposed to defining a factory class:

'Album\Controller\Foo' => 'Album\Service\FooControllerFactory'

Which will be called when we use the service key:  'Album\Controller\Foo'

Is this correct?

Many Thanks.
Daniel
Reply | Threaded
Open this post in threaded view
|

Re: ServiceManager Factories and Callable functions

Ben Scholzen 'DASPRiD'
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: ServiceManager Factories and Callable functions

stormwild
Hi

I was wondering if there are examples on how the factory class composes
dependencies.

Should it expect to be passed a service manager in it's constructor in the
same function a closure does?

Should a factory create new instances of objects or should it also retrieve
them from the service manager?

Or should a factory import(use)/reference other factories as well?

Much appreciated,

Alex


On Fri, Nov 8, 2013 at 12:25 AM, Ben Scholzen 'DASPRiD' <[hidden email]>wrote:

> Hi Daniel,
>
> > Am I correct in thinking that if I have a ServiceManager defined with
> > anonymous functions like so: (Added to 'factories' configuration key)
> >
> > ...
> >
> > it will be called upon every request?
>
> The closure will only be called when the controller "Foo" is actually
> requested. But the function itself is loaded every time.
>
> > As opposed to defining a factory class:
> >
> > 'Album\Controller\Foo' => 'Album\Service\FooControllerFactory'
> >
> > Which will be called when we use the service key:  'Album\Controller\Foo'
>
> The factory will actually just be loaded when the controller is
> requested. So it makes more sense to really define all factories as
> classes, instead of closures. Closures are just great for rapid
> prototyping.
>
> --
> Ben Scholzen 'DASPRiD'
> Community Review Team Member | [hidden email]
> Zend Framework               | http://www.dasprids.de
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>


--
------------------------------

Alexander R. Torrijos
Web Developer
Mobile: +63 919 991 5653
Res: +63 2 822 7219
http://alex.stormwild.com
Skype/YM: artorrijos

------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: ServiceManager Factories and Callable functions

stormwild
I just looked at the Zend\Db\Adapter\AdapterServiceFactory as an example:

namespace Zend\Db\Adapter;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class AdapterServiceFactory implements FactoryInterface
{
    /**
     * Create db adapter service
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return Adapter
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('Config');
        return new Adapter($config['db']);
    }
}




On Sat, Nov 9, 2013 at 3:53 PM, Alexander Torrijos <[hidden email]>wrote:

> Hi
>
> I was wondering if there are examples on how the factory class composes
> dependencies.
>
> Should it expect to be passed a service manager in it's constructor in the
> same function a closure does?
>
> Should a factory create new instances of objects or should it also
> retrieve them from the service manager?
>
> Or should a factory import(use)/reference other factories as well?
>
> Much appreciated,
>
> Alex
>
>
> On Fri, Nov 8, 2013 at 12:25 AM, Ben Scholzen 'DASPRiD' <[hidden email]>wrote:
>
>> Hi Daniel,
>>
>> > Am I correct in thinking that if I have a ServiceManager defined with
>> > anonymous functions like so: (Added to 'factories' configuration key)
>> >
>> > ...
>> >
>> > it will be called upon every request?
>>
>> The closure will only be called when the controller "Foo" is actually
>> requested. But the function itself is loaded every time.
>>
>> > As opposed to defining a factory class:
>> >
>> > 'Album\Controller\Foo' => 'Album\Service\FooControllerFactory'
>> >
>> > Which will be called when we use the service key:
>>  'Album\Controller\Foo'
>>
>> The factory will actually just be loaded when the controller is
>> requested. So it makes more sense to really define all factories as
>> classes, instead of closures. Closures are just great for rapid
>> prototyping.
>>
>> --
>> Ben Scholzen 'DASPRiD'
>> Community Review Team Member | [hidden email]
>> Zend Framework               | http://www.dasprids.de
>>
>> --
>> List: [hidden email]
>> Info: http://framework.zend.com/archives
>> Unsubscribe: [hidden email]
>>
>>
>>
>
>
> --
> ------------------------------
>
> Alexander R. Torrijos
> Web Developer
> Mobile: +63 919 991 5653
> Res: +63 2 822 7219
> http://alex.stormwild.com
> Skype/YM: artorrijos
>
> ------------------------------
>



--
------------------------------

Alexander R. Torrijos
Web Developer
Mobile: +63 919 991 5653
Res: +63 2 822 7219
http://alex.stormwild.com
Skype/YM: artorrijos

------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: ServiceManager Factories and Callable functions

Artur Bodera
In reply to this post by stormwild
On Sat, Nov 9, 2013 at 8:53 AM, Alexander Torrijos <[hidden email]>wrote:

> I was wondering if there are examples on how the factory class composes
> dependencies.
>

http://framework.zend.com/manual/2.2/en/modules/zend.service-manager.intro.html



--
[hidden email]
+48 695 600 936
http://thinkscape.pro
Reply | Threaded
Open this post in threaded view
|

Re: ServiceManager Factories and Callable functions

stormwild
Thanks, sorry.


On Sat, Nov 9, 2013 at 5:13 PM, Artur Bodera <[hidden email]> wrote:

>
> On Sat, Nov 9, 2013 at 8:53 AM, Alexander Torrijos <[hidden email]>wrote:
>
>> I was wondering if there are examples on how the factory class composes
>> dependencies.
>>
>
>
> http://framework.zend.com/manual/2.2/en/modules/zend.service-manager.intro.html
>
>
>
> --
> [hidden email]
> +48 695 600 936
> http://thinkscape.pro
>



--
------------------------------

Alexander R. Torrijos
Web Developer
Mobile: +63 919 991 5653
Res: +63 2 822 7219
http://alex.stormwild.com
Skype/YM: artorrijos

------------------------------