PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/legacy/Zikula/Routing/UrlRouter.php

https://github.com/antoniom/core
PHP | 154 lines | 61 code | 19 blank | 74 comment | 10 complexity | c59b2978c3c28816fb8937b359996673 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright 2010 Zikula Foundation.
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. * @subpackage Routing
  11. *
  12. * Please see the NOTICE file distributed with this source code for further
  13. * information regarding copyright and licensing.
  14. */
  15. /**
  16. * Url router base class.
  17. */
  18. class Zikula_Routing_UrlRouter
  19. {
  20. /**
  21. * The list of managed routes (Zikula_Routing_UrlRoute instances).
  22. *
  23. * @var array
  24. */
  25. protected $routes;
  26. /**
  27. * Constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->routes = array();
  32. }
  33. /**
  34. * Generate a short url for given arguments.
  35. *
  36. * @param string $name Optional name of route to be used (if not set the route will be selected based on given params).
  37. * @param array $params The arguments to be processed by the created url.
  38. *
  39. * @return mixed string With created url or false on error.
  40. */
  41. public function generate($name = '', array $params = array())
  42. {
  43. // reference to the route used for url creation
  44. $route = null;
  45. if ($name) {
  46. // a certain route should be used
  47. if (!isset($this->routes[$name])) {
  48. // this route does not exist, so we abort here
  49. return false;
  50. }
  51. // use this route
  52. $route = $this->routes[$name];
  53. } else {
  54. // determine the route based on given params
  55. foreach ($this->routes as $testRoute) {
  56. if (!$testRoute->matchParameters($params)) {
  57. // this route does not fit to our arguments, so we skip it
  58. continue;
  59. }
  60. // use this route
  61. $route = $testRoute;
  62. // exit loop as the first match wins
  63. break;
  64. }
  65. // abort if we did not found a route to use
  66. if (is_null($route)) {
  67. return false;
  68. }
  69. }
  70. // let the route do the actual url creation
  71. $url = $route->generate($params);
  72. // return the result
  73. return $url;
  74. }
  75. /**
  76. * Parse a given url and return the params read out of it.
  77. *
  78. * @param string $url The input url.
  79. *
  80. * @throws InvalidArgumentException If the Url is empty.
  81. *
  82. * @return mixed array With determined params or false on error.
  83. */
  84. public function parse($url = '')
  85. {
  86. // return if we have an empty input
  87. if (empty($url)) {
  88. throw new InvalidArgumentException('Zikula_Routing_UrlRouter->parse: $url was empty!');
  89. }
  90. // reference to resulting params
  91. $params = null;
  92. // search for the right route for given url
  93. foreach ($this->routes as $testRoute) {
  94. // check if this route does the job
  95. $testParams = $testRoute->matchesUrl($url);
  96. // if not...
  97. if ($testParams === false) {
  98. // skip it
  99. continue;
  100. }
  101. // store parameters
  102. $params = $testParams;
  103. // exit loop as the first match wins
  104. break;
  105. }
  106. // if we found something return it
  107. if (!is_null($params)) {
  108. return $params;
  109. }
  110. // else return false
  111. return false;
  112. }
  113. /**
  114. * Set (or add) a certain route to this router instance.
  115. *
  116. * @param string $name Storage name for the route.
  117. * @param Zikula_Routing_UrlRoute $route The actual route instance.
  118. *
  119. * @throws InvalidArgumentException If name is empty.
  120. *
  121. * @return void
  122. */
  123. public function set($name, Zikula_Routing_UrlRoute $route)
  124. {
  125. // return if we have an empty input
  126. if (empty($name)) {
  127. throw new InvalidArgumentException('Zikula_Routing_UrlRouter->set: $name was empty!');
  128. }
  129. if (is_null($route)) {
  130. throw new InvalidArgumentException('Zikula_Routing_UrlRouter->set: $route was null!');
  131. }
  132. // store given route
  133. $this->routes[$name] = $route;
  134. }
  135. }