zf2 启动简介:二,服务的获取

zf2框架在启动的过程中会创建一个service manager对象 (仅仅会实例化一次)

而zf2框架中各种服务的调用都是通过 service manager 对象来实现的

最常用的如 get 方法,用来获取某一个服务

1
2
3
4
5
6
7
8
9
10
11
12
//Zend\Mvc\Application::init(require 'config/application.config.php')
public static function init($configuration = array())
{
$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
$listeners = isset($configuration['listeners']) ? $configuration['listeners'] : array();
$serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig));
$serviceManager->setService('ApplicationConfig', $configuration);
$serviceManager->get('ModuleManager')->loadModules();
return $serviceManager->get('Application')->bootstrap($listeners);
}

在zf2启动过程中一开始便通过 Zend/Mvc/Service/ServiceManagerConfig.php 来初始化的创建一个服务管理器
提供的默认配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
protected $invokables = array(
'SharedEventManager' => 'Zend\EventManager\SharedEventManager',
);
/**
* Service factories
*
* @var array
*/
protected $factories = array(
'EventManager' => 'Zend\Mvc\Service\EventManagerFactory',
'ModuleManager' => 'Zend\Mvc\Service\ModuleManagerFactory',
);
/**
* Abstract factories
*
* @var array
*/
protected $abstractFactories = array();
/**
* Aliases
*
* @var array
*/
protected $aliases = array(
'Zend\EventManager\EventManagerInterface' => 'EventManager',
);
/**
* Shared services
*
* Services are shared by default; this is primarily to indicate services
* that should NOT be shared
*
* @var array
*/
protected $shared = array(
'EventManager' => false,
);

