/** * Retrieves the view renderer object * * @return Zend_Controller_Action_Helper_ViewRenderer|null * @throws Glitch_Application_Resource_Exception */ public function getViewRenderer() { if (null === $this->_viewRenderer) { // Pull in the front controller; bootstrap first if necessary $this->_bootstrap->bootstrap('FrontController'); $front = $this->_bootstrap->getResource('FrontController'); // Ignore if no view renderer is to be used if ($front->getParam('noViewRenderer')) { return null; } // Get existing renderer, if any, or create a new one $this->_viewRenderer = Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer') ? Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer') : new $this->_className(null, $this->getOptions()); // Dynamic class loading; perform sanity check if (!$this->_viewRenderer instanceof Zend_Controller_Action_Helper_ViewRenderer) { throw new Glitch_Application_Resource_Exception('Class is not a valid view renderer instance'); } // Register the view as the default view for handling view scripts $this->_bootstrap->bootstrap('View'); $view = $this->_bootstrap->getResource('View'); $this->_viewRenderer->setView($view); // It is paramount to set this base path spec: ZF otherwise uses its own // spec, causing it to create a path to a conventional ZF-style directory $this->_viewRenderer->setViewBasePathSpec(':module/' . Glitch_View::PATH_VIEW); // Set empty inflector settings: all path translations are handled by the custom dispatcher $inflector = new Zend_Filter_Inflector(); $inflector->addRules(array(':module' => array(), ':controller' => array(), ':action' => array())); $this->_viewRenderer->setInflector($inflector, true); if (!Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) { Zend_Controller_Action_HelperBroker::addHelper($this->_viewRenderer); } } return $this->_viewRenderer; }
protected function _initView() { // Start initail view $this->bootstrap('layout'); $config = $this->getOption('views'); $resources = $this->getOption('resources'); $view = new Zend_View(); if (isset($resources['layout']['layoutPath'])) { $view->assign('layoutRootPath', $resources['layout']['layoutPath']); } $this->bootstrap('db'); Zend_Loader::loadClass('Ht_Utils_SystemSetting'); $sysSetting = Ht_Utils_SystemSetting::getSettings(); $view->assign('sysSetting', $sysSetting); $view->assign('profile', Zend_Auth::getInstance()->getIdentity()); Zend_Loader::loadClass("Ht_Model_SystemSetting"); $this->setSystemLogConfiguration($sysSetting); // use the viewrenderer to keep the code DRY // instantiate and add the helper in one go $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer'); $viewRenderer->setView($view); $viewRenderer->setViewSuffix('phtml'); // add it to the action helper broker Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); /** * Set inflector for Zend_Layout */ $inflector = new Zend_Filter_Inflector(':script.:suffix'); $inflector->addRules(array(':script' => array('Word_CamelCaseToDash', 'StringToLower'), 'suffix' => 'phtml')); // Initialise Zend_Layout's MVC helpers $this->getResource('layout')->setLayoutPath(realpath($resources['layout']['layoutPath']))->setView($view)->setContentKey('content')->setInflector($inflector); return $this->getResource('layout')->getView(); }
/** * Get inflector * * @return Zend_Filter_Inflector */ public function getInflector() { if (null === $this->_inflector) { $this->_inflector = new Zend_Filter_Inflector(); $this->_inflector->addRules(array(':module' => array('Word_CamelCaseToDash', 'StringToLower'), ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator('/'), 'StringToLower', new Zend_Filter_PregReplace('/\\./', '-')), ':action' => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('#[^a-z0-9' . preg_quote('/', '#') . ']+#i', '-'), 'StringToLower'))); } return $this->_inflector; }
/** * Get path inflector * @return Zend_Filter_Inflector */ public static function getInflector() { if (null === self::$_inflector) { $inflector = new Zend_Filter_Inflector(); $inflector->addRules(array(':module' => array('Word_CamelCaseToDash', 'StringToLower'), ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator('/'), 'StringToLower', new Zend_Filter_PregReplace('/\\./', '-')), ':action' => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('#[^a-z0-9' . preg_quote('/', '#') . ']+#i', '-'), 'StringToLower')))->setTarget('../application/modules/:module/views/scripts/:controller/:action.xsl'); self::setInflector($inflector); } return self::$_inflector; }
/** * @issue ZF-2964 */ public function testNoInflectableTarget() { $inflector = new Zend_Filter_Inflector('abc'); $inflector->addRules(array(':foo' => array())); $this->assertEquals($inflector->filter(array('fo' => 'bar')), 'abc'); }
public function testCustomInflectorUsesViewRendererTargetWhenPassedInWithReferenceFlag() { $this->request->setModuleName('bar')->setControllerName('index')->setActionName('test'); $controller = new Bar_IndexController($this->request, $this->response, array()); $this->helper->view->addBasePath($this->basePath . '/_files/modules/bar/views'); require_once 'Zend/Filter/PregReplace.php'; require_once 'Zend/Filter/Word/UnderscoreToSeparator.php'; $inflector = new Zend_Filter_Inflector('test.phtml'); $inflector->addRules(array(':module' => array('Word_CamelCaseToDash', 'stringToLower'), ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator(DIRECTORY_SEPARATOR), 'StringToLower'), ':action' => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('/[^a-z0-9]+/i', '-'), 'StringToLower'))); $this->helper->setInflector($inflector, true); $this->helper->render(); $body = $this->response->getBody(); $this->assertContains('Rendered index/test.phtml in bar module', $body); }
public function preDispatch() { $request = $this->getRequest(); if (Axis_Area::isFrontend()) { if (!Axis::getCustomerId() && $this->getActionController() instanceof Axis_Account_Controller_Abstract) { $request->setModuleName('Axis_Account')->setControllerName('auth')->setActionName('index')->setDispatched(false); } return; } if (!Axis_Area::isBackend()) { return; } $auth = Zend_Auth::getInstance(); $auth->setStorage(new Zend_Auth_Storage_Session('admin')); if (in_array($request->getControllerName(), array('auth', 'forgot')) && 'Axis_Admin' === $request->getModuleName()) { return; } if (!$auth->hasIdentity()) { if ($request->isXmlHttpRequest()) { Axis::message()->addError(Axis::translate('admin')->__('Your session has been expired. Please relogin')); $jsonHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('json'); $jsonHelper->sendFailure(); return; } $request->setModuleName('Axis_Admin')->setControllerName('auth')->setActionName('index')->setDispatched(false); return; } $user = Axis::single('admin/user')->find($auth->getIdentity())->current(); if (!$user) { $request->setModuleName('Axis_Admin')->setControllerName('auth')->setActionName('logout')->setDispatched(false); return; } $acl = new Zend_Acl(); // add resources $resources = Axis::model('admin/acl_resource')->toFlatTree(); foreach ($resources as $resource) { $parent = $resource['parent']; try { $acl->addResource($resource['id'], $parent); } catch (Zend_Acl_Exception $e) { Axis::message()->addError($e->getMessage()); } } //add role(s) $role = (string) $user->role_id; $acl->addRole($role); //add permission $rowset = Axis::single('admin/acl_rule')->select('*')->where('role_id = ?', $role)->fetchRowset(); foreach ($rowset as $row) { if (!$acl->has($row->resource_id)) { // $row->delete(); // remove invalid rule continue; } $action = 'deny'; if ('allow' === $row->permission) { $action = 'allow'; } try { $acl->{$action}($row->role_id, $row->resource_id); } catch (Zend_Acl_Exception $e) { Axis::message()->addError($e->getMessage()); } } Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl); Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role); if (in_array($request->getControllerName(), array('error')) && 'Axis_Admin' === $request->getModuleName()) { return; } //get current resource by request $request = $this->getRequest(); $inflector = new Zend_Filter_Inflector(); $resource = $inflector->addRules(array(':module' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator('/'), 'StringToLower'), ':controller' => array('Word_CamelCaseToDash', 'StringToLower', new Zend_Filter_PregReplace('/admin_/', '')), ':action' => array('Word_CamelCaseToDash', 'StringToLower')))->setTarget('admin/:module/:controller/:action')->filter($request->getParams()); if (!$acl->has($resource) || $acl->isAllowed($role, $resource)) { return; } if ($request->isXmlHttpRequest()) { Axis::message()->addError(Axis::translate('admin')->__('You have no permission for this operation')); $jsonHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('json'); $jsonHelper->sendFailure(); return; } $request->setModuleName('Axis_Admin')->setControllerName('error')->setActionName('access-denied')->setDispatched(false); }
/** * Get inflector * * @return Zend_Filter_Inflector */ public function getInflector() { if (null === $this->_inflector) { require_once 'Zend/Filter/Inflector.php'; require_once 'Zend/Filter/PregReplace.php'; require_once 'Zend/Filter/Word/UnderscoreToSeparator.php'; $this->_inflector = new Zend_Filter_Inflector(); $this->_inflector->addRules(array(':module' => array('Word_CamelCaseToDash', 'stringToLower'), ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator(DIRECTORY_SEPARATOR), 'StringToLower'), ':action' => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('/[^a-z0-9]+/i', '-'), 'StringToLower')))->setStaticRuleReference('suffix', $this->_viewSuffix)->setStaticRuleReference('moduleDir', $this->_moduleDir)->setTargetReference($this->_inflectorTarget); } // Ensure that module directory is current $this->getModuleDirectory(); return $this->_inflector; }
/** * Constructor * * Accepts either: * - A string path to layouts * - An array of options * - A Zend_Config object with options * * Layout script path, either as argument or as key in options, is * required. * * If mvcEnabled flag is false from options, simply sets layout script path. * Otherwise, also instantiates and registers action helper and controller * plugin. * * @param string|array|Zend_Config $options * @return void */ public function __construct($options = null, $initMvc = false) { if (null !== $options) { if (is_string($options)) { $this->setLayoutPath($options); } elseif (is_array($options)) { $this->setOptions($options); } elseif ($options instanceof Zend_Config) { $this->setConfig($options); } else { require_once 'Zend/Layout/Exception.php'; throw new Zend_Layout_Exception('Invalid option provided to constructor'); } } $this->_initVarContainer(); if ($initMvc) { $this->_setMvcEnabled(true); $this->_initMvc(); } else { $this->_setMvcEnabled(false); } if (null === $this->getInflector()) { require_once 'Zend/Filter/Inflector.php'; $inflector = new Zend_Filter_Inflector(':script.:suffix'); $inflector->addRules(array(':script' => array('Word_CamelCaseToDash', 'StringToLower'), 'suffix' => 'phtml')); $this->setInflector($inflector); } }