Thứ Năm, 24 tháng 4, 2014

Zend Framework 2 ServiceManager [Data Management and Document Sharing]

Zend Framework 2 ServiceManager


The ZF2 ServiceManager implements the service locator design pattern. The service locator is a service/object locator used for retrieving other objects.

The ServiceManager configurations are classified into six main categories; your application/module configuration will fall under one or more of the categories listed in the following table:

            Configuration type              Description
abstract_factories                Used to define an array of abstract classes.
aliases                                    Used to define an associative array of alias name / target name pairs. 
Configuration type              Description
factories                                 Used to define an array of service name / factory class name pairs.
The factory classes defined here should either implemenZend/
ServiceManager/FactoryInterface or invokable classes.
invokables                              Used to define an array of service name / class name pairs. The classes listed here may be directly instantiated without anconstructor arguments.
services                                  Used to define an array of service name / object pairs. The service is basically an instance of a class. Services can be used tregister classes which are already initialized.
shared                                     Used to define an array of service name / Boolean pairs, indicating whether or not a service should be shared. All services are shared by default; this ServiceManager option can be used to disable sharing on specific services.
 The ServiceManager configuration can be stored either in the application configuration or in the module configuration; this can be chosen according to the needs, application, or module. Usually, the configuration, which isstatic across the application, is stored in the application-level configuration; all other information is stored at a module level.

The configuration for ServiceManager is merged in the following order:

1.       Module configuration provided by the Module lass using the getServiceConfig()
method. This will be processed in the same order in which the modules are processed:
public function getServiceConfig()
{
return array(
'abstract_factories' => array(), 'aliases' => array(),
'factories' => array(), 'invokables' => array(), 'services' => array(),
'shared' => array(),
);
}

2.       Module configuration is present in the service_manager key; again, this is processed in the same order in which the modules are processed.
3.       Application configuration is present in various configuration files in the
config/autoload/ directory in the order in which they are processed:
<?php
return array(
'service_manager' => array(



'abstract_factories' => array(), 'aliases' => array(),
'factories' => array(), 'invokables' => array(), 'services' => array(), 'shared' => array(),
),
);
Our nexstep will be to migratexisting code blockto make use of ServiceManager. Some of the kefactories thacan be moved into ServiceManager are as follows:

        Database connections
        Models and table gateways
        Forms and filters
        Authentication service

If you review the existing code, you will be able to figure out that all the database connections are already using the Zend Framework 2 ServiceManager model for storing credentials. We
will take one step forward and move the rest of the factories into ServiceManager using the following steps:

1.       Modify Module.php and add a new function to load the ServiceManager configuration:
public function getServiceConfig()
{
return array(
'abstract_factories' => array(), 'aliases' => array(),
'factories' => array(

// DB
'UserTable' =>  function($sm) {
$tableGateway = $sm->get('UserTableGateway');
$table = new UserTable($tableGateway); return $table;
},
'UserTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());


return new TableGateway('user', $dbAdapter, null,
$resultSetPrototype);
},

// FORMS
'LoginForm' => function ($sm) {
$form = new \Users\Form\LoginForm();
$form->setInputFilter($sm->get('LoginFilter')); return $form;
},
'RegisterForm' => function ($sm) {
$form = new \Users\Form\RegisterForm();
$form->setInputFilter($sm->get('RegisterFilter')); return $form;
},

// FILTERS
'LoginFilter' => function ($sm) {
return new \Users\Form\LoginFilter();
},
'RegisterFilter' => function ($sm) {
return new \Users\Form\RegisterFilter();
},
),
'invokables' => array(), 'services' => array(), 'shared' => array(),
);
}

2.       Make sure that the Module.php file includes all the necessary namespaces:
use Users\Model\User;
use Users\Model\UserTable;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
3.       Make necessary changes to the controllertfetch the instances from ServiceManager:
// to get Login Form
$form = $this->getServiceLocator()->get('LoginForm');

// to get User Table
$userTable = $this->getServiceLocator()->get('UserTable');

4.       To check if the changes arworking as expected, trtregister and log in with new credentials.
What just happened?
We have migrated our code to make use of Zend's ServiceManager framework. ServiceManager provides enormous benefits in terms of a cleaner code, highly effective refactoring ability, and a centralized register for core application components.

Không có nhận xét nào:

Đăng nhận xét