Zend_Controller_Front->dispatch 注解

本文介绍Zend框架中的调度过程,包括初始化、路由、插件注册、请求与响应对象配置等关键步骤,并详细解释了如何通过Zend_Controller_Front进行调度。
Zend_Controller_Front
public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
{

//    注册出错的插件

    
if (!$this->getParam('noErrorHandler'&& !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) {
        
// Register with stack index of 100 该插件存放在序号是 100的位置
        $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
    }

//    添加视图渲染助手
    if (!$this->getParam('noViewRenderer'&& !Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
        Zend_Controller_Action_HelperBroker
::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
    }

//设置请求对象 如果没提供则实例化默认http方式的请求对象(该对象封装了 module  controller  action 对应的名字 及对$_GET  $_POST )
    /**
     * Instantiate default request object (HTTP version) if none provided
     
*/
    
if (null !== $request) {
        
$this->setRequest($request);
    } 
elseif ((null === $request&& (null === ($request = $this->getRequest()))) {
        
require_once 'Zend/Controller/Request/Http.php';
        
$request = new Zend_Controller_Request_Http();
        
$this->setRequest($request);
    }

//重新设置 BaseUrl  通过调用 request对象的 setBaseUrl
        /**
         * Set base URL of request object, if available
         
*/
        
if (is_callable(array($this->_request, 'setBaseUrl'))) {
            
if (null !== ($baseUrl = $this->getBaseUrl())) {    //存在 $baseUrl 时才执行
                $this->_request->setBaseUrl($baseUrl);
            }
        }

//设置 响应对象 如果没提供则实例化默认http方式的响应对象 (该对象处理服务端向客户端的响应 比如 显示网页内容)
        /**
         * Instantiate default response object (HTTP version) if none provided
         
*/
        
if (null !== $response) {
            
$this->setResponse($response);
        } 
elseif ((null === $this->_response) && (null === ($this->_response = $this->getResponse()))) {
            
require_once 'Zend/Controller/Response/Http.php';
            
$response = new Zend_Controller_Response_Http();
            
$this->setResponse($response);
        }

//把 request 和 response 对象保存在 提供的plugin 中
        /**
         * Register request and response objects with plugin broker
         
*/
        
$this->_plugins
             
->setRequest($this->_request)
             
->setResponse($this->_response);


//初始化路由
        /**
         * Initialize router
         
*/
        
$router = $this->getRouter();    //这里是采用 Zend_Controller_Router_Rewrite 
        $router->setParams($this->getParams());    //传递引用的参数到路由

//初始化调配器

        /**
         * Initialize dispatcher
         
*/
        
$dispatcher = $this->getDispatcher();        //这里是采用 Zend_Controller_Dispatcher_Standard
        $dispatcher->setParams($this->getParams())
                   
->setResponse($this->_response);    //保存 Response 在 dispatcher 因为在 Zend_Controller_Action 中用到

    //开始 调配过程
       // Begin dispatch

        try {
            
/**
             * Route request to controller/action, if a router is provided
             
*/

            
/**
            * Notify plugins of router startup        通知各个插件 路由启动
            
*/
            
$this->_plugins->routeStartup($this->_request);

            
$router->route($this->_request);        //启动路由 得到了 module  controller  action 值

            
/**
            * Notify plugins of router completion    通知各个插件 路由结束
            
*/
            
$this->_plugins->routeShutdown($this->_request);

            
/**
             * Notify plugins of dispatch loop startup        通知各个插件 调配 循环开始
             
*/
            
$this->_plugins->dispatchLoopStartup($this->_request);

           
/**
             *  Attempt to dispatch the controller/action. If the $this->_request
             *  indicates that it needs to be dispatched, move to the next
             *  action in the request.
             
*/
         
//根据当前的 controller/action 尝试进行调配  对于Request 指定 dispatched 的请求 跳过 
            do {
                
$this->_request->setDispatched(true);

                
/**
                 * Notify plugins of dispatch startup     通知插件 开始调配
                 
*/
                
$this->_plugins->preDispatch($this->_request);

                
/**
                 * Skip requested action if preDispatch() has reset it 对于Request 指定 dispatched 的请求 跳过 
                 
*/
                
if (!$this->_request->isDispatched()) {
                    
continue;
                }

                
/**
                 * Dispatch request    调用 Zend_Controller_Dispatcher_Standard-dispatch()
                 
*/
                
try {
                    
$dispatcher->dispatch($this->_request, $this->_response);
                } 
catch (Exception $e) {
                    
if ($this->throwExceptions()) {
                        
throw $e;
                    }
                    
$this->_response->setException($e);
                }
                
/**
                 * Notify plugins of dispatch completion    调用 Zend_Controller_Plugin_Broker->postDispatch 通知所有插件完成当前调配
                 
*/
                
$this->_plugins->postDispatch($this->_request);
            } 
while (!$this->_request->isDispatched());
        } 
catch (Exception $e) {
            
if ($this->throwExceptions()) {
                
throw $e;
            }

            
$this->_response->setException($e);
        }


        
/**
         * Notify plugins of dispatch loop completion 通知所有插件完成所有调配
         
*/
        
try {
            
$this->_plugins->dispatchLoopShutdown();
        } 
catch (Exception $e) {
            
if ($this->throwExceptions()) {
                
throw $e;
            }

            
$this->_response->setException($e);
        }

        
if ($this->returnResponse()) {    //如果设制了 返回响应
            return $this->_response;
        }

        
$this->_response->sendResponse();    //发送响应到客户端

}
 
80 /** 1681 * bioset_init - Initialize a bio_set 1682 * @bs: pool to initialize 1683 * @pool_size: Number of bio and bio_vecs to cache in the mempool 1684 * @front_pad: Number of bytes to allocate in front of the returned bio 1685 * @flags: Flags to modify behavior, currently %BIOSET_NEED_BVECS 1686 * and %BIOSET_NEED_RESCUER 1687 * 1688 * Description: 1689 * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller 1690 * to ask for a number of bytes to be allocated in front of the bio. 1691 * Front pad allocation is useful for embedding the bio inside 1692 * another structure, to avoid allocating extra data to go with the bio. 1693 * Note that the bio must be embedded at the END of that structure always, 1694 * or things will break badly. 1695 * If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated 1696 * for allocating iovecs. This pool is not needed e.g. for bio_init_clone(). 1697 * If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used 1698 * to dispatch queued requests when the mempool runs out of space. 1699 * 1700 */ 1701 int bioset_init(struct bio_set *bs, 1702 unsigned int pool_size, 1703 unsigned int front_pad, 1704 int flags) 1705 { 1706 bs->front_pad = front_pad; 1707 if (flags & BIOSET_NEED_BVECS) 1708 bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec); 1709 else 1710 bs->back_pad = 0; 1711 1712 spin_lock_init(&bs->rescue_lock); 1713 bio_list_init(&bs->rescue_list); 1714 INIT_WORK(&bs->rescue_work, bio_alloc_rescue); 1715 1716 bs->bio_slab = bio_find_or_create_slab(bs); 1717 if (!bs->bio_slab) 1718 return -ENOMEM; 1719 1720 if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab)) 1721 goto bad; 1722 1723 if ((flags & BIOSET_NEED_BVECS) && 1724 biovec_init_pool(&bs->bvec_pool, pool_size)) 1725 goto bad; 1726 1727 if (flags & BIOSET_NEED_RESCUER) { 1728 bs->rescue_workqueue = alloc_workqueue("bioset", 1729 WQ_MEM_RECLAIM, 0); 1730 if (!bs->rescue_workqueue) 1731 goto bad; 1732 } 1733 if (flags & BIOSET_PERCPU_CACHE) { 1734 bs->cache = alloc_percpu(struct bio_alloc_cache); 1735 if (!bs->cache) 1736 goto bad; 1737 cpuhp_state_add_instance_nocalls(CPUHP_BIO_DEAD, &bs->cpuhp_dead); 1738 } 1739 1740 return 0; 1741 bad: 1742 bioset_exit(bs); 1743 return -ENOMEM; 1744 } 1745 EXPORT_SYMBOL(bioset_init);
最新发布
11-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值