Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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