首页 > 代码库 > ZendFramework-2.4 源代码 - 关于MVC - Controller层

ZendFramework-2.4 源代码 - 关于MVC - Controller层

 

// 1.控制器管理器
class ServiceManager implements ServiceLocatorInterface
{
    public function __construct(ConfigInterface $config = null)
    {
        if ($config) {
            $config->configureServiceManager($this);
        }
    }
}
abstract class AbstractPluginManager extends ServiceManager implements ServiceLocatorAwareInterface
{
    public function __construct(ConfigInterface $configuration = null)
    {
        parent::__construct($configuration);
        $self = $this;
        $this->addInitializer(function ($instance) use ($self) {//!!! 创建出来的对象的初始化器
            if ($instance instanceof ServiceLocatorAwareInterface) {
                $instance->setServiceLocator($self);
            }
        });
    }
    
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
}
class ControllerManager extends AbstractPluginManager
{
    public function __construct(ConfigInterface $configuration = null)
    {
        parent::__construct($configuration);
        // Pushing to bottom of stack to ensure this is done last
        $this->addInitializer(array($this, ‘injectControllerDependencies‘), false);
    }
    
    public function injectControllerDependencies($controller, ServiceLocatorInterface $serviceLocator)
    {
        if (!$controller instanceof DispatchableInterface) {
            return;
        }
        // $serviceLocator === ControllerManager
        // $parentLocator === ServiceManager
        $parentLocator = $serviceLocator->getServiceLocator(); // ServiceManager
    
        if ($controller instanceof ServiceLocatorAwareInterface) {//!!! 注入服务管理器
            $controller->setServiceLocator($parentLocator->get(‘Zend\ServiceManager\ServiceLocatorInterface‘));
        }
    
        if ($controller instanceof EventManagerAwareInterface) {
            // If we have an event manager composed already, make sure it gets
            // injected with the shared event manager.
            // The AbstractController lazy-instantiates an EM instance, which
            // is why the shared EM injection needs to happen; the conditional
            // will always pass.
            $events = $controller->getEventManager();// 创建新的事件管理器
            if (!$events instanceof EventManagerInterface) {
                $controller->setEventManager($parentLocator->get(‘EventManager‘));
            } else {
                // !!!注入共享事件管理器
                // $parentLocator->get(‘SharedEventManager‘)->printIdentifiersKey();
                $events->setSharedManager($parentLocator->get(‘SharedEventManager‘));
            }
        }
    
        if ($controller instanceof AbstractConsoleController) {
            $controller->setConsole($parentLocator->get(‘Console‘)); // 注入控制台对象
        }
    
        if (method_exists($controller, ‘setPluginManager‘)) { // 注入插件管理器
            // Zend\Mvc\Service\ControllerPluginManagerFactory
            // Zend\Mvc\Controller\PluginManager
            $controller->setPluginManager($parentLocator->get(‘ControllerPluginManager‘));
        }
    }
}

// 2.控制器创建 + 感知注入1
$controllerLoader = $application->getServiceManager()->get(‘ControllerManager‘); // Zend\Mvc\Controller\ControllerManager 在 loadModules.post 已然实例化
$controller = $controllerLoader->get($controllerName); // 创建控制器对象

// 3.感知注入2
if ($controller instanceof InjectApplicationEventInterface) {
    $controller->setEvent($e);//!!!
}

