/libraries/joomla/application/web/router.php

https://bitbucket.org/eternaware/joomus · PHP · 153 lines · 41 code · 18 blank · 94 comment · 2 complexity · 7f91f233194eb6b4dcf7c56d36d78cf8 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Class to define an abstract Web application router.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Application
  15. * @since 12.2
  16. */
  17. abstract class JApplicationWebRouter
  18. {
  19. /**
  20. * @var JApplicationWeb The web application on whose behalf we are routing the request.
  21. * @since 12.2
  22. */
  23. protected $app;
  24. /**
  25. * @var string The default page controller name for an empty route.
  26. * @since 12.2
  27. */
  28. protected $default;
  29. /**
  30. * @var string Controller class name prefix for creating controller objects by name.
  31. * @since 12.2
  32. */
  33. protected $controllerPrefix;
  34. /**
  35. * @var JInput An input object from which to derive the route.
  36. * @since 12.2
  37. */
  38. protected $input;
  39. /**
  40. * Constructor.
  41. *
  42. * @param JApplicationWeb $app The web application on whose behalf we are routing the request.
  43. * @param JInput $input An optional input object from which to derive the route. If none
  44. * is given than the input from the application object will be used.
  45. *
  46. * @since 12.2
  47. */
  48. public function __construct(JApplicationWeb $app, JInput $input = null)
  49. {
  50. $this->app = $app;
  51. $this->input = ($input === null) ? $this->app->input : $input;
  52. }
  53. /**
  54. * Find and execute the appropriate controller based on a given route.
  55. *
  56. * @param string $route The route string for which to find and execute a controller.
  57. *
  58. * @return void
  59. *
  60. * @since 12.2
  61. * @throws InvalidArgumentException
  62. * @throws RuntimeException
  63. */
  64. public function execute($route)
  65. {
  66. // Get the controller name based on the route patterns and requested route.
  67. $name = $this->parseRoute($route);
  68. // Get the controller object by name.
  69. $controller = $this->fetchController($name);
  70. // Execute the controller.
  71. $controller->execute();
  72. }
  73. /**
  74. * Set the controller name prefix.
  75. *
  76. * @param string $prefix Controller class name prefix for creating controller objects by name.
  77. *
  78. * @return JApplicationWebRouter This object for method chaining.
  79. *
  80. * @since 12.2
  81. */
  82. public function setControllerPrefix($prefix)
  83. {
  84. $this->controllerPrefix = (string) $prefix;
  85. return $this;
  86. }
  87. /**
  88. * Set the default controller name.
  89. *
  90. * @param string $name The default page controller name for an empty route.
  91. *
  92. * @return JApplicationWebRouter This object for method chaining.
  93. *
  94. * @since 12.2
  95. */
  96. public function setDefaultController($name)
  97. {
  98. $this->default = (string) $name;
  99. return $this;
  100. }
  101. /**
  102. * Parse the given route and return the name of a controller mapped to the given route.
  103. *
  104. * @param string $route The route string for which to find and execute a controller.
  105. *
  106. * @return string The controller name for the given route excluding prefix.
  107. *
  108. * @since 12.2
  109. * @throws InvalidArgumentException
  110. */
  111. abstract protected function parseRoute($route);
  112. /**
  113. * Get a JController object for a given name.
  114. *
  115. * @param string $name The controller name (excluding prefix) for which to fetch and instance.
  116. *
  117. * @return JController
  118. *
  119. * @since 12.2
  120. * @throws RuntimeException
  121. */
  122. protected function fetchController($name)
  123. {
  124. // Derive the controller class name.
  125. $class = $this->controllerPrefix . ucfirst($name);
  126. // If the controller class does not exist panic.
  127. if (!class_exists($class) || !is_subclass_of($class, 'JController'))
  128. {
  129. throw new RuntimeException(sprintf('Unable to locate controller `%s`.', $class), 404);
  130. }
  131. // Instantiate the controller.
  132. $controller = new $class($this->input, $this->app);
  133. return $controller;
  134. }
  135. }