-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.php
62 lines (55 loc) · 1.21 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Bee\Router;
/**
* Class1 Router
*
* @package Star\Router
*/
class Router implements RouterInterface
{
/**
* @var array
*/
protected $handlers = [];
/**
* 执行路由匹配
*
* @param string $method
* @param string $urlPath
* @return Handler|bool
*/
public function match(string $method, string $urlPath)
{
return $this->handlers[$method][$urlPath] ?? false;
}
/**
* 路由挂载
*
* @param CollectionInterface[]
*/
public function map(array $collections)
{
foreach ($collections as $collection) {
$this->mount($collection);
}
}
/**
* @param CollectionInterface $collection
*/
public function mount(CollectionInterface $collection)
{
$handlers = $collection->getHandlers();
foreach ($handlers as $handler) {
$this->setHandler($handler);
}
}
/**
* @param Handler $handler
*/
public function setHandler(Handler $handler): void
{
$method = $handler->getMethod();
$routePattern = $handler->getRoutePattern();
$this->handlers[$method][$routePattern] = $handler;
}
}