PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/OntoWiki/Dispatcher.php

https://code.google.com/p/ontowiki/
PHP | 160 lines | 73 code | 20 blank | 67 comment | 10 complexity | ba1e4561cafa8f33e2bd2742ed692e49 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the {@link http://ontowiki.net OntoWiki} project.
  4. *
  5. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  6. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  7. */
  8. /**
  9. * OntoWiki dispatcher
  10. *
  11. * Overwrites Zend_Controller_Dispatcher_Standard in order to allow for
  12. * multiple (component) controller directories.
  13. *
  14. * @category OntoWiki
  15. * @package Dispatcher
  16. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  17. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  18. * @author Norman Heino <norman.heino@gmail.com>
  19. */
  20. class OntoWiki_Dispatcher extends Zend_Controller_Dispatcher_Standard
  21. {
  22. /**
  23. * The extension manager
  24. * @var OntoWiki_Extension_Manager
  25. */
  26. protected $_extensionManager = null;
  27. /**
  28. * Base for building URLs
  29. * @var string
  30. */
  31. protected $_urlBase = '';
  32. public function __construct($params = array())
  33. {
  34. if (array_key_exists('url_base', $params)) {
  35. $urlBase = (string)$params['url_base'];
  36. unset($params['url_base']);
  37. }
  38. parent::__construct($params);
  39. $this->urlBase = $urlBase;
  40. }
  41. /**
  42. * Sets the component manager
  43. */
  44. public function setExtensionManager(OntoWiki_Extension_Manager $extensionManager)
  45. {
  46. $this->_extensionManager = $extensionManager;
  47. }
  48. /**
  49. * Gets the component manager
  50. */
  51. public function getExtensionManager()
  52. {
  53. return $this->_extensionManager;
  54. }
  55. /**
  56. * Get controller class name
  57. *
  58. * Try request first; if not found, try pulling from request parameter;
  59. * if still not found, fallback to default
  60. *
  61. * @param Zend_Controller_Request_Abstract $request
  62. * @return string|false Returns class name on success
  63. */
  64. public function getControllerClass(Zend_Controller_Request_Abstract $request)
  65. {
  66. $controllerName = $request->getControllerName();
  67. if (empty($controllerName)) {
  68. if (!$this->getParam('useDefaultControllerAlways')) {
  69. return false;
  70. }
  71. $controllerName = $this->getDefaultControllerName();
  72. $request->setControllerName($controllerName);
  73. }
  74. // Zend 1.10+ changes
  75. $className = $this->formatControllerName($controllerName);
  76. $controllerDirs = $this->getControllerDirectory();
  77. $module = $request->getModuleName();
  78. if ($this->isValidModule($module)) {
  79. $this->_curModule = $module;
  80. $this->_curDirectory = $controllerDirs[$module];
  81. } elseif ($this->isValidModule($this->_defaultModule)) {
  82. $request->setModuleName($this->_defaultModule);
  83. $this->_curModule = $this->_defaultModule;
  84. $this->_curDirectory = $controllerDirs[$this->_defaultModule];
  85. } else {
  86. require_once 'Zend/Controller/Exception.php';
  87. throw new Zend_Controller_Exception('No default module defined for this application');
  88. }
  89. // PATCH
  90. // if component manager has controller registered
  91. // redirect to specific controller dir index
  92. if (null !== $this->_extensionManager && $this->_extensionManager->isComponentRegistered($controllerName)) {
  93. $this->_curDirectory = $controllerDirs[$this->_extensionManager->getComponentPrefix() . $controllerName];
  94. }
  95. return $className;
  96. }
  97. /**
  98. * Returns TRUE if the Zend_Controller_Request_Abstract object can be
  99. * dispatched to a controller.
  100. *
  101. * Use this method wisely. By default, the dispatcher will fall back to the
  102. * default controller (either in the module specified or the global default)
  103. * if a given controller does not exist. This method returning false does
  104. * not necessarily indicate the dispatcher will not still dispatch the call.
  105. *
  106. * @param Zend_Controller_Request_Abstract $action
  107. * @return boolean
  108. */
  109. public function isDispatchable(Zend_Controller_Request_Abstract $request)
  110. {
  111. // Zend 1.10+ changes
  112. $className = $this->getControllerClass($request);
  113. if (class_exists($className, false)) {
  114. return true;
  115. }
  116. $fileSpec = $this->classToFilename($className);
  117. $dispatchDir = $this->getDispatchDirectory();
  118. $test = $dispatchDir . DIRECTORY_SEPARATOR . $fileSpec;
  119. if (Zend_Loader::isReadable($test)) {
  120. return true;
  121. }
  122. /**
  123. * @trigger onIsDispatchable
  124. * Triggered if no suitable controller has been found. Plug-ins can
  125. * attach to this event in order to modify request URLs or provide
  126. * mechanisms that do not allow a controller/action mapping from URL
  127. * parts.
  128. */
  129. $event = new Erfurt_Event('onIsDispatchable');
  130. $event->uri = $this->urlBase . ltrim($request->getPathInfo(), '/');
  131. $event->request = $request;
  132. // We need to make sure that registered plugins return a boolean value!
  133. // Otherwise we return false.
  134. $eventResult = $event->trigger();
  135. if (is_bool($eventResult)) {
  136. return $eventResult;
  137. }
  138. return false;
  139. }
  140. }