web/lib/Zend/View/Helper/Action.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_View
       
    17  * @subpackage Helper
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @version    $Id: Action.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 /** Zend_View_Helper_Abstract.php */
       
    24 require_once 'Zend/View/Helper/Abstract.php';
       
    25 
       
    26 /**
       
    27  * Helper for rendering output of a controller action
       
    28  *
       
    29  * @package    Zend_View
       
    30  * @subpackage Helper
       
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  */
       
    34 class Zend_View_Helper_Action extends Zend_View_Helper_Abstract
       
    35 {
       
    36     /**
       
    37      * @var string
       
    38      */
       
    39     public $defaultModule;
       
    40 
       
    41     /**
       
    42      * @var Zend_Controller_Dispatcher_Interface
       
    43      */
       
    44     public $dispatcher;
       
    45 
       
    46     /**
       
    47      * @var Zend_Controller_Request_Abstract
       
    48      */
       
    49     public $request;
       
    50 
       
    51     /**
       
    52      * @var Zend_Controller_Response_Abstract
       
    53      */
       
    54     public $response;
       
    55 
       
    56     /**
       
    57      * Constructor
       
    58      *
       
    59      * Grab local copies of various MVC objects
       
    60      *
       
    61      * @return void
       
    62      */
       
    63     public function __construct()
       
    64     {
       
    65         $front   = Zend_Controller_Front::getInstance();
       
    66         $modules = $front->getControllerDirectory();
       
    67         if (empty($modules)) {
       
    68             require_once 'Zend/View/Exception.php';
       
    69             $e = new Zend_View_Exception('Action helper depends on valid front controller instance');
       
    70             $e->setView($this->view);
       
    71             throw $e;
       
    72         }
       
    73 
       
    74         $request  = $front->getRequest();
       
    75         $response = $front->getResponse();
       
    76 
       
    77         if (empty($request) || empty($response)) {
       
    78             require_once 'Zend/View/Exception.php';
       
    79             $e = new Zend_View_Exception('Action view helper requires both a registered request and response object in the front controller instance');
       
    80             $e->setView($this->view);
       
    81             throw $e;
       
    82         }
       
    83 
       
    84         $this->request       = clone $request;
       
    85         $this->response      = clone $response;
       
    86         $this->dispatcher    = clone $front->getDispatcher();
       
    87         $this->defaultModule = $front->getDefaultModule();
       
    88     }
       
    89 
       
    90     /**
       
    91      * Reset object states
       
    92      *
       
    93      * @return void
       
    94      */
       
    95     public function resetObjects()
       
    96     {
       
    97         $params = $this->request->getUserParams();
       
    98         foreach (array_keys($params) as $key) {
       
    99             $this->request->setParam($key, null);
       
   100         }
       
   101 
       
   102         $this->response->clearBody();
       
   103         $this->response->clearHeaders()
       
   104                        ->clearRawHeaders();
       
   105     }
       
   106 
       
   107     /**
       
   108      * Retrieve rendered contents of a controller action
       
   109      *
       
   110      * If the action results in a forward or redirect, returns empty string.
       
   111      *
       
   112      * @param  string $action
       
   113      * @param  string $controller
       
   114      * @param  string $module Defaults to default module
       
   115      * @param  array $params
       
   116      * @return string
       
   117      */
       
   118     public function action($action, $controller, $module = null, array $params = array())
       
   119     {
       
   120         $this->resetObjects();
       
   121         if (null === $module) {
       
   122             $module = $this->defaultModule;
       
   123         }
       
   124 
       
   125         // clone the view object to prevent over-writing of view variables
       
   126         $viewRendererObj = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
       
   127         Zend_Controller_Action_HelperBroker::addHelper(clone $viewRendererObj);
       
   128 
       
   129         $this->request->setParams($params)
       
   130                       ->setModuleName($module)
       
   131                       ->setControllerName($controller)
       
   132                       ->setActionName($action)
       
   133                       ->setDispatched(true);
       
   134 
       
   135         $this->dispatcher->dispatch($this->request, $this->response);
       
   136 
       
   137         // reset the viewRenderer object to it's original state
       
   138         Zend_Controller_Action_HelperBroker::addHelper($viewRendererObj);
       
   139 
       
   140 
       
   141         if (!$this->request->isDispatched()
       
   142             || $this->response->isRedirect())
       
   143         {
       
   144             // forwards and redirects render nothing
       
   145             return '';
       
   146         }
       
   147 
       
   148         $return = $this->response->getBody();
       
   149         $this->resetObjects();
       
   150         return $return;
       
   151     }
       
   152 
       
   153     /**
       
   154      * Clone the current View
       
   155      *
       
   156      * @return Zend_View_Interface
       
   157      */
       
   158     public function cloneView()
       
   159     {
       
   160         $view = clone $this->view;
       
   161         $view->clearVars();
       
   162         return $view;
       
   163     }
       
   164 }