📄 contextswitch.php
字号:
} /** * Add new context * * @param string $context Context type * @param array $spec Context specification * @throws Zend_Controller_Action_Exception * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface */ public function addContext($context, array $spec) { if ($this->hasContext($context)) { /** * @see Zend_Controller_Action_Exception */ require_once 'Zend/Controller/Action/Exception.php'; throw new Zend_Controller_Action_Exception(sprintf('Cannot add context "%s"; already exists', $context)); } $context = (string) $context; $this->_contexts[$context] = array(); $this->setSuffix($context, (isset($spec['suffix']) ? $spec['suffix'] : '')) ->setHeaders($context, (isset($spec['headers']) ? $spec['headers'] : array())) ->setCallbacks($context, (isset($spec['callbacks']) ? $spec['callbacks'] : array())); return $this; } /** * Overwrite existing context * * @param string $context Context type * @param array $spec Context specification * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface */ public function setContext($context, array $spec) { $this->removeContext($context); return $this->addContext($context, $spec); } /** * Add multiple contexts * * @param array $contexts * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface */ public function addContexts(array $contexts) { foreach ($contexts as $context => $spec) { $this->addContext($context, $spec); } return $this; } /** * Set multiple contexts, after first removing all * * @param array $contexts * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface */ public function setContexts(array $contexts) { $this->clearContexts(); foreach ($contexts as $context => $spec) { $this->addContext($context, $spec); } return $this; } /** * Retrieve context specification * * @param string $context * @return array|null */ public function getContext($context) { if ($this->hasContext($context)) { return $this->_contexts[(string) $context]; } return null; } /** * Retrieve context definitions * * @return array */ public function getContexts() { return $this->_contexts; } /** * Remove a context * * @param string $context * @return boolean */ public function removeContext($context) { if ($this->hasContext($context)) { unset($this->_contexts[(string) $context]); return true; } return false; } /** * Remove all contexts * * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface */ public function clearContexts() { $this->_contexts = array(); return $this; } /** * Return current context, if any * * @return null|string */ public function getCurrentContext() { return $this->_currentContext; } /** * Post dispatch processing * * Execute postDispatch callback for current context, if available * * @throws Zend_Controller_Action_Exception * @return void */ public function postDispatch() { $context = $this->getCurrentContext(); if (null !== $context) { if (null !== ($callback = $this->getCallback($context, self::TRIGGER_POST))) { 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 postDispatch context callback registered for context "%s"', $context)); } } } } /** * JSON post processing * * JSON serialize view variables to response body * * @return void */ public function postJsonContext() { if (!$this->getAutoJsonSerialization()) { return; } $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); $view = $viewRenderer->view; if ($view instanceof Zend_View_Interface) { /** * @see Zend_Json */ require_once 'Zend/Json.php'; $vars = Zend_Json::encode($view->getVars()); $this->getResponse()->setBody($vars); } } /** * Add one or more contexts to an action * * @param string $action * @param string|array $context * @return Zend_Controller_Action_Helper_ContextSwitch|void Provides a fluent interface */ public function addActionContext($action, $context) { $this->hasContext($context, true); $controller = $this->getActionController(); if (null === $controller) { return; } $action = (string) $action; $contextKey = $this->_contextKey; if (!isset($controller->$contextKey)) { $controller->$contextKey = array(); } if (true === $context) { $contexts = $this->getContexts(); $controller->{$contextKey}[$action] = array_keys($contexts); return $this; } $context = (array) $context; if (!isset($controller->{$contextKey}[$action])) { $controller->{$contextKey}[$action] = $context; } else { $controller->{$contextKey}[$action] = array_merge( $controller->{$contextKey}[$action], $context ); } return $this; } /** * Set a context as available for a given controller action * * @param string $action * @param string|array $context * @return Zend_Controller_Action_Helper_ContextSwitch|void Provides a fluent interface */ public function setActionContext($action, $context) { $this->hasContext($context, true); $controller = $this->getActionController(); if (null === $controller) { return; } $action = (string) $action; $contextKey = $this->_contextKey; if (!isset($controller->$contextKey)) { $controller->$contextKey = array(); } if (true === $context) { $contexts = $this->getContexts(); $controller->{$contextKey}[$action] = array_keys($contexts); } else { $controller->{$contextKey}[$action] = (array) $context; } return $this; } /** * Add multiple action/context pairs at once * * @param array $contexts * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface */ public function addActionContexts(array $contexts) { foreach ($contexts as $action => $context) { $this->addActionContext($action, $context); } return $this; } /** * Overwrite and set multiple action contexts at once * * @param array $contexts * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface */ public function setActionContexts(array $contexts) { foreach ($contexts as $action => $context) { $this->setActionContext($action, $context); } return $this; } /** * Does a particular controller action have the given context(s)? * * @param string $action * @param string|array $context * @throws Zend_Controller_Action_Exception * @return boolean */ public function hasActionContext($action, $context) { $this->hasContext($context, true); $controller = $this->getActionController(); if (null === $controller) { return false; } $action = (string) $action; $contextKey = $this->_contextKey; if (!isset($controller->{$contextKey})) { return false; } $allContexts = $controller->{$contextKey}; if (!is_array($allContexts)) { /** * @see Zend_Controller_Action_Exception */ require_once 'Zend/Controller/Action/Exception.php'; throw new Zend_Controller_Action_Exception("Invalid contexts found for controller"); } if (!isset($allContexts[$action])) { return false; } if (true === $allContexts[$action]) { return true; } $contexts = $allContexts[$action]; if (!is_array($contexts)) { /** * @see Zend_Controller_Action_Exception */ require_once 'Zend/Controller/Action/Exception.php'; throw new Zend_Controller_Action_Exception(sprintf("Invalid contexts found for action '%s'", $action)); } if (is_string($context) && in_array($context, $contexts)) { return true; } elseif (is_array($context)) { $found = true; foreach ($context as $test) { if (!in_array($test, $contexts)) { $found = false; break; } } return $found; } return false; } /** * Get contexts for a given action or all actions in the controller * * @param string $action * @return array */ public function getActionContexts($action = null) { $controller = $this->getActionController(); if (null === $controller) { return array(); } $action = (string) $action; $contextKey = $this->_contextKey; if (!isset($controller->$contextKey)) { return array(); } if (null !== $action) { if (isset($controller->{$contextKey}[$action])) { return $controller->{$contextKey}[$action]; } else { return array(); } } return $controller->$contextKey; } /** * Remove one or more contexts for a given controller action * * @param string $action * @param string|array $context * @return boolean */ public function removeActionContext($action, $context) { if ($this->hasActionContext($action, $context)) { $controller = $this->getActionController(); $contextKey = $this->_contextKey; $action = (string) $action; $contexts = $controller->$contextKey; $actionContexts = $contexts[$action]; $contexts = (array) $context; foreach ($contexts as $context) { $index = array_search($context, $actionContexts); if (false !== $index) { unset($controller->{$contextKey}[$action][$index]); } } return true; } return false; } /** * Clear all contexts for a given controller action or all actions * * @param string $action * @return Zend_Controller_Action_Helper_ContextSwitch Provides a fluent interface */ public function clearActionContexts($action = null) { $controller = $this->getActionController(); $contextKey = $this->_contextKey; if (!isset($controller->$contextKey) || empty($controller->$contextKey)) { return $this; } if (null === $action) { $controller->$contextKey = array(); return $this; } $action = (string) $action; if (isset($controller->{$contextKey}[$action])) { unset($controller->{$contextKey}[$action]); } return $this; } /** * Retrieve ViewRenderer * * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface */ protected function _getViewRenderer() { if (null === $this->_viewRenderer) { $this->_viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); } return $this->_viewRenderer; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -