0

I am currently looking a this piece of code from a module called ZfcUser for Zend 2:

namespace ZfcUser\Controller;

use Zend\Form\Form;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\View\Model\ViewModel;
use ZfcUser\Service\User as UserService;
use ZfcUser\Options\UserControllerOptionsInterface;

class UserController extends AbstractActionController
{
/**
 * @var UserService
 */
protected $userService;
     .
     .

public function indexAction()
{
    if (!$this->zfcUserAuthentication()->hasIdentity()) {
        return $this->redirect()->toRoute('zfcuser/login');
    }
    return new ViewModel();
}
    .
    .
}

In the namespace ZfcUser\Controller\Plugin:

namespace ZfcUser\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use ZfcUser\Authentication\Adapter\AdapterChain as AuthAdapter;

class ZfcUserAuthentication extends AbstractPlugin implements ServiceManagerAwareInterface
{
/**
 * @var AuthAdapter
 */
protected $authAdapter;
    .
    .
/**
 * Proxy convenience method
 *
 * @return mixed
 */
public function hasIdentity()
{
    return $this->getAuthService()->hasIdentity();
}
/**
 * Get authService.
 *
 * @return AuthenticationService
 */
public function getAuthService()
{
    if (null === $this->authService) {
        $this->authService = $this->getServiceManager()->get('zfcuser_auth_service');
    }
    return $this->authService;
}

My Questions:

  • From indexAction(), the controller plugin is called without being instantiated ($this->zfcUserAuthentication()->hasIdentity()), do controller plugins always work like this?.
  • What really happens in the hasIdentity()? I see getAuthService() returning something but not hasIdentity().I am not familiar with this type of advanced class implementation of function calling so I would truly appreciate any explanation here or topic I should look into.
paxtor
  • 331
  • 5
  • 17

2 Answers2

1

I can't answer your first question, but regarding your second question:

The getAuthService() method in your code returns an AuthenticationService object, which has a hasIdentity() method.

So there are two different hasIdentity() methods:

  • In the AuthenticationService class (source code here).
  • In the ZfcUserAuthentication class which you're looking at.

This line of code in the ZfcUserAuthentication class:

return $this->getAuthService()->hasIdentity();

does three things:

  • $this->getAuthService() returns an AuthenticationService object.
  • The hasIdentity() method of that AuthenticationService object is then called, and it returns a boolean.
  • That boolean is then returned.

Imagine splitting the code into two parts:

// Get AuthenticationService object     Call a method of that object
$this->getAuthService()                 ->hasIdentity();

Hope that helps!

TachyonVortex
  • 8,242
  • 3
  • 48
  • 63
  • Awesome! I really appreciate your answer on my second question, TachyonVortex. The answer was indeed helpful and it all makes sense now. – paxtor Nov 25 '12 at 23:34
0

All sorts of plugins in Zend Framework are managed by plugin managers, which are subclasses of AbstractPluginManager which is subclasss of ServiceManager.

$this->zfcUserAuthentication() proxies by AbstractController to pluginmanager internally.

AuthenticationService::hasIdentity() checks if something was added to storage during successful authentication attempt in this or previous request: See here

Xerkus
  • 2,695
  • 1
  • 19
  • 32
  • Xerkus, are you saying that once a plugin is implemented it's automatically sent to the ServiceManager? If it is then that kind of makes sense that the class would be instantiated there. is that it? – paxtor Nov 25 '12 at 23:46
  • No, it should be registered first, as invokable: https://github.com/ZF-Commons/ZfcUser/blob/master/config/module.config.php#L15 or as factory. Then service manager will be aware of it and will instantiate it – Xerkus Nov 26 '12 at 01:32
  • I'm starting to understand this better now. However, when things are put in the service manager and to get to them, aren't they retrieved by their key name? In this case, 'zfcuserauthentication' => 'ZfcUser\Controller\Plugin\ZfcUserAuthentication', shouldn't be retrieved as 'zfcuserauthentication' as opposed to 'ZfcUserAuthentication'? – paxtor Nov 26 '12 at 02:40
  • keys in service manager are case insensitive, but `zfcUserAuthentication()` follows plugin class name and section 4.3 of PSR-1 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md#43-methods – Xerkus Nov 26 '12 at 09:00
  • That's it! Thanks Xerkus. If it's case insensitive then it makes complete sense. Thanks a lot. – paxtor Nov 26 '12 at 15:04