ZendFramework-2.4 源代码 - 关于服务管理器

 

// ------ 决定“服务管理器”配置的位置 ------
// 1、在模块的入口类/data/www/www.domain.com/www/module/Module1/Module.php中实现了“服务管理器声明”的注入的接口,那么会进行调用 注入
    $albumModule->getMyMailManagerConfig();
            
// 2、在如下路径获取的到配置中配置了“服务管理器声明”的键: $configListener->getMergedConfig(false)['my_mails']
     2.0 可在模块的入口类/data/www/www.domain.com/www/module/Module1/Module.php中$modulex->getConfig()配置的$modulex->getConfig();
    2.1 $applicationConfig['module_listener_options']['ConfigGlobPaths']抓取的内容
    2.2 $applicationConfig['module_listener_options']['ConfigStaticPaths']抓取的内容
    2.3 $applicationConfig['module_listener_options']['ExtraConfig']配置的内容

// ------ ServiceManagers 服务管理器是如何创建实例的------
     1、查询"对等的服务管理器"中是否存在指定实例对象
     2、依次尝试使用如下的方式创建对象
            isset($this->aliases[$cName])
            isset($this->delegators[$cName])
            isset($this->factories[$cName])
            isset($this->invokableClasses[$cName])
            $this->abstractFactories->canCreateServiceWithName($this, $cName, $rName)
            
    $serviceManager->get('Config') =  $configListener->getMergedConfig(false) 
        === array_merge(
                根据$applicationConfig['module_listener_options']['ConfigGlobPaths']抓取的,
                根据$applicationConfig['module_listener_options']['ConfigStaticPaths']抓取的,
                根据$applicationConfig['module_listener_options']['ExtraConfig']配置的,
                $modulex->getConfig()配置的
        )
