⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contextswitch.php

📁 zend的加强包 zend的加强包
💻 PHP
📖 第 1 页 / 共 3 页
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to version 1.0 of the Zend Framework * license, that is bundled with this package in the file LICENSE.txt, and * is available through the world-wide-web at the following URL: * http://framework.zend.com/license/new-bsd. If you did not receive * a copy of the Zend Framework license and are unable to obtain it * through the world-wide-web, please send a note to license@zend.com * so we can mail 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) * @version    $Id: ContextSwitch.php 8972 2008-03-21 18:48:44Z thomas $ * @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';/** * Simplify context switching based on requested format * * @uses       Zend_Controller_Action_Helper_Abstract * @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 */class Zend_Controller_Action_Helper_ContextSwitch extends Zend_Controller_Action_Helper_Abstract{    /**     * Trigger type constants     */    const TRIGGER_INIT = 'TRIGGER_INIT';    const TRIGGER_POST = 'TRIGGER_POST';    /**     * Supported contexts     * @var array     */    protected $_contexts = array();    /**     * JSON auto-serialization flag     * @var boolean     */    protected $_autoJsonSerialization = true;    /**     * Controller property key to utilize for context switching     * @var string     */    protected $_contextKey = 'contexts';    /**     * Request parameter containing requested context     * @var string     */    protected $_contextParam = 'format';    /**     * Current context     * @var string     */    protected $_currentContext;     /**     * Default context (xml)     * @var string     */    protected $_defaultContext = 'xml';      /**     * Whether or not to disable layouts when switching contexts     * @var boolean     */    protected $_disableLayout = true;     /**     * Methods that require special configuration     * @var array     */    protected $_specialConfig = array(        'setSuffix',        'setHeaders',        'setCallbacks',    );    /**     * Methods that are not configurable via setOptions and setConfig     * @var array     */    protected $_unconfigurable = array(        'setOptions',        'setConfig',        'setHeader',        'setCallback',        'setContext',        'setActionContext',        'setActionContexts',    );    /**     * @var Zend_Controller_Action_Helper_ViewRenderer     */    protected $_viewRenderer;    /**     * Constructor     *      * @param  array|Zend_Config $options      * @return void     */    public function __construct($options = null)    {        if (empty($this->_contexts)) {            $this->addContexts(array(                'json' => array(                    'suffix'    => 'json',                    'headers'   => array('Content-Type' => 'application/json'),                    'callbacks' => array(                        'init' => 'initJsonContext',                        'post' => 'postJsonContext'                    )                ),                'xml'  => array(                    'suffix'    => 'xml',                    'headers'   => array('Content-Type' => 'text/xml'),                )            ));        }    }    /**     * Configure object from array of options     *      * @param  array $options      * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface     */    public function setOptions(array $options)    {        if (isset($options['contexts'])) {            $this->setContexts($options['contexts']);            unset($options['contexts']);        }        foreach ($options as $key => $value) {            $method = 'set' . ucfirst($key);            if (in_array($method, $this->_unconfigurable)) {                continue;            }            if (in_array($method, $this->_specialConfig)) {                $method = '_' . $method;            }            if (method_exists($this, $method)) {                $this->$method($value);            }        }        return $this;    }    /**     * Set object state from config object     *      * @param  Zend_Config $config      * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface     */    public function setConfig(Zend_Config $config)    {        return $this->setOptions($config->toArray());    }    /**     * Strategy pattern: return object     *      * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface     */    public function direct()    {        return $this;    }     /**     * Initialize context detection and switching     *     * @param  mixed $format     * @throws Zend_Controller_Action_Exception     * @return void     */     public function initContext($format = null)    {        $this->_currentContext = null;        $controller = $this->getActionController();        $request    = $this->getRequest();        $action     = $request->getActionName();        // Return if no context switching enabled, or no context switching         // enabled for this action        $contexts = $this->getActionContexts($action);        if (empty($contexts)) {            return;        }        // Return if no context parameter provided        if (!$context = $request->getParam($this->getContextParam())) {            if ($format === null) {                return;            }            $context = $format;            $format  = null;        }        // Check if context allowed by action controller        if (!$this->hasActionContext($action, $context)) {            return;        }        // Return if invalid context parameter provided and no format or invalid         // format provided        if (!$this->hasContext($context)) {            if (empty($format) || !$this->hasContext($format)) {                return;            }        }        // Use provided format if passed        if (!empty($format) && $this->hasContext($format)) {            $context = $format;        }        $suffix = $this->getSuffix($context);        $this->_getViewRenderer()->setViewSuffix($suffix);        $headers = $this->getHeaders($context);        if (!empty($headers)) {            $response = $this->getResponse();            foreach ($headers as $header => $content) {                $response->setHeader($header, $content);            }        }        if ($this->getAutoDisableLayout()) {            /**             * @see Zend_Layout             */            require_once 'Zend/Layout.php';            $layout = Zend_Layout::getMvcInstance();            if (null !== $layout) {                $layout->disableLayout();            }        }        if (null !== ($callback = $this->getCallback($context, self::TRIGGER_INIT))) {            if (is_string($callback) && method_exists($this, $callback)) {                $this->$callback();            } elseif (is_string($callback) && function_exists($callback)) {                $callback();            } elseif (is_array($callback)) {                call_user_func($callback);            } else {                /**                 * @see Zend_Controller_Action_Exception                 */                require_once 'Zend/Controller/Action/Exception.php';                throw new Zend_Controller_Action_Exception(sprintf('Invalid context callback registered for context "%s"', $context));            }        }        $this->_currentContext = $context;    }    /**     * JSON context extra initialization     *     * Turns off viewRenderer auto-rendering     *      * @return void     */    public function initJsonContext()    {        if (!$this->getAutoJsonSerialization()) {            return;        }        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');        $view = $viewRenderer->view;        if ($view instanceof Zend_View_Interface) {            $viewRenderer->setNoRender(true);        }    }    /**     * Should JSON contexts auto-serialize?     *      * @param  boolean $flag      * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface     */    public function setAutoJsonSerialization($flag)    {        $this->_autoJsonSerialization = (bool) $flag;        return $this;    }    /**     * Get JSON context auto-serialization flag     *      * @return boolean     */    public function getAutoJsonSerialization()    {        return $this->_autoJsonSerialization;    }    /**     * Set suffix from array     *      * @param  array $spec      * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface     */    protected function _setSuffix(array $spec)    {        foreach ($spec as $context => $suffixInfo) {            if (!is_string($context)) {                $context = null;            }            if (is_string($suffixInfo)) {                $this->setSuffix($context, $suffixInfo);                continue;            } elseif (is_array($suffixInfo)) {                if (isset($suffixInfo['suffix'])) {                    $suffix                    = $suffixInfo['suffix'];                    $prependViewRendererSuffix = true;                    if ((null === $context) && isset($suffixInfo['context'])) {                        $context = $suffixInfo['context'];                    }                    if (isset($suffixInfo['prependViewRendererSuffix'])) {                        $prependViewRendererSuffix = $suffixInfo['prependViewRendererSuffix'];                    }                    $this->setSuffix($context, $suffix, $prependViewRendererSuffix);                    continue;                }                $count = count($suffixInfo);                switch (true) {                    case (($count < 2) && (null === $context)):                        /**                         * @see Zend_Controller_Action_Exception                         */                        require_once 'Zend/Controller/Action/Exception.php';                        throw new Zend_Controller_Action_Exception('Invalid suffix information provided in config');                    case ($count < 2):                        $suffix = array_shift($suffixInfo);                        $this->setSuffix($context, $suffix);                        break;                    case (($count < 3) && (null === $context)):                        $context = array_shift($suffixInfo);                        $suffix  = array_shift($suffixInfo);                        $this->setSuffix($context, $suffix);                        break;                    case (($count == 3) && (null === $context)):                        $context = array_shift($suffixInfo);                        $suffix  = array_shift($suffixInfo);                        $prependViewRendererSuffix = array_shift($suffixInfo);                        $this->setSuffix($context, $suffix, $prependViewRendererSuffix);                        break;                    case ($count >= 2):                        $suffix  = array_shift($suffixInfo);                        $prependViewRendererSuffix = array_shift($suffixInfo);                        $this->setSuffix($context, $suffix, $prependViewRendererSuffix);                        break;                }            }        }        return $this;    }     /**     * Customize view script suffix to use when switching context.     *      * Passing an empty suffix value to the setters disables the view script     * suffix change.     *     * @param  string  $context                   Context type for which to set suffix     * @param  string  $suffix                    Suffix to use     * @param  boolean $prependViewRendererSuffix Whether or not to prepend the new suffix to the viewrenderer suffix     * @throws Zend_Controller_Action_Exception     * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface     */     public function setSuffix($context, $suffix, $prependViewRendererSuffix = true)    {        if (!isset($this->_contexts[$context])) {            /**             * @see Zend_Controller_Action_Exception             */            require_once 'Zend/Controller/Action/Exception.php';            throw new Zend_Controller_Action_Exception(sprintf('Cannot set suffix; invalid context type "%s"', $context));        }        if (empty($suffix)) {            $suffix = '';        }        if (is_array($suffix)) {            if (isset($suffix['prependViewRendererSuffix'])) {                $prependViewRendererSuffix = $suffix['prependViewRendererSuffix'];            }            if (isset($suffix['suffix'])) {                $suffix = $suffix['suffix'];            } else {                $suffix = '';            }        }        $suffix = (string) $suffix;        if ($prependViewRendererSuffix) {            if (empty($suffix)) {                $suffix = $this->_getViewRenderer()->getViewSuffix();            } else {                $suffix .= '.' . $this->_getViewRenderer()->getViewSuffix();            }        }        $this->_contexts[$context]['suffix'] = $suffix;        return $this;    }    /**     * Retrieve suffix for given context type     *      * @param  string $type Context type     * @throws Zend_Controller_Action_Exception     * @return string     */    public function getSuffix($type)    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -