Passed
Push — master ( 6740b1...356763 )
by 世昌
02:25
created

GroupRoutes::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\route;
3
4
use nebula\application\Request;
5
use nebula\component\route\RouteMatcher;
6
use nebula\application\route\MatchResult;
7
use nebula\component\route\RouteCollection;
8
use nebula\component\cacheable\CacheableTrait;
9
use nebula\component\cacheable\CacheableInterface;
10
11
/**
12
 * 应用
13
 *
14
 */
15
class GroupRoutes implements CacheableInterface
16
{
17
    use CacheableTrait;
18
    
19
    /**
20
     * 路由集合
21
     *
22
     * @var RouteCollection[]
23
     */
24
    protected $routes = [];
25
26
    /**
27
     * 默认分组
28
     *
29
     * @var string
30
     */
31
    protected $default = 'global:';
32
33
    /**
34
     * 组名
35
     *
36
     * @var string
37
     */
38
    protected $name;
39
40
    /**
41
     * 分组名
42
     *
43
     * @param string $name
44
     */
45
    public function __construct(string $name)
46
    {
47
        $this->name = $name;
48
    }
49
50
    /**
51
     * 注册路由
52
     *
53
     * @param string|null $module 模块名
54
     * @param string $name 路由名
55
     * @param RouteMatcher $router 路由匹配
56
     * @return void
57
     */
58
    public function add(?string $module = null, string $name, RouteMatcher $router)
59
    {
60
        $module =  $module ?: $this->default;
61
        if (\array_key_exists($module, $this->routes)) {
62
            $this->routes[$module]->add($name, $router);
63
        } else {
64
            $this->routes[$module] = new RouteCollection([$name => $router]);
65
        }
66
    }
67
68
    /**
69
     * 合并路由组
70
     *
71
     * @param GroupRoutes $routes
72
     * @return void
73
     */
74
    public function merge(GroupRoutes $routes)
75
    {
76
        $name = $routes->name;
77
        if (\array_key_exists($name, $this->routes)) {
78
            foreach ($routes->routes as $module => $route) {
79
                $this->routes[$name][$module]->merge($route);
80
            }
81
        } else {
82
            $this->routes[$name] = $routes;
83
        }
84
    }
85
86
    /**
87
     * 匹配路由
88
     *
89
     * @param Request $request
90
     * @return MatchResult|null
91
     */
92
    public function match(Request $request):?MatchResult
93
    {
94
        foreach ($this->routes as $module => $collection) {
95
            foreach ($collection as $name => $matcher) {
96
                if (($parameter = $matcher->match($request)) !== null) {
97
                    return new MatchResult($module, $name, $matcher, $parameter);
98
                }
99
            }
100
        }
101
        return null;
102
    }
103
104
    /**
105
     * 搜索路由
106
     *
107
     * @param string|null $module
108
     * @param string $name
109
     * @return RouteMatcher|null
110
     */
111
    public function find(?string $module = null, string $name):?RouteMatcher
112
    {
113
        $module =  $module ?: $this->default;
114
        if (\array_key_exists($module, $this->routes)) {
115
            return $this->routes[$module]->get($name);
116
        }
117
        return null;
118
    }
119
120
    public function getCacheProperty():array
121
    {
122
        return ['routes'];
123
    }
124
125
    public function getCacheKey():string
126
    {
127
        return 'routes:'.$this->name;
128
    }
129
}
130