// ------ 如何创建、使用自己的“服务管理器” ------ 1、编写“服务管理器”相关类 // 1.0 声明“服务管理器”配置注入入口 namespace Zendx\MyMailPluginManager\Feature; interface MyMailPluginManagerProviderInterface { public function getMyMailPluginManagerConfig(); } //1.1 编写“服务管理器”业务代码 namespace Zendx\MyMailPluginManager\; class MyMailPluginManager extends AbstractPluginManager { public function __construct(ConfigInterface $configuration = null) { parent::__construct($configuration); $this->addInitializer(array($this, 'injectDependencies'), false); } public function injectDependencies($mailPlugin, ServiceLocatorInterface $serviceLocator) { } public function has($name, $checkAbstractFactories = true, $usePeeringServiceManagers = false) { return parent::has($name, $checkAbstractFactories, $usePeeringServiceManagers); } public function get($name, $options = array(), $usePeeringServiceManagers = false) { return parent::get($name, $options, $usePeeringServiceManagers); } } 2、使用“服务管理器” 2.1、声明“服务管理器”的创建方式 $applicationConfig = array( // 服务管理器的配置 'service_manager' => array( // ... 'invokables' => array( // 服务的创建方式:直接 new 出对象 return new $invokable(); 'SharedEventManager' => 'Zend\EventManager\SharedEventManager', 'Album\Service\AlbumServiceInterface' => 'Album\Service\AlbumService',// return (new Album\Service\AlbumService()); 'Zendx\ModuleManager\Feature\MyMailManagerProviderInterface' => 'Zendx\ModuleManager\Feature\MyMailManagerProvider', ), // ... ) // ... ); 2.2、声明 配置“服务管理器”的方式 $applicationConfig = array( // ... 'service_listener_options'=>array( // ... 'key_non_used_1'=>array( // 扩展自己的管理器 $serviceManager->get('MyMailPluginManager'); 'service_manager'=>'MyMailPluginManager', 'config_key'=>'my_mail_plugin_manager', // $configListener->getMergedConfig(false)['my_mails'] // 从模块入口类中获取配置的方式 // 只需 /data/www/www.domain.com/www/module/Module1/Module.php 中的 Module 类实现如下内容 'interface'=>'Zendx\MyMailPluginManager\Feature\MyMailPluginManagerProviderInterface', 'method'=>'getMyMailPluginManagerConfig', // $albumModule->getMyMailPluginManagerConfig(); ), // ... ) // ... ); 2.3、配置 2.3.0 可在模块的入口类/data/www/www.domain.com/www/module/Module1/Module.php中实现“服务管理器声明”的注入的接口,那么会自动进行调用 注入 namespace Module1; class Module implements ...,MyMailPluginManagerProviderInterface { // ... public function getMyMailPluginManagerConfig() { return include __DIR__ . '/config/my_mail_plugin_manager.config.php'; } // ... } 2.3.1.0 可在模块的入口类/data/www/www.domain.com/www/module/Module1/Module.php中$modulex->getConfig()配置 $modulex->getConfig(); // return include __DIR__ . '/config/module.config.php'; 2.3.1.1可在 $applicationConfig['module_listener_options']['ConfigGlobPaths']路径中的文件配置 2.3.1.2可在 $applicationConfig['module_listener_options']['ConfigStaticPaths']路径中的文件配置 2.3.1.3可在 $applicationConfig = array( // ... 'module_listener_options'=>array( // ... 'ExtraConfig'=>array( 'my_mail_plugin_manager'=>array( 'key1'=>'key1_value', 'key2'=>'key2_value', ), ), // ... ) // ... ); 2.4、应用 $serviceManager->get('MyMailPluginManager'); // ---------- 例子、模块配置 ---------------- // /data/www/www.domain.com/www/module/Album/config/module.config.php $moduleConfig = array( // 配置导航 'navigation'=>array( 'style1'=>'' ), 'di'=>array( 'allowed_controllers'=>array( 'Controller1', 'Controller2', ) ), 'view_manager'=>array( // 模板搜索路径 // Zend\View\Resolver\TemplateMapResolver 'template_map'=>'', // Zend\View\Resolver\TemplatePathStack 'template_path_stack'=>array( 'album' => __DIR__ . '/../view', 'module1' => __DIR__ . '/../view', ), 'default_template_suffix'=>'phtml', // Zend\View\Resolver\PrefixPathStackResolver 'prefix_template_path_stack'=>'', ) ); // ------------例子、 Application 应用的配置-------------- // /data/www/www.domain.com/www/config/application.config.php $applicationConfig = array( // 服务管理器的配置 'service_manager' => array( 'allow_override'=>null, 'factories'=>array( // 服务的创建方式:调用工厂创建 return $xxxfactory->createService($serviceLocator); 'EventManager' => 'Zend\Mvc\Service\EventManagerFactory', 'ModuleManager' => 'Zend\Mvc\Service\ModuleManagerFactory', ), 'abstract_factories'=>array( // 服务的创建方式:调用抽象工厂创建 $abstractFactory->createServiceWithName($serviceLocator,$cName, $rName); // 服务的获取方式:$serviceManager->get('zendnavigationstyle1'); 'zendnavigationstyle1'=>'Zend\Navigation\Service\NavigationAbstractServiceFactory' ), 'invokables' => array( // 服务的创建方式:直接 new 出对象 return new $invokable(); 'SharedEventManager' => 'Zend\EventManager\SharedEventManager', 'Album\Service\AlbumServiceInterface' => 'Album\Service\AlbumService',// return (new Album\Service\AlbumService()); 'Zendx\ModuleManager\Feature\MyMailManagerProviderInterface' => 'Zendx\ModuleManager\Feature\MyMailManagerProvider', ), 'services'=>array( // 服务的创建方式:“键值对”的“值”是实例对象 'instance_1'=>new Instance_1(), 'PHPMailer'=>new PHPMailer(), // $serviceManager->get('PHPMailer'); 'myconfig1'=>array( // $serviceManager->get('myconfig1'); 'key1'=>'key1_value', 'key2'=>'key2_value', ), ), 'aliases'=>array( // 别名 // 服务的获取方式:$serviceManager->get('Zend\EventManager\EventManagerInterface') === $serviceManager->get('EventManager'); 'Zend\EventManager\EventManagerInterface' => 'EventManager', 'Zend\ServiceManager\ServiceLocatorInterface' => 'ServiceManager', 'Zend\ServiceManager\ServiceManager' => 'ServiceManager', ), 'initializers'=>array( // 创建完对象的初始化器 'ServiceLocatorAwareInitializer' => function ($instance, ServiceLocatorInterface $serviceLocator) { if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($serviceLocator); } } ), 'shared'=>array( // 创建出来的对象是否共享。如果不配置,默认是共享的。 'EventManager' => false, ), 'delegators'=>array( // 委托 'lazyService'=>array( 'LazyServiceFactory' ) ) ), 'module_listener_options'=>array( 'ExtraConfig'=>array( // 配置导航 'navigation'=>array( 'style1'=>'' ) ) ), 'service_listener_options'=>array( 'ControllerLoader'=>array( 'service_manager'=>'ControllerLoader', 'config_key'=>'controllers', 'interface'=>'Zend\ModuleManager\Feature\ControllerProviderInterface', 'method'=>'getControllerConfig', ), 'MyMailManager'=>array( // 声明自己的“服务管理器” $serviceManager->get('MyMailManager'); 'service_manager'=>'MyMailManager', 'config_key'=>'my_mails', 'interface'=>'Zendx\ModuleManager\Feature\MyMailManagerProviderInterface', 'method'=>'getMyMailManagerConfig', ) ) );

 

posted on 2017-02-10 18:22  周~~  阅读(323)  评论(0编辑  收藏  举报

导航