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

https://bitbucket.org/ke2083/transfans.co.uk-website · PHP · 176 lines · 88 code · 21 blank · 67 comment · 15 complexity · c256a61f4deb0dbcabac80e6e1dfd962 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Application
  5. *
  6. * @copyright Copyright (C) 2005 - 2018 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. * Basic Web application router class for the Joomla Platform.
  12. *
  13. * @since 3.0
  14. * @deprecated 4.0 Use the `joomla/router` package via Composer instead
  15. */
  16. class JApplicationWebRouterBase extends JApplicationWebRouter
  17. {
  18. /**
  19. * @var array An array of rules, each rule being an associative array('regex'=> $regex, 'vars' => $vars, 'controller' => $controller)
  20. * for routing the request.
  21. * @since 3.0
  22. */
  23. protected $maps = array();
  24. /**
  25. * Add a route map to the router. If the pattern already exists it will be overwritten.
  26. *
  27. * @param string $pattern The route pattern to use for matching.
  28. * @param string $controller The controller name to map to the given pattern.
  29. *
  30. * @return JApplicationWebRouter This object for method chaining.
  31. *
  32. * @since 3.0
  33. */
  34. public function addMap($pattern, $controller)
  35. {
  36. // Sanitize and explode the pattern.
  37. $pattern = explode('/', trim(parse_url((string) $pattern, PHP_URL_PATH), ' /'));
  38. // Prepare the route variables
  39. $vars = array();
  40. // Initialize regular expression
  41. $regex = array();
  42. // Loop on each segment
  43. foreach ($pattern as $segment)
  44. {
  45. // Match a splat with no variable.
  46. if ($segment == '*')
  47. {
  48. $regex[] = '.*';
  49. }
  50. // Match a splat and capture the data to a named variable.
  51. elseif ($segment[0] == '*')
  52. {
  53. $vars[] = substr($segment, 1);
  54. $regex[] = '(.*)';
  55. }
  56. // Match an escaped splat segment.
  57. elseif ($segment[0] == '\\' && $segment[1] == '*')
  58. {
  59. $regex[] = '\*' . preg_quote(substr($segment, 2));
  60. }
  61. // Match an unnamed variable without capture.
  62. elseif ($segment == ':')
  63. {
  64. $regex[] = '[^/]*';
  65. }
  66. // Match a named variable and capture the data.
  67. elseif ($segment[0] == ':')
  68. {
  69. $vars[] = substr($segment, 1);
  70. $regex[] = '([^/]*)';
  71. }
  72. // Match a segment with an escaped variable character prefix.
  73. elseif ($segment[0] == '\\' && $segment[1] == ':')
  74. {
  75. $regex[] = preg_quote(substr($segment, 1));
  76. }
  77. // Match the standard segment.
  78. else
  79. {
  80. $regex[] = preg_quote($segment);
  81. }
  82. }
  83. $this->maps[] = array(
  84. 'regex' => chr(1) . '^' . implode('/', $regex) . '$' . chr(1),
  85. 'vars' => $vars,
  86. 'controller' => (string) $controller,
  87. );
  88. return $this;
  89. }
  90. /**
  91. * Add a route map to the router. If the pattern already exists it will be overwritten.
  92. *
  93. * @param array $maps A list of route maps to add to the router as $pattern => $controller.
  94. *
  95. * @return JApplicationWebRouter This object for method chaining.
  96. *
  97. * @since 3.0
  98. */
  99. public function addMaps($maps)
  100. {
  101. foreach ($maps as $pattern => $controller)
  102. {
  103. $this->addMap($pattern, $controller);
  104. }
  105. return $this;
  106. }
  107. /**
  108. * Parse the given route and return the name of a controller mapped to the given route.
  109. *
  110. * @param string $route The route string for which to find and execute a controller.
  111. *
  112. * @return string The controller name for the given route excluding prefix.
  113. *
  114. * @since 3.0
  115. * @throws InvalidArgumentException
  116. */
  117. protected function parseRoute($route)
  118. {
  119. $controller = false;
  120. // Trim the query string off.
  121. $route = preg_replace('/([^?]*).*/u', '\1', $route);
  122. // Sanitize and explode the route.
  123. $route = trim(parse_url($route, PHP_URL_PATH), ' /');
  124. // If the route is empty then simply return the default route. No parsing necessary.
  125. if ($route == '')
  126. {
  127. return $this->default;
  128. }
  129. // Iterate through all of the known route maps looking for a match.
  130. foreach ($this->maps as $rule)
  131. {
  132. if (preg_match($rule['regex'], $route, $matches))
  133. {
  134. // If we have gotten this far then we have a positive match.
  135. $controller = $rule['controller'];
  136. // Time to set the input variables.
  137. // We are only going to set them if they don't already exist to avoid overwriting things.
  138. foreach ($rule['vars'] as $i => $var)
  139. {
  140. $this->input->def($var, $matches[$i + 1]);
  141. // Don't forget to do an explicit set on the GET superglobal.
  142. $this->input->get->def($var, $matches[$i + 1]);
  143. }
  144. $this->input->def('_rawRoute', $route);
  145. break;
  146. }
  147. }
  148. // We were unable to find a route match for the request. Panic.
  149. if (!$controller)
  150. {
  151. throw new InvalidArgumentException(sprintf('Unable to handle request for route `%s`.', $route), 404);
  152. }
  153. return $controller;
  154. }
  155. }