📄 viewrenderer.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_Controller * @subpackage Zend_Controller_Action_Helper * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License *//** * @see Zend_Controller_Action_Helper_Abstract */require_once 'Zend/Controller/Action/Helper/Abstract.php';/** * @see Zend_View_Interface */require_once 'Zend/View/Interface.php';/** * @see Zend_View */require_once 'Zend/View.php';/** * View script integration * * Zend_Controller_Action_Helper_ViewRenderer provides transparent view * integration for action controllers. It allows you to create a view object * once, and populate it throughout all actions. Several global options may be * set: * * - noController: if set true, render() will not look for view scripts in * subdirectories named after the controller * - viewSuffix: what view script filename suffix to use * * The helper autoinitializes the action controller view preDispatch(). It * determines the path to the class file, and then determines the view base * directory from there. It also uses the module name as a class prefix for * helpers and views such that if your module name is 'Search', it will set the * helper class prefix to 'Search_View_Helper' and the filter class prefix to ; * 'Search_View_Filter'. * * Usage: * <code> * // In your bootstrap: * Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer()); * * // In your action controller methods: * $viewHelper = $this->_helper->getHelper('view'); * * // Don't use controller subdirectories * $viewHelper->setNoController(true); * * // Specify a different script to render: * $this->_helper->view('form'); * * </code> * * @uses Zend_Controller_Action_Helper_Abstract * @package Zend_Controller * @subpackage Zend_Controller_Action_Helper * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Controller_Action_Helper_ViewRenderer extends Zend_Controller_Action_Helper_Abstract{ /** * @var Zend_View_Interface */ public $view; /** * Word delimiters * @var array */ protected $_delimiters; /** * Front controller instance * @var Zend_Controller_Front */ protected $_frontController; /** * @var Zend_Filter_Inflector */ protected $_inflector; /** * Inflector target * @var string */ protected $_inflectorTarget = ''; /** * Current module directory * @var string */ protected $_moduleDir = ''; /** * Whether or not to autorender using controller name as subdirectory; * global setting (not reset at next invocation) * @var boolean */ protected $_neverController = false; /** * Whether or not to autorender postDispatch; global setting (not reset at * next invocation) * @var boolean */ protected $_neverRender = false; /** * Whether or not to use a controller name as a subdirectory when rendering * @var boolean */ protected $_noController = false; /** * Whether or not to autorender postDispatch; per controller/action setting (reset * at next invocation) * @var boolean */ protected $_noRender = false; /** * Characters representing path delimiters in the controller * @var string|array */ protected $_pathDelimiters; /** * Which named segment of the response to utilize * @var string */ protected $_responseSegment = null; /** * Which action view script to render * @var string */ protected $_scriptAction = null; /** * View object basePath * @var string */ protected $_viewBasePathSpec = ':moduleDir/views'; /** * View script path specification string * @var string */ protected $_viewScriptPathSpec = ':controller/:action.:suffix'; /** * View script path specification string, minus controller segment * @var string */ protected $_viewScriptPathNoControllerSpec = ':action.:suffix'; /** * View script suffix * @var string */ protected $_viewSuffix = 'phtml'; /** * Constructor * * Optionally set view object and options. * * @param Zend_View_Interface $view * @param array $options * @return void */ public function __construct(Zend_View_Interface $view = null, array $options = array()) { if (null !== $view) { $this->setView($view); } if (!empty($options)) { $this->_setOptions($options); } } /** * Set the view object * * @param Zend_View_Interface $view * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface */ public function setView(Zend_View_Interface $view) { $this->view = $view; return $this; } /** * Retrieve front controller instance * * @return Zend_Controller_Front */ public function getFrontController() { if (null === $this->_frontController) { $this->_frontController = Zend_Controller_Front::getInstance(); } return $this->_frontController; } /** * Get current module name * * @return string */ public function getModule() { $request = $this->getRequest(); $module = $request->getModuleName(); if (null === $module) { $module = $this->getFrontController()->getDispatcher()->getDefaultModule(); } return $module; } /** * Get module directory * * @throws Zend_Controller_Action_Exception * @return string */ public function getModuleDirectory() { $module = $this->getModule(); $moduleDir = $this->getFrontController()->getControllerDirectory($module); if ((null === $moduleDir) || is_array($moduleDir)) { /** * @see Zend_Controller_Action_Exception */ throw new Zend_Controller_Action_Exception('ViewRenderer cannot locate module directory'); } $this->_moduleDir = dirname($moduleDir); return $this->_moduleDir; } /** * Get inflector * * @return Zend_Filter_Inflector */ public function getInflector() { if (null === $this->_inflector) { /** * @see Zend_Filter_Inflector */ require_once 'Zend/Filter/Inflector.php'; /** * @see Zend_Filter_PregReplace */ require_once 'Zend/Filter/PregReplace.php'; /** * @see Zend_Filter_Word_UnderscoreToSeparator */ require_once 'Zend/Filter/Word/UnderscoreToSeparator.php'; $this->_inflector = new Zend_Filter_Inflector(); $this->_inflector->setStaticRuleReference('moduleDir', $this->_moduleDir) // moduleDir must be specified before the less specific 'module' ->addRules(array( ':module' => array('Word_CamelCaseToDash', 'StringToLower'), ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator('/'), 'StringToLower'), ':action' => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('#[^a-z0-9' . preg_quote('/', '#') . ']+#i', '-'), 'StringToLower'), )) ->setStaticRuleReference('suffix', $this->_viewSuffix) ->setTargetReference($this->_inflectorTarget); } // Ensure that module directory is current $this->getModuleDirectory(); return $this->_inflector; } /** * Set inflector * * @param Zend_Filter_Inflector $inflector * @param boolean $reference Whether the moduleDir, target, and suffix should be set as references to ViewRenderer properties * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface */ public function setInflector(Zend_Filter_Inflector $inflector, $reference = false) { $this->_inflector = $inflector; if ($reference) { $this->_inflector->setStaticRuleReference('suffix', $this->_viewSuffix) ->setStaticRuleReference('moduleDir', $this->_moduleDir) ->setTargetReference($this->_inflectorTarget); } return $this; } /** * Set inflector target * * @param string $target * @return void */ protected function _setInflectorTarget($target) { $this->_inflectorTarget = (string) $target; } /** * Set internal module directory representation * * @param string $dir * @return void */ protected function _setModuleDir($dir) { $this->_moduleDir = (string) $dir; } /** * Get internal module directory representation * * @return string */ protected function _getModuleDir() { return $this->_moduleDir; } /** * Generate a class prefix for helper and filter classes * * @return string */ protected function _generateDefaultPrefix() { if ((null === $this->_actionController) || !strstr(get_class($this->_actionController), '_')) { $prefix = 'Zend_View'; } else { $class = get_class($this->_actionController); $prefix = substr($class, 0, strpos($class, '_')) . '_View'; } return $prefix; } /** * Retrieve base path based on location of current action controller * * @return string */ protected function _getBasePath() { if (null === $this->_actionController) { return './views'; } $inflector = $this->getInflector(); $this->_setInflectorTarget($this->getViewBasePathSpec()); $dispatcher = $this->_frontController->getDispatcher(); $request = $this->getRequest(); $parts = array( 'module' => (($moduleName = $request->getModuleName()) != '') ? $dispatcher->formatModuleName($moduleName) : $moduleName, 'controller' => substr($dispatcher->formatControllerName($request->getControllerName()), 0, -10), 'action' => $dispatcher->formatActionName($request->getActionName()) ); $path = $inflector->filter($parts); return $path; } /** * Set options * * @param array $options * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface */ protected function _setOptions(array $options) { foreach ($options as $key => $value) { switch ($key) { case 'neverRender': case 'neverController': case 'noController': case 'noRender': $property = '_' . $key; $this->{$property} = ($value) ? true : false; break; case 'responseSegment': case 'scriptAction': case 'viewBasePathSpec': case 'viewScriptPathSpec': case 'viewScriptPathNoControllerSpec': case 'viewSuffix': $property = '_' . $key; $this->{$property} = (string) $value; break; default: break; } } return $this; } /** * Initialize the view object * * $options may contain the following keys: * - neverRender - flag dis/enabling postDispatch() autorender (affects all subsequent calls) * - noController - flag indicating whether or not to look for view scripts in subdirectories named after the controller * - noRender - flag indicating whether or not to autorender postDispatch() * - responseSegment - which named response segment to render a view script to * - scriptAction - what action script to render * - viewBasePathSpec - specification to use for determining view base path * - viewScriptPathSpec - specification to use for determining view script paths * - viewScriptPathNoControllerSpec - specification to use for determining view script paths when noController flag is set * - viewSuffix - what view script filename suffix to use * * @param string $path * @param string $prefix * @param array $options * @throws Zend_Controller_Action_Exception * @return void */ public function initView($path = null, $prefix = null, array $options = array()) { if (null === $this->view) { $this->setView(new Zend_View()); } // Reset some flags every time $options['noController'] = (isset($options['noController'])) ? $options['noController'] : false; $options['noRender'] = (isset($options['noRender'])) ? $options['noRender'] : false; $this->_scriptAction = null; $this->_responseSegment = null; // Set options first; may be used to determine other initializations $this->_setOptions($options); // Get base view path if (empty($path)) { $path = $this->_getBasePath(); if (empty($path)) { /** * @see Zend_Controller_Action_Exception */ throw new Zend_Controller_Action_Exception('ViewRenderer initialization failed: retrieved view base path is empty'); } } if (null === $prefix) { $prefix = $this->_generateDefaultPrefix(); } // Determine if this path has already been registered $currentPaths = $this->view->getScriptPaths(); $path = str_replace(array('/', '\\'), '/', $path); $pathExists = false; foreach ($currentPaths as $tmpPath) { $tmpPath = str_replace(array('/', '\\'), '/', $tmpPath); if (strstr($tmpPath, $path)) { $pathExists = true; break; } } if (!$pathExists) { $this->view->addBasePath($path, $prefix); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -