RouteCollector.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace FastRoute;
  3. class RouteCollector
  4. {
  5. /** @var RouteParser */
  6. protected $routeParser;
  7. /** @var DataGenerator */
  8. protected $dataGenerator;
  9. /** @var string */
  10. protected $currentGroupPrefix;
  11. /**
  12. * Constructs a route collector.
  13. *
  14. * @param RouteParser $routeParser
  15. * @param DataGenerator $dataGenerator
  16. */
  17. public function __construct(RouteParser $routeParser, DataGenerator $dataGenerator)
  18. {
  19. $this->routeParser = $routeParser;
  20. $this->dataGenerator = $dataGenerator;
  21. $this->currentGroupPrefix = '';
  22. }
  23. /**
  24. * Adds a route to the collection.
  25. *
  26. * The syntax used in the $route string depends on the used route parser.
  27. *
  28. * @param string|string[] $httpMethod
  29. * @param string $route
  30. * @param mixed $handler
  31. */
  32. public function addRoute($httpMethod, $route, $handler)
  33. {
  34. $route = $this->currentGroupPrefix . $route;
  35. $routeDatas = $this->routeParser->parse($route);
  36. foreach ((array) $httpMethod as $method) {
  37. foreach ($routeDatas as $routeData) {
  38. $this->dataGenerator->addRoute($method, $routeData, $handler);
  39. }
  40. }
  41. }
  42. /**
  43. * Create a route group with a common prefix.
  44. *
  45. * All routes created in the passed callback will have the given group prefix prepended.
  46. *
  47. * @param string $prefix
  48. * @param callable $callback
  49. */
  50. public function addGroup($prefix, callable $callback)
  51. {
  52. $previousGroupPrefix = $this->currentGroupPrefix;
  53. $this->currentGroupPrefix = $previousGroupPrefix . $prefix;
  54. $callback($this);
  55. $this->currentGroupPrefix = $previousGroupPrefix;
  56. }
  57. /**
  58. * Adds a GET route to the collection
  59. *
  60. * This is simply an alias of $this->addRoute('GET', $route, $handler)
  61. *
  62. * @param string $route
  63. * @param mixed $handler
  64. */
  65. public function get($route, $handler)
  66. {
  67. $this->addRoute('GET', $route, $handler);
  68. }
  69. /**
  70. * Adds a POST route to the collection
  71. *
  72. * This is simply an alias of $this->addRoute('POST', $route, $handler)
  73. *
  74. * @param string $route
  75. * @param mixed $handler
  76. */
  77. public function post($route, $handler)
  78. {
  79. $this->addRoute('POST', $route, $handler);
  80. }
  81. /**
  82. * Adds a PUT route to the collection
  83. *
  84. * This is simply an alias of $this->addRoute('PUT', $route, $handler)
  85. *
  86. * @param string $route
  87. * @param mixed $handler
  88. */
  89. public function put($route, $handler)
  90. {
  91. $this->addRoute('PUT', $route, $handler);
  92. }
  93. /**
  94. * Adds a DELETE route to the collection
  95. *
  96. * This is simply an alias of $this->addRoute('DELETE', $route, $handler)
  97. *
  98. * @param string $route
  99. * @param mixed $handler
  100. */
  101. public function delete($route, $handler)
  102. {
  103. $this->addRoute('DELETE', $route, $handler);
  104. }
  105. /**
  106. * Adds a PATCH route to the collection
  107. *
  108. * This is simply an alias of $this->addRoute('PATCH', $route, $handler)
  109. *
  110. * @param string $route
  111. * @param mixed $handler
  112. */
  113. public function patch($route, $handler)
  114. {
  115. $this->addRoute('PATCH', $route, $handler);
  116. }
  117. /**
  118. * Adds a HEAD route to the collection
  119. *
  120. * This is simply an alias of $this->addRoute('HEAD', $route, $handler)
  121. *
  122. * @param string $route
  123. * @param mixed $handler
  124. */
  125. public function head($route, $handler)
  126. {
  127. $this->addRoute('HEAD', $route, $handler);
  128. }
  129. /**
  130. * Returns the collected route data, as provided by the data generator.
  131. *
  132. * @return array
  133. */
  134. public function getData()
  135. {
  136. return $this->dataGenerator->getData();
  137. }
  138. }