File indexing completed on 2024-12-22 05:37:13

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_View
0017  * @subpackage Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @version    $Id$
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** Zend_View_Helper_Abstract.php */
0024 // require_once 'Zend/View/Helper/Abstract.php';
0025 
0026 /**
0027  * Helper for rendering output of a controller action
0028  *
0029  * @package    Zend_View
0030  * @subpackage Helper
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_View_Helper_Action extends Zend_View_Helper_Abstract
0035 {
0036     /**
0037      * @var string
0038      */
0039     public $defaultModule;
0040 
0041     /**
0042      * @var Zend_Controller_Dispatcher_Interface
0043      */
0044     public $dispatcher;
0045 
0046     /**
0047      * @var Zend_Controller_Request_Abstract
0048      */
0049     public $request;
0050 
0051     /**
0052      * @var Zend_Controller_Response_Abstract
0053      */
0054     public $response;
0055 
0056     /**
0057      * Constructor
0058      *
0059      * Grab local copies of various MVC objects
0060      *
0061      * @return void
0062      */
0063     public function __construct()
0064     {
0065         $front   = Zend_Controller_Front::getInstance();
0066         $modules = $front->getControllerDirectory();
0067         if (empty($modules)) {
0068             // require_once 'Zend/View/Exception.php';
0069             $e = new Zend_View_Exception('Action helper depends on valid front controller instance');
0070             $e->setView($this->view);
0071             throw $e;
0072         }
0073 
0074         $request  = $front->getRequest();
0075         $response = $front->getResponse();
0076 
0077         if (empty($request) || empty($response)) {
0078             // require_once 'Zend/View/Exception.php';
0079             $e = new Zend_View_Exception('Action view helper requires both a registered request and response object in the front controller instance');
0080             $e->setView($this->view);
0081             throw $e;
0082         }
0083 
0084         $this->request       = clone $request;
0085         $this->response      = clone $response;
0086         $this->dispatcher    = clone $front->getDispatcher();
0087         $this->defaultModule = $front->getDefaultModule();
0088     }
0089 
0090     /**
0091      * Reset object states
0092      *
0093      * @return void
0094      */
0095     public function resetObjects()
0096     {
0097         $params = $this->request->getUserParams();
0098         foreach (array_keys($params) as $key) {
0099             $this->request->setParam($key, null);
0100         }
0101 
0102         $this->response->clearBody();
0103         $this->response->clearHeaders()
0104                        ->clearRawHeaders();
0105     }
0106 
0107     /**
0108      * Retrieve rendered contents of a controller action
0109      *
0110      * If the action results in a forward or redirect, returns empty string.
0111      *
0112      * @param  string $action
0113      * @param  string $controller
0114      * @param  string $module Defaults to default module
0115      * @param  array $params
0116      * @return string
0117      */
0118     public function action($action, $controller, $module = null, array $params = array())
0119     {
0120         $this->resetObjects();
0121         if (null === $module) {
0122             $module = $this->defaultModule;
0123         }
0124 
0125         // clone the view object to prevent over-writing of view variables
0126         $viewRendererObj = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
0127         Zend_Controller_Action_HelperBroker::addHelper(clone $viewRendererObj);
0128 
0129         $this->request->setParams($params)
0130                       ->setModuleName($module)
0131                       ->setControllerName($controller)
0132                       ->setActionName($action)
0133                       ->setDispatched(true);
0134 
0135         $this->dispatcher->dispatch($this->request, $this->response);
0136 
0137         // reset the viewRenderer object to it's original state
0138         Zend_Controller_Action_HelperBroker::addHelper($viewRendererObj);
0139 
0140 
0141         if (!$this->request->isDispatched()
0142             || $this->response->isRedirect())
0143         {
0144             // forwards and redirects render nothing
0145             return '';
0146         }
0147 
0148         $return = $this->response->getBody();
0149         $this->resetObjects();
0150         return $return;
0151     }
0152 
0153     /**
0154      * Clone the current View
0155      *
0156      * @return Zend_View_Interface
0157      */
0158     public function cloneView()
0159     {
0160         $view = clone $this->view;
0161         $view->clearVars();
0162         return $view;
0163     }
0164 }