这些默认初始化配置会合并到 service manager 对象中,在启动过程中除了这些默认配置以外,其实在启动的其他位置也提供了许多 service manager 对象默认的服务配置
Zend/Mvc/Service/ServiceListenerFactory.php 中提供的默认配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
protected $defaultServiceConfig = array(
'invokables' => array(
'DispatchListener' => 'Zend\Mvc\DispatchListener',
'RouteListener' => 'Zend\Mvc\RouteListener',
'SendResponseListener' => 'Zend\Mvc\SendResponseListener'
),
'factories' => array(
'Application' => 'Zend\Mvc\Service\ApplicationFactory',
'Config' => 'Zend\Mvc\Service\ConfigFactory',
'ControllerLoader' => 'Zend\Mvc\Service\ControllerLoaderFactory',
'ControllerPluginManager' => 'Zend\Mvc\Service\ControllerPluginManagerFactory',
'ConsoleAdapter' => 'Zend\Mvc\Service\ConsoleAdapterFactory',
'ConsoleRouter' => 'Zend\Mvc\Service\RouterFactory',
'ConsoleViewManager' => 'Zend\Mvc\Service\ConsoleViewManagerFactory',
'DependencyInjector' => 'Zend\Mvc\Service\DiFactory',
'DiAbstractServiceFactory' => 'Zend\Mvc\Service\DiAbstractServiceFactoryFactory',
'DiServiceInitializer' => 'Zend\Mvc\Service\DiServiceInitializerFactory',
'DiStrictAbstractServiceFactory' => 'Zend\Mvc\Service\DiStrictAbstractServiceFactoryFactory',
'FilterManager' => 'Zend\Mvc\Service\FilterManagerFactory',
'FormElementManager' => 'Zend\Mvc\Service\FormElementManagerFactory',
'HttpRouter' => 'Zend\Mvc\Service\RouterFactory',
'HttpViewManager' => 'Zend\Mvc\Service\HttpViewManagerFactory',
'HydratorManager' => 'Zend\Mvc\Service\HydratorManagerFactory',
'InputFilterManager' => 'Zend\Mvc\Service\InputFilterManagerFactory',
'MvcTranslator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
'PaginatorPluginManager' => 'Zend\Mvc\Service\PaginatorPluginManagerFactory',
'Request' => 'Zend\Mvc\Service\RequestFactory',
'Response' => 'Zend\Mvc\Service\ResponseFactory',
'Router' => 'Zend\Mvc\Service\RouterFactory',
'RoutePluginManager' => 'Zend\Mvc\Service\RoutePluginManagerFactory',
'SerializerAdapterManager' => 'Zend\Mvc\Service\SerializerAdapterPluginManagerFactory',
'ValidatorManager' => 'Zend\Mvc\Service\ValidatorManagerFactory',
'ViewHelperManager' => 'Zend\Mvc\Service\ViewHelperManagerFactory',
'ViewFeedRenderer' => 'Zend\Mvc\Service\ViewFeedRendererFactory',
'ViewFeedStrategy' => 'Zend\Mvc\Service\ViewFeedStrategyFactory',
'ViewJsonRenderer' => 'Zend\Mvc\Service\ViewJsonRendererFactory',
'ViewJsonStrategy' => 'Zend\Mvc\Service\ViewJsonStrategyFactory',
'ViewManager' => 'Zend\Mvc\Service\ViewManagerFactory',
'ViewResolver' => 'Zend\Mvc\Service\ViewResolverFactory',
'ViewTemplateMapResolver' => 'Zend\Mvc\Service\ViewTemplateMapResolverFactory',
'ViewTemplatePathStack' => 'Zend\Mvc\Service\ViewTemplatePathStackFactory',
),
'aliases' => array(
'Configuration' => 'Config',
'Console' => 'ConsoleAdapter',
'Di' => 'DependencyInjector',
'Zend\Di\LocatorInterface' => 'DependencyInjector',
'Zend\Mvc\Controller\PluginManager' => 'ControllerPluginManager',
'Zend\View\Resolver\TemplateMapResolver' => 'ViewTemplateMapResolver',
'Zend\View\Resolver\TemplatePathStack' => 'ViewTemplatePathStack',
'Zend\View\Resolver\AggregateResolver' => 'ViewResolver',
'Zend\View\Resolver\ResolverInterface' => 'ViewResolver',
),
'abstract_factories' => array(
'Zend\Form\FormAbstractServiceFactory',
)

而对于框架的使用者来说我们可以在模块配置文件中自定义服务配置,或者在控制器或者方法中动态注册服务(不太推荐,相对于在配置中注册多走了一道流程)

如下是用户自定义配置实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
// a module configuration, "module/SomeModule/config/module.config.php"
return array(
'service_manager' => array(
'abstract_factories' => array(
// Valid values include names of classes implementing
// AbstractFactoryInterface, instances of classes implementing
// AbstractFactoryInterface, or any PHP callbacks
'SomeModule\Service\FallbackFactory',
),
'aliases' => array(
// Aliasing a FQCN to a service name
'SomeModule\Model\User' => 'User',
// Aliasing a name to a known service name
'AdminUser' => 'User',
// Aliasing to an alias
'SuperUser' => 'AdminUser',
),
'factories' => array(
// Keys are the service names.
// Valid values include names of classes implementing
// FactoryInterface, instances of classes implementing
// FactoryInterface, or any PHP callbacks
'User' => 'SomeModule\Service\UserFactory',
'UserForm' => function ($serviceManager) {
$form = new SomeModule\Form\User();
// Retrieve a dependency from the service manager and inject it!
$form->setInputFilter($serviceManager->get('UserInputFilter'));
return $form;
},
),
'invokables' => array(
// Keys are the service names
// Values are valid class names to instantiate.
'UserInputFiler' => 'SomeModule\InputFilter\User',
),
'services' => array(
// Keys are the service names
// Values are objects
'Auth' => new SomeModule\Authentication\AuthenticationService(),
),
'shared' => array(
// Usually, you'll only indicate services that should _NOT_ be
// shared -- i.e., ones where you want a different instance
// every time.
'UserForm' => false,
),
),
);