123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace FastRoute;
- class RouteCollector
- {
-
- protected $routeParser;
-
- protected $dataGenerator;
-
- protected $currentGroupPrefix;
-
- public function __construct(RouteParser $routeParser, DataGenerator $dataGenerator)
- {
- $this->routeParser = $routeParser;
- $this->dataGenerator = $dataGenerator;
- $this->currentGroupPrefix = '';
- }
-
- public function addRoute($httpMethod, $route, $handler)
- {
- $route = $this->currentGroupPrefix . $route;
- $routeDatas = $this->routeParser->parse($route);
- foreach ((array) $httpMethod as $method) {
- foreach ($routeDatas as $routeData) {
- $this->dataGenerator->addRoute($method, $routeData, $handler);
- }
- }
- }
-
- public function addGroup($prefix, callable $callback)
- {
- $previousGroupPrefix = $this->currentGroupPrefix;
- $this->currentGroupPrefix = $previousGroupPrefix . $prefix;
- $callback($this);
- $this->currentGroupPrefix = $previousGroupPrefix;
- }
-
- public function get($route, $handler)
- {
- $this->addRoute('GET', $route, $handler);
- }
-
- public function post($route, $handler)
- {
- $this->addRoute('POST', $route, $handler);
- }
-
- public function put($route, $handler)
- {
- $this->addRoute('PUT', $route, $handler);
- }
-
- public function delete($route, $handler)
- {
- $this->addRoute('DELETE', $route, $handler);
- }
-
- public function patch($route, $handler)
- {
- $this->addRoute('PATCH', $route, $handler);
- }
-
- public function head($route, $handler)
- {
- $this->addRoute('HEAD', $route, $handler);
- }
-
- public function getData()
- {
- return $this->dataGenerator->getData();
- }
- }
|