/application/classes/OntoWiki/Dispatcher.php
PHP | 160 lines | 73 code | 20 blank | 67 comment | 10 complexity | ba1e4561cafa8f33e2bd2742ed692e49 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- <?php
- /**
- * This file is part of the {@link http://ontowiki.net OntoWiki} project.
- *
- * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
- * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
- */
- /**
- * OntoWiki dispatcher
- *
- * Overwrites Zend_Controller_Dispatcher_Standard in order to allow for
- * multiple (component) controller directories.
- *
- * @category OntoWiki
- * @package Dispatcher
- * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
- * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
- * @author Norman Heino <norman.heino@gmail.com>
- */
- class OntoWiki_Dispatcher extends Zend_Controller_Dispatcher_Standard
- {
- /**
- * The extension manager
- * @var OntoWiki_Extension_Manager
- */
- protected $_extensionManager = null;
- /**
- * Base for building URLs
- * @var string
- */
- protected $_urlBase = '';
- public function __construct($params = array())
- {
- if (array_key_exists('url_base', $params)) {
- $urlBase = (string)$params['url_base'];
- unset($params['url_base']);
- }
- parent::__construct($params);
- $this->urlBase = $urlBase;
- }
- /**
- * Sets the component manager
- */
- public function setExtensionManager(OntoWiki_Extension_Manager $extensionManager)
- {
- $this->_extensionManager = $extensionManager;
- }
- /**
- * Gets the component manager
- */
- public function getExtensionManager()
- {
- return $this->_extensionManager;
- }
- /**
- * Get controller class name
- *
- * Try request first; if not found, try pulling from request parameter;
- * if still not found, fallback to default
- *
- * @param Zend_Controller_Request_Abstract $request
- * @return string|false Returns class name on success
- */
- public function getControllerClass(Zend_Controller_Request_Abstract $request)
- {
- $controllerName = $request->getControllerName();
- if (empty($controllerName)) {
- if (!$this->getParam('useDefaultControllerAlways')) {
- return false;
- }
- $controllerName = $this->getDefaultControllerName();
- $request->setControllerName($controllerName);
- }
- // Zend 1.10+ changes
- $className = $this->formatControllerName($controllerName);
- $controllerDirs = $this->getControllerDirectory();
- $module = $request->getModuleName();
- if ($this->isValidModule($module)) {
- $this->_curModule = $module;
- $this->_curDirectory = $controllerDirs[$module];
- } elseif ($this->isValidModule($this->_defaultModule)) {
- $request->setModuleName($this->_defaultModule);
- $this->_curModule = $this->_defaultModule;
- $this->_curDirectory = $controllerDirs[$this->_defaultModule];
- } else {
- require_once 'Zend/Controller/Exception.php';
- throw new Zend_Controller_Exception('No default module defined for this application');
- }
- // PATCH
- // if component manager has controller registered
- // redirect to specific controller dir index
- if (null !== $this->_extensionManager && $this->_extensionManager->isComponentRegistered($controllerName)) {
- $this->_curDirectory = $controllerDirs[$this->_extensionManager->getComponentPrefix() . $controllerName];
- }
- return $className;
- }
- /**
- * Returns TRUE if the Zend_Controller_Request_Abstract object can be
- * dispatched to a controller.
- *
- * Use this method wisely. By default, the dispatcher will fall back to the
- * default controller (either in the module specified or the global default)
- * if a given controller does not exist. This method returning false does
- * not necessarily indicate the dispatcher will not still dispatch the call.
- *
- * @param Zend_Controller_Request_Abstract $action
- * @return boolean
- */
- public function isDispatchable(Zend_Controller_Request_Abstract $request)
- {
- // Zend 1.10+ changes
- $className = $this->getControllerClass($request);
- if (class_exists($className, false)) {
- return true;
- }
- $fileSpec = $this->classToFilename($className);
- $dispatchDir = $this->getDispatchDirectory();
- $test = $dispatchDir . DIRECTORY_SEPARATOR . $fileSpec;
- if (Zend_Loader::isReadable($test)) {
- return true;
- }
- /**
- * @trigger onIsDispatchable
- * Triggered if no suitable controller has been found. Plug-ins can
- * attach to this event in order to modify request URLs or provide
- * mechanisms that do not allow a controller/action mapping from URL
- * parts.
- */
- $event = new Erfurt_Event('onIsDispatchable');
- $event->uri = $this->urlBase . ltrim($request->getPathInfo(), '/');
- $event->request = $request;
- // We need to make sure that registered plugins return a boolean value!
- // Otherwise we return false.
- $eventResult = $event->trigger();
- if (is_bool($eventResult)) {
- return $eventResult;
- }
- return false;
- }
- }