/** * Encode data as JSON, disable layouts, and set response header * * If $keepLayouts is true, does not disable layouts. * If $encodeJson is false, does not JSON-encode $data * * @param mixed $data * @param bool $keepLayouts * NOTE: if boolean, establish $keepLayouts to true|false * if array, admit params for IfwPsn_Vendor_Zend_Json::encode as enableJsonExprFinder=>true|false * this array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false * that will not be passed to IfwPsn_Vendor_Zend_Json::encode method but will be used here * @param bool $encodeData * @return string|void */ public function json($data, $keepLayouts = false, $encodeData = true) { $options = array(); if (is_array($keepLayouts)) { $options = $keepLayouts; $keepLayouts = false; if (array_key_exists('keepLayouts', $options)) { $keepLayouts = $options['keepLayouts']; unset($options['keepLayouts']); } if (array_key_exists('encodeData', $options)) { $encodeData = $options['encodeData']; unset($options['encodeData']); } } if ($encodeData) { $data = IfwPsn_Vendor_Zend_Json::encode($data, null, $options); } if (!$keepLayouts) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Layout.php'; $layout = IfwPsn_Vendor_Zend_Layout::getMvcInstance(); if ($layout instanceof IfwPsn_Vendor_Zend_Layout) { $layout->disableLayout(); } } $response = IfwPsn_Vendor_Zend_Controller_Front::getInstance()->getResponse(); $response->setHeader('Content-Type', 'application/json', true); return $data; }
/** * Instantiates route based on passed IfwPsn_Vendor_Zend_Config structure */ public static function getInstance(IfwPsn_Vendor_Zend_Config $config) { $frontController = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); $defs = $config->defaults instanceof IfwPsn_Vendor_Zend_Config ? $config->defaults->toArray() : array(); $dispatcher = $frontController->getDispatcher(); $request = $frontController->getRequest(); return new self($defs, $dispatcher, $request); }
/** * Get BaseUrl * * @return string */ public function getBaseUrl() { if ($this->_baseUrl === null) { /** @see IfwPsn_Vendor_Zend_Controller_Front */ require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Front.php'; $baseUrl = IfwPsn_Vendor_Zend_Controller_Front::getInstance()->getBaseUrl(); // Remove scriptname, eg. index.php from baseUrl $baseUrl = $this->_removeScriptName($baseUrl); $this->setBaseUrl($baseUrl); } return $this->_baseUrl; }
/** * Constructor * * Register action stack plugin * * @return void */ public function __construct() { $front = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); if (!$front->hasPlugin('IfwPsn_Vendor_Zend_Controller_Plugin_ActionStack')) { /** * @see IfwPsn_Vendor_Zend_Controller_Plugin_ActionStack */ require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Plugin/ActionStack.php'; $this->_actionStack = new IfwPsn_Vendor_Zend_Controller_Plugin_ActionStack(); $front->registerPlugin($this->_actionStack, 97); } else { $this->_actionStack = $front->getPlugin('IfwPsn_Vendor_Zend_Controller_Plugin_ActionStack'); } }
/** * * @param IfwPsn_Vendor_Zend_Application $application */ public function __construct($application) { $options = array('IfwPsn_Vendor_Zend_Application_Resource' => IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Application/Resource'); $this->_pluginLoader = new IfwPsn_Vendor_Zend_Loader_PluginLoader($options); parent::__construct($application); $this->_pm = $this->getApplication()->getOption('pluginmanager'); // init the custom front controller instance $this->_pm->getLogger()->logPrefixed('Initializing front controller.'); $front = IfwPsn_Zend_Controller_Front::getInstance(); IfwPsn_Vendor_Zend_Controller_Front::getInstance()->returnResponse(true); // set dispatcher require_once $this->_pm->getPathinfo()->getRootLib() . 'IfwPsn/Zend/Controller/Dispatcher/Wp.php'; $dispatcher = new IfwPsn_Zend_Controller_Dispatcher_Wp($this->_pm); IfwPsn_Vendor_Zend_Controller_Front::getInstance()->setDispatcher($dispatcher); }
/** * Assembles a URL path defined by this route * * @param array $data An array of variable and value pairs used as parameters * @param bool $reset * @param bool $encode * @return string Route path with user submitted parameters */ public function assemble($data = array(), $reset = false, $encode = false) { $frontController = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); if (!array_key_exists('mod', $data) && !$reset && array_key_exists('mod', $this->_current) && $this->_current['mod'] != $frontController->getDefaultModule()) { $data = array_merge(array('mod' => $this->_current['mod']), $data); } if (!array_key_exists('controller', $data) && !$reset && array_key_exists('controller', $this->_current) && $this->_current['controller'] != $frontController->getDefaultControllerName()) { $data = array_merge(array('controller' => $this->_current['controller']), $data); } if (!array_key_exists('action', $data) && !$reset && array_key_exists('action', $this->_current) && $this->_current['action'] != $frontController->getDefaultAction()) { $data = array_merge(array('action' => $this->_current['action']), $data); } if (!empty($data)) { $querydata = array(); if (isset($data['page'])) { $url = IfwPsn_Wp_Proxy_Admin::getOptionsBaseUrl(); $querydata['page'] = $data['page']; unset($data['page']); } elseif (isset($data['adminpage'])) { $url = IfwPsn_Wp_Proxy_Admin::getAdminPageBaseUrl(); $querydata['page'] = $data['adminpage']; unset($data['adminpage']); } elseif (isset($data['editpage'])) { $url = 'edit.php'; if (isset($data['posttype'])) { $querydata['post_type'] = $data['posttype']; unset($data['posttype']); } $querydata['page'] = $data['editpage']; unset($data['editpage']); } if (isset($data['module'])) { $querydata['mod'] = $data['module']; unset($data['module']); } if (isset($data['action'])) { $querydata[$this->_pm->getConfig()->getActionKey()] = $data['action']; unset($data['action']); } $querydata = array_merge($querydata, $data); $url .= '?' . http_build_query($querydata, '', '&'); } return $url; }
/** * Renders a template fragment within a variable scope distinct from the * calling View object. * * If no arguments are passed, returns the helper instance. * * If the $model is an array, it is passed to the view object's assign() * method. * * If the $model is an object, it first checks to see if the object * implements a 'toArray' method; if so, it passes the result of that * method to to the view object's assign() method. Otherwise, the result of * get_object_vars() is passed. * * @param string $name Name of view script * @param string|array $module If $model is empty, and $module is an array, * these are the variables to populate in the * view. Otherwise, the module in which the * partial resides * @param array $model Variables to populate in the view * @return string|IfwPsn_Vendor_Zend_View_Helper_Partial */ public function partial($name = null, $module = null, $model = null) { if (0 == func_num_args()) { return $this; } $view = $this->cloneView(); if (isset($this->partialCounter)) { $view->partialCounter = $this->partialCounter; } if (isset($this->partialTotalCount)) { $view->partialTotalCount = $this->partialTotalCount; } if (null !== $module && is_string($module)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Front.php'; $moduleDir = IfwPsn_Vendor_Zend_Controller_Front::getInstance()->getControllerDirectory($module); if (null === $moduleDir) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/View/Helper/Partial/Exception.php'; $e = new IfwPsn_Vendor_Zend_View_Helper_Partial_Exception('Cannot render partial; module does not exist'); $e->setView($this->view); throw $e; } $viewsDir = dirname($moduleDir) . '/views'; $view->addBasePath($viewsDir); } elseif (null == $model && null !== $module && (is_array($module) || is_object($module))) { $model = $module; } if (!empty($model)) { if (is_array($model)) { $view->assign($model); } elseif (is_object($model)) { if (null !== ($objectKey = $this->getObjectKey())) { $view->assign($objectKey, $model); } elseif (method_exists($model, 'toArray')) { $view->assign($model->toArray()); } else { $view->assign(get_object_vars($model)); } } } return $view->render($name); }
/** * Prepare data for autocompletion * * @param mixed $data * @param boolean $keepLayouts * @return string */ public function prepareAutoCompletion($data, $keepLayouts = false) { if (!$data instanceof IfwPsn_Vendor_Zend_Dojo_Data) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Dojo/Data.php'; $items = array(); foreach ($data as $key => $value) { $items[] = array('label' => $value, 'name' => $value); } $data = new IfwPsn_Vendor_Zend_Dojo_Data('name', $items); } if (!$keepLayouts) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Action/HelperBroker.php'; IfwPsn_Vendor_Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Layout.php'; $layout = IfwPsn_Vendor_Zend_Layout::getMvcInstance(); if ($layout instanceof IfwPsn_Vendor_Zend_Layout) { $layout->disableLayout(); } } $response = IfwPsn_Vendor_Zend_Controller_Front::getInstance()->getResponse(); $response->setHeader('Content-Type', 'application/json'); return $data->toJson(); }
/** * Constructor * * Grab local copies of various MVC objects * * @return void */ public function __construct() { $front = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); $modules = $front->getControllerDirectory(); if (empty($modules)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/View/Exception.php'; $e = new IfwPsn_Vendor_Zend_View_Exception('Action helper depends on valid front controller instance'); $e->setView($this->view); throw $e; } $request = $front->getRequest(); $response = $front->getResponse(); if (empty($request) || empty($response)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/View/Exception.php'; $e = new IfwPsn_Vendor_Zend_View_Exception('Action view helper requires both a registered request and response object in the front controller instance'); $e->setView($this->view); throw $e; } $this->request = clone $request; $this->response = clone $response; $this->dispatcher = clone $front->getDispatcher(); $this->defaultModule = $front->getDefaultModule(); }
/** * Get the Cache Manager instance or instantiate the object if not * exists. Attempts to load from bootstrap if available. * * @return IfwPsn_Vendor_Zend_Cache_Manager */ public function getManager() { if ($this->_manager !== null) { return $this->_manager; } $front = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); if ($front->getParam('bootstrap') && $front->getParam('bootstrap')->getResource('CacheManager')) { return $front->getParam('bootstrap')->getResource('CacheManager'); } $this->_manager = new IfwPsn_Vendor_Zend_Cache_Manager(); return $this->_manager; }
/** * Retrieve Front Controller * * @return IfwPsn_Vendor_Zend_Controller_Front */ public function getFrontController() { // Used cache version if found if (null !== $this->_frontController) { return $this->_frontController; } // Grab singleton instance, if class has been loaded if (class_exists('IfwPsn_Vendor_Zend_Controller_Front')) { $this->_frontController = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); return $this->_frontController; } // Throw exception in all other cases require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Exception.php'; throw new IfwPsn_Vendor_Zend_Controller_Exception('Front controller class has not been loaded'); }
/** * Retrieve front controller instance * * @return IfwPsn_Vendor_Zend_Controller_Front */ public function getFrontController() { if (null === $this->_frontController) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Front.php'; $this->_frontController = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); } return $this->_frontController; }
/** * Retrieve front controller instance * * @return IfwPsn_Vendor_Zend_Controller_Front */ public function getFrontController() { if (null === $this->_front) { $this->_front = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); } return $this->_front; }
/** * Get the request object * * @return IfwPsn_Vendor_Zend_Controller_Request_Abstract $request */ public function getRequest() { if ($this->_request === null) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Front.php'; $this->_request = IfwPsn_Vendor_Zend_Controller_Front::getInstance()->getRequest(); } return $this->_request; }
/** * Generates an url given the name of a route. * * @access public * * @param array $urlOptions Options passed to the assemble method of the Route object. * @param mixed $name The name of a Route to use. If null it will use the current Route * @param bool $reset Whether or not to reset the route defaults with those provided * @return string Url for the link href attribute. */ public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true) { $router = IfwPsn_Vendor_Zend_Controller_Front::getInstance()->getRouter(); return $router->assemble($urlOptions, $name, $reset, $encode); }
/** * Retrieve front controller instance * * @return IfwPsn_Vendor_Zend_Controller_Front */ public function getFrontController() { return IfwPsn_Vendor_Zend_Controller_Front::getInstance(); }
/** * Returns whether page should be considered active or not * * This method will compare the page properties against the request object * that is found in the front controller. * * @param bool $recursive [optional] whether page should be considered * active if any child pages are active. Default is * false. * @return bool whether page should be considered active or not */ public function isActive($recursive = false) { if (null === $this->_active) { $front = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); $request = $front->getRequest(); $reqParams = array(); if ($request) { $reqParams = $request->getParams(); if (!array_key_exists('module', $reqParams)) { $reqParams['module'] = $front->getDefaultModule(); } } $myParams = $this->_params; if ($this->_route && method_exists($front->getRouter(), 'getRoute')) { $route = $front->getRouter()->getRoute($this->_route); if (method_exists($route, 'getDefaults')) { $myParams = array_merge($route->getDefaults(), $myParams); } } if (null !== $this->_module) { $myParams['module'] = $this->_module; } elseif (!array_key_exists('module', $myParams)) { $myParams['module'] = $front->getDefaultModule(); } if (null !== $this->_controller) { $myParams['controller'] = $this->_controller; } elseif (!array_key_exists('controller', $myParams)) { $myParams['controller'] = $front->getDefaultControllerName(); } if (null !== $this->_action) { $myParams['action'] = $this->_action; } elseif (!array_key_exists('action', $myParams)) { $myParams['action'] = $front->getDefaultAction(); } foreach ($myParams as $key => $value) { if (null === $value) { unset($myParams[$key]); } } if (count(array_intersect_assoc($reqParams, $myParams)) == count($myParams)) { $this->_active = true; return true; } $this->_active = false; } return parent::isActive($recursive); }
/** * Handle errors and exceptions * * If the 'noErrorHandler' front controller flag has been set, * returns early. * * @param IfwPsn_Vendor_Zend_Controller_Request_Abstract $request * @return void */ protected function _handleError(IfwPsn_Vendor_Zend_Controller_Request_Abstract $request) { $frontController = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); if ($frontController->getParam('noErrorHandler')) { return; } $response = $this->getResponse(); if ($this->_isInsideErrorHandlerLoop) { $exceptions = $response->getException(); if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) { // Exception thrown by error handler; tell the front controller to throw it $frontController->throwExceptions(true); throw array_pop($exceptions); } } // check for an exception AND allow the error handler controller the option to forward if ($response->isException() && !$this->_isInsideErrorHandlerLoop) { $this->_isInsideErrorHandlerLoop = true; // Get exception information $error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS); $exceptions = $response->getException(); $exception = $exceptions[0]; $exceptionType = get_class($exception); $error->exception = $exception; switch ($exceptionType) { case 'IfwPsn_Vendor_Zend_Controller_Router_Exception': if (404 == $exception->getCode()) { $error->type = self::EXCEPTION_NO_ROUTE; } else { $error->type = self::EXCEPTION_OTHER; } break; case 'IfwPsn_Vendor_Zend_Controller_Dispatcher_Exception': $error->type = self::EXCEPTION_NO_CONTROLLER; break; case 'IfwPsn_Vendor_Zend_Controller_Action_Exception': if (404 == $exception->getCode()) { $error->type = self::EXCEPTION_NO_ACTION; } else { $error->type = self::EXCEPTION_OTHER; } break; default: $error->type = self::EXCEPTION_OTHER; break; } // Keep a copy of the original request $error->request = clone $request; // get a count of the number of exceptions encountered $this->_exceptionCountAtFirstEncounter = count($exceptions); // Forward to the error handler $request->setParam('error_handler', $error)->setModuleName($this->getErrorHandlerModule())->setControllerName($this->getErrorHandlerController())->setActionName($this->getErrorHandlerAction())->setDispatched(false); } }
/** * Called before IfwPsn_Vendor_Zend_Controller_Front exits its dispatch loop. * * @param IfwPsn_Vendor_Zend_Controller_Request_Abstract $request * @return void */ public function dispatchLoopShutdown() { foreach ($this->_plugins as $plugin) { try { $plugin->dispatchLoopShutdown(); } catch (Exception $e) { if (IfwPsn_Vendor_Zend_Controller_Front::getInstance()->throwExceptions()) { throw new IfwPsn_Vendor_Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e); } else { $this->getResponse()->setException($e); } } } }
/** * Singleton instance * * @return IfwPsn_Vendor_Zend_Controller_Front */ public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; }
/** * Initialize front controller plugin * * @return void */ protected function _initPlugin() { $pluginClass = $this->getPluginClass(); require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Front.php'; $front = IfwPsn_Vendor_Zend_Controller_Front::getInstance(); if (!$front->hasPlugin($pluginClass)) { if (!class_exists($pluginClass)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Zend/Loader.php'; IfwPsn_Zend_Loader::loadClass($pluginClass); } $front->registerPlugin(new $pluginClass($this), 99); } }