Perforce Chronicle 2012.2/486814
API Documentation
|
Enhances ContextSwitch to automatically initialize context if contexts are defined in the action controller and initContext has not been called explicitly. More...
Public Member Functions | |
hasActionContext ($action, $context) | |
Extend hasActionContext to be aware of request method limitations. | |
initContext ($format=null) | |
Extend init context to disable auto-init when called. | |
preDispatch () | |
Automatically initialize context prior to dispatch. | |
Protected Attributes | |
$_autoInit = true | |
$_complexContextKey = 'contexts' | |
$_contextKey = 'normalizedContexts' |
Enhances ContextSwitch to automatically initialize context if contexts are defined in the action controller and initContext has not been called explicitly.
Automatically adds unknown contexts using the context name as the view script suffix (ie. no need to pre-declare formats, just use them).
In addition, allows controller to specify which request methods a given context is valid for by expanding the contexts definition to accommodate a list of http request methods for each context. For example:
$contexts = array( 'action-one' => array('json'), 'action-two' => array('json' => 'post'), 'action-three' => array('json' => array('post', 'put') );
The above contexts definition indicates that action-one supports the json context for all request methods; whereas 'action-two' only supports json for http post requests and 'action-three' supports json for post and put.
P4Cms_Controller_Action_Helper_ContextSwitch::hasActionContext | ( | $ | action, |
$ | context | ||
) |
Extend hasActionContext to be aware of request method limitations.
string | $action | Action to evaluate. |
string | array | $context | Context to evaluate. |
Zend_Controller_Action_Exception |
{ $result = parent::hasActionContext($action, $context); // if action doesn't have this context, exit early. if (!$result) { return false; } // ensure that context is valid for the current request method. $contextKey = $this->_complexContextKey; $contexts = $this->getActionController()->$contextKey; // if action contexts is true, request method doesn't matter. if ($contexts[$action] === true) { return true; } // check for request method limit on this action/context. if (array_key_exists($context, $contexts[$action])) { $requestMethod = $this->getRequest()->getMethod(); $actionMethods = (array) $contexts[$action][$context]; foreach ($actionMethods as $method) { if (strtoupper($method) === $requestMethod) { return true; } } return false; } // must be valid. return true; }
P4Cms_Controller_Action_Helper_ContextSwitch::initContext | ( | $ | format = null | ) |
Extend init context to disable auto-init when called.
mixed | $format | The default context. |
Zend_Controller_Action_Exception |
{ // normalize controller contexts for parent. $controller = $this->getActionController(); $contextKey = $this->_complexContextKey; $normalized = array(); $contexts = isset($controller->$contextKey) ? $controller->$contextKey : array(); foreach ($contexts as $action => $actionContexts) { if ($actionContexts === true) { $normalized[$action] = true; continue; } $normalized[$action] = array(); foreach ($actionContexts as $key => $value) { $context = is_string($key) ? $key : $value; $normalized[$action][] = $context; // automatically add any missing contexts. if (!$this->hasContext($context)) { $this->addContext($context, array('suffix' => $context)); } } } // copy normalized contexts to standard context key. $contextKey = $this->_contextKey; $controller->$contextKey = $normalized; $this->_autoInit = false; return parent::initContext($format); }
P4Cms_Controller_Action_Helper_ContextSwitch::preDispatch | ( | ) |
Automatically initialize context prior to dispatch.
{ // init contexts if auto-init true and controller defines contexts. if ($this->_autoInit && !empty($this->getActionController()->contexts)) { // disable automatic json serialization when auto initializing. $this->setAutoJsonSerialization(false); $this->initContext(); $this->_autoInit = true; } // make the helper easier to access from the controller. $this->getActionController()->contextSwitch = $this; return parent::preDispatch(); }
P4Cms_Controller_Action_Helper_ContextSwitch::$_autoInit = true [protected] |
P4Cms_Controller_Action_Helper_ContextSwitch::$_complexContextKey = 'contexts' [protected] |
P4Cms_Controller_Action_Helper_ContextSwitch::$_contextKey = 'normalizedContexts' [protected] |