On Fri, May 17, 2013 at 7:16 AM, Razorblade <kaiohken1...@hotmail.com> wrote:
> I am 'disassembling' ZFCUser trying to understand logics behind this module
> and how to extend it.
> I am a bit confuse about the AuthenticationService and how it works.
>
> First, within the Module.php we have
>
> 'ZfcUser\Authentication\Adapter\AdapterChain' =>
> 'ZfcUser\Authentication\Adapter\AdapterChainServiceFactory',
>
> that is required within the configuration key 'zfcuser_auth_service'
>
>                 'zfcuser_auth_service' => function ($sm) {
>                     return new \Zend\Authentication\AuthenticationService(
>                         $sm->get('ZfcUser\Authentication\Storage\Db'),
>
> $sm->get('ZfcUser\Authentication\Adapter\AdapterChain') // here
>                     );
>                 },
>
> AuthenticationService requires as second parameter an instance of
> Adapter\AdapterInterface and the
> 'ZfcUser\Authentication\Adapter\AdapterChain' will create an instance of
> AdapterChainServiceFactory ( correct me if I am mistaking) that returns the
> correct object type after it calls the 'createService' method: who calls
> this method and when? 'ZfcUser\Authentication\Adapter\AdapterChain' will
> contain the object returned by the createService?

You need to do some reading on the ServiceManager. I'd recommend the
following pages:

- 
http://framework.zend.com/manual/2.2/en/modules/zend.mvc.services.html#servicemanager
- 
http://framework.zend.com/manual/2.2/en/modules/zend.service-manager.intro.html
- 
http://framework.zend.com/manual/2.2/en/modules/zend.service-manager.quick-start.html

When you define service manager configuration, you're telling the
ServiceManager how it should create and/or fetch instances that
represent a given service.

In your first snippet, the authors are defining a ServiceManager
_factory_ for the service name
"ZfcUser\Authentication\Adapter\AdapterChain". That factory will be
consumed by the ServiceManager when it needs to get an instance for
that service. (Typically, this will happen only once, as most services
are shared by default, so once we have an instance, it's stored for
later retrieval.)

In the second snippet you quote above, the authors are using a PHP
Closure to define a _factory_ for the "zfcuser_auth_service".
Factories are always passed the ServiceManager instance as an
argument, so that you can fetch other objects you might need to build
your dependency graph. In the case of this factory, it fetches the
AdapterChain service and injects it. Since the AdapterChain service is
defined as  a factory, that means the factory will be invoked on the
first retrieval of that service -- that's when createService() is
called.

> Also I noticed that the controller plugin 'zfcUserAuthentication' is created
> to be used as a 'ZF1 action helper' in the UserController: as the
> AuthAdapter exists within the $authService why don't use directly the
> 'zfcuser_auth_service' by creating a getauthAdapter in the userController?

That's absolutely possible, and some people actually prefer to inject
helpers/plugins/etc. directly into the controllers, as it makes the
dependencies more clear. The plugin system, however, allows you to be
more lazy and selective about it - often you only need a plugin in one
or two actions, which means fetching and injecting it on every
invocation of the controller is overkill. (That said, another new
feature of 2.2 is Lazy Services, which addresses this issue nicely.)


--
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


Reply via email to