/** * Create JSON response * * Encodes and returns data to JSON. Content-Type header set to * 'application/json', and disables layouts and viewRenderer (if being * used). * * @param mixed $data * @param boolean $keepLayouts * @param boolean|array $keepLayouts * NOTE: if boolean, establish $keepLayouts to true|false * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false * if $keepLayouts and parmas for Zend_Json::encode are required * then, the array can contains a 'keepLayout'=>true|false * that will not be passed to Zend_Json::encode method but will be passed * to Zend_View_Helper_Json * @throws EvHttp_Controller_Action_Helper_Json * @return string */ public function encodeJson($data, $keepLayouts = false) { /** * @see Zend_View_Helper_Json */ require_once 'Zend/View/Helper/Json.php'; $jsonHelper = new Zend_View_Helper_Json(); $data = $jsonHelper->json($data, $keepLayouts); if (!$keepLayouts) { /** * @see EvHttp_Controller_Action_HelperBroker */ require_once 'EvHttp/Controller/Action/HelperBroker.php'; EvHttp_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true); } return $data; }
/** * Prepare data for autocompletion * * @param mixed $data * @param boolean $keepLayouts * @return string */ public function prepareAutoCompletion($data, $keepLayouts = false) { if (!$data instanceof Zend_Dojo_Data) { require_once 'Zend/Dojo/Data.php'; $items = array(); foreach ($data as $key => $value) { $items[] = array('label' => $value, 'name' => $value); } $data = new Zend_Dojo_Data('name', $items); } if (!$keepLayouts) { require_once 'EvHttp/Controller/Action/HelperBroker.php'; EvHttp_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true); require_once 'Zend/Layout.php'; $layout = Zend_Layout::getMvcInstance(); if ($layout instanceof Zend_Layout) { $layout->disableLayout(); } } $response = EvHttp_Controller_Front::getInstance()->getResponse(); $response->setHeader('Content-Type', 'application/json'); return $data->toJson(); }
/** * Lazy load the priority stack and return it * * @return EvHttp_Controller_Action_HelperBroker_PriorityStack */ public static function getStack() { if (self::$_stack == null) { self::$_stack = new EvHttp_Controller_Action_HelperBroker_PriorityStack(); } return self::$_stack; }
/** * Encode data to JSON * * @param mixed $data * @param bool $keepLayouts * @throws EvHttp_Controller_Action_Exception * @return string */ public function encodeJson($data, $keepLayouts = false) { if ($this->validateData($data)) { return EvHttp_Controller_Action_HelperBroker::getStaticHelper('Json')->encodeJson($data, $keepLayouts); } /** * @see EvHttp_Controller_Action_Exception */ require_once 'EvHttp/Controller/Action/Exception.php'; throw new EvHttp_Controller_Action_Exception('Invalid data passed for autocompletion'); }
/** * Dispatch an HTTP request to a controller/action. * * @param EvHttp_Controller_Request_Abstract|null $request * @param EvHttp_Controller_Response_Abstract|null $response * @return void|EvHttp_Controller_Response_Abstract Returns response object if returnResponse() is true */ public function dispatch(EvHttp_Controller_Request_Abstract $request = null, EvHttp_Controller_Response_Abstract $response = null) { if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('EvHttp_Controller_Plugin_ErrorHandler')) { // Register with stack index of 100 require_once 'EvHttp/Controller/Plugin/ErrorHandler.php'; $this->_plugins->registerPlugin(new EvHttp_Controller_Plugin_ErrorHandler(), 100); } if (!$this->getParam('noViewRenderer') && !EvHttp_Controller_Action_HelperBroker::hasHelper('viewRenderer')) { require_once 'EvHttp/Controller/Action/Helper/ViewRenderer.php'; EvHttp_Controller_Action_HelperBroker::getStack()->offsetSet(-80, new EvHttp_Controller_Action_Helper_ViewRenderer()); } /** * Instantiate default request object (HTTP version) if none provided */ if (null !== $request) { $this->setRequest($request); } elseif (null !== ($request = $this->getRequest())) { $request->init(); } elseif (null === $request && null === ($request = $this->getRequest())) { require_once 'EvHttp/Controller/Request/Http.php'; $request = new EvHttp_Controller_Request_Http(); $this->setRequest($request); } /** * Set base URL of request object, if available */ if (is_callable(array($this->_request, 'setBaseUrl'))) { if (null !== $this->_baseUrl) { $this->_request->setBaseUrl($this->_baseUrl); } } /** * Instantiate default response object (HTTP version) if none provided */ if (null !== $response) { $this->setResponse($response); } elseif (null !== ($this->_response = $this->getResponse())) { $this->_response->reset(); } elseif (null === $this->_response && null === ($this->_response = $this->getResponse())) { require_once 'EvHttp/Controller/Response/Http.php'; $response = new EvHttp_Controller_Response_Http(); $this->setResponse($response); } /** * Register request and response objects with plugin broker */ $this->_plugins->setRequest($this->_request)->setResponse($this->_response); /** * Initialize router */ $router = $this->getRouter(); $router->setParams($this->getParams()); /** * Initialize dispatcher */ $dispatcher = $this->getDispatcher(); $dispatcher->setParams($this->getParams())->setResponse($this->_response); // Begin dispatch try { /** * Route request to controller/action, if a router is provided */ /** * Notify plugins of router startup */ $this->_plugins->routeStartup($this->_request); $router->route($this->_request); /** * Notify plugins of router completion */ $this->_plugins->routeShutdown($this->_request); /** * Notify plugins of dispatch loop startup */ $this->_plugins->dispatchLoopStartup($this->_request); /** * Attempt to dispatch the controller/action. If the $this->_request * indicates that it needs to be dispatched, move to the next * action in the request. */ do { $this->_request->setDispatched(true); /** * Notify plugins of dispatch startup */ $this->_plugins->preDispatch($this->_request); /** * Skip requested action if preDispatch() has reset it */ if (!$this->_request->isDispatched()) { continue; } /** * Dispatch request */ try { $dispatcher->dispatch($this->_request, $this->_response); } catch (Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } /** * Notify plugins of dispatch completion */ $this->_plugins->postDispatch($this->_request); } while (!$this->_request->isDispatched()); } catch (Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } /** * Notify plugins of dispatch loop completion */ try { $this->_plugins->dispatchLoopShutdown(); } catch (Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } if ($this->returnResponse()) { return $this->_response; } $this->_response->sendResponse(); }
/** * Dispatch the requested action * * @param string $action Method name of action * @return void */ public function dispatch($action) { // Notify helpers of action preDispatch state $this->_helper->notifyPreDispatch(); $this->preDispatch(); if ($this->getRequest()->isDispatched()) { if (null === $this->_classMethods) { $this->_classMethods = get_class_methods($this); } // preDispatch() didn't change the action, so we can continue if ($this->getInvokeArg('useCaseSensitiveActions') || in_array($action, $this->_classMethods)) { if ($this->getInvokeArg('useCaseSensitiveActions')) { trigger_error('Using case sensitive actions without word separators is deprecated; please do not rely on this "feature"'); } $this->{$action}(); } else { $this->__call($action, array()); } $this->postDispatch(); } // whats actually important here is that this action controller is // shutting down, regardless of dispatching; notify the helpers of this // state $this->_helper->notifyPostDispatch(); }
/** * Retrieve ViewRenderer * * @return EvHttp_Controller_Action_Helper_ViewRenderer Provides a fluent interface */ protected function _getViewRenderer() { if (null === $this->_viewRenderer) { $this->_viewRenderer = EvHttp_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); } return $this->_viewRenderer; }