// 4. case.0控制器中获取感知对象
class Ctrl1Controller extends AbstractConsoleController
{
    public function testAction()
    {
        $applicaitonServiceManager = $this->getServiceLocator();
        $albumTable = $applicaitonServiceManager->get(‘Album\Model\AlbumTable‘);
        $applicationConfig = $applicaitonServiceManager->get(‘application‘);
        $applicaitonServiceManager->get(‘Config‘) =  array_merge($application[‘module_listener_options‘][‘ExtraConfig‘],glob($application[‘module_listener_options‘][‘ConfigGlobPaths‘]),$modulex->getConfig());
        
        
        $ctrlEventManager = $this->getEventManager();
        $ctrlSharedManager = $applicationSharedManager = $ctrlEventManager->getSharedManager();
        $applicationEvent = $this->getEvent();
        $ctrlPluginManager = $this->getPluginManager();
        
    }
}
// 4. case.1控制器中获取感知对象
class Ctrl1Controller extends AbstractActionController
{
    public function testAction()
    {
        $applicaitonServiceManager = $this->getServiceLocator();
        $albumTable = $applicaitonServiceManager->get(‘Album\Model\AlbumTable‘);
        $applicationConfig = $applicaitonServiceManager->get(‘application‘);
        $applicaitonServiceManager->get(‘Config‘) =  array_merge($application[‘module_listener_options‘][‘ExtraConfig‘],glob($application[‘module_listener_options‘][‘ConfigGlobPaths‘]),$modulex->getConfig() );
        
        $ctrlEventManager = $this->getEventManager();
        $ctrlSharedManager = $applicationSharedManager = $ctrlEventManager->getSharedManager();
        $applicationEvent = $this->getEvent();
        $ctrlPluginManager = $this->getPluginManager();
    }
}
// 5、控制器插件管理器
class PluginManager extends AbstractPluginManager
{
    /**
     * Default set of plugins factories
     *
     * @var array
     */
    protected $factories = array(
        ‘forward‘  => ‘Zend\Mvc\Controller\Plugin\Service\ForwardFactory‘,
        ‘identity‘ => ‘Zend\Mvc\Controller\Plugin\Service\IdentityFactory‘,
    );
    
    /**
     * Default set of plugins
     *
     * @var array
    */
    protected $invokableClasses = array(
        ‘acceptableviewmodelselector‘ => ‘Zend\Mvc\Controller\Plugin\AcceptableViewModelSelector‘,
        ‘filepostredirectget‘         => ‘Zend\Mvc\Controller\Plugin\FilePostRedirectGet‘,
        ‘flashmessenger‘              => ‘Zend\Mvc\Controller\Plugin\FlashMessenger‘,
        ‘layout‘                      => ‘Zend\Mvc\Controller\Plugin\Layout‘,
        ‘params‘                      => ‘Zend\Mvc\Controller\Plugin\Params‘,
        ‘postredirectget‘             => ‘Zend\Mvc\Controller\Plugin\PostRedirectGet‘,
        ‘redirect‘                    => ‘Zend\Mvc\Controller\Plugin\Redirect‘,
        ‘url‘                         => ‘Zend\Mvc\Controller\Plugin\Url‘,
        ‘createhttpnotfoundmodel‘     => ‘Zend\Mvc\Controller\Plugin\CreateHttpNotFoundModel‘,
        ‘createconsolenotfoundmodel‘  => ‘Zend\Mvc\Controller\Plugin\CreateConsoleNotFoundModel‘,
    );
    
    /**
     * Default set of plugin aliases
     *
     * @var array
    */
    protected $aliases = array(
        ‘prg‘     => ‘postredirectget‘,
        ‘fileprg‘ => ‘filepostredirectget‘,
    );
    
    public function get($name, $options = array(), $usePeeringServiceManagers = true)
    {
        $plugin = parent::get($name, $options, $usePeeringServiceManagers);
        $this->injectController($plugin); //给控制器插件注入控制器对象
    
        return $plugin;
    }
    
    /**
     * 给控制器插件注入控制器对象
     * @param unknown $plugin
     */
    public function injectController($plugin)
    {
        if (!is_object($plugin)) {
            return;
        }
        if (!method_exists($plugin, ‘setController‘)) {
            return;
        }
    
        $controller = $this->getController();
        if (!$controller instanceof DispatchableInterface) {
            return;
        }
    
        $plugin->setController($controller);
    }
    
    public function getController()
    {
        return $this->controller;
    }
}
// 控制器插件的应用
class Ctrl1Controller extends AbstractActionController
{
    public function testAction()
    {
        // redirect 控制器插件
        // case.0
        $plugin = $this->plugin(‘redirect‘);
        return $plugin->toUrl(‘http://www.baidu.com/‘);
        // case.1
        return $this->redirect()->toUrl(‘http://www.baidu.com/‘);
                
        // forward 控制器插件
        // case.0
        $plugin = $this->plugin(‘forward‘);
        return $plugin->dispatch(‘Module1\Controller\Ctrl2‘,array(‘action‘=>‘method2‘,‘otherparams‘=>‘otherparams_value‘));
        // case.1
        return $this->forward()->dispatch(‘Module1\Controller\Ctrl2‘,array(‘action‘=>‘method2‘,‘otherparams‘=>‘otherparams_value‘));
    }
}
class Ctrl2Controller extends AbstractActionController
{
    public function method2Action()
    {
        
    }
}

 

ZendFramework-2.4 源代码 - 关于MVC - Controller层