Passed
Push — master ( 229425...f03095 )
by 世昌
01:53
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\NameFinder;
5
use nebula\component\route\RouteMatcher;
6
use nebula\component\route\RouteCollection;
7
use nebula\component\cacheable\CacheableTrait;
8
use nebula\component\cacheable\CacheableInterface;
9
10
/**
11
 * 应用
12
 *
13
 */
14
class GroupRoutes implements CacheableInterface
15
{
16
    use CacheableTrait;
17
    
18
    /**
19
     * 名称查找器
20
     *
21
     * @var NameFinder
22
     */
23
    protected $finder;
24
25
    /**
26
     * 路由集合
27
     *
28
     * @var RouteCollection[]
29
     */
30
    protected $routes = [];
31
32
    /**
33
     * 默认分组
34
     *
35
     * @var string
36
     */
37
    protected $default = 'global:';
38
39
    /**
40
     * 组名
41
     *
42
     * @var string
43
     */
44
    protected $name;
45
46
    public function __construct(string $name, NameFinder $finder)
47
    {
48
        $this->finder = $finder;
49
        $this->name = $name;
50
    }
51
52
    /**
53
     * 注册路由
54
     *
55
     * @param string|null $group
56
     * @param RouteMatcher $router
57
     * @return void
58
     */
59
    public function add(?string $group = null, string $name, RouteMatcher $router)
60
    {
61
        $group =  $group ? $this->finder->getFullName($group) : $this->default;
62
        if (\array_key_exists($group, $this->routes)) {
63
            $this->routes[$group]->add($name, $router);
64
        } else {
65
            $this->routes[$group] = new RouteCollection([$name => $router]);
66
        }
67
    }
68
69
    /**
70
     * 搜索路由
71
     *
72
     * @param string $name
73
     * @return RouteMatcher|null
74
     */
75
    public function find(string $name, ?string $default = null):?RouteMatcher
76
    {
77
        list($group, $name) = $this->finder->info($name, $default ?? $this->default);
78
        if (\array_key_exists($group, $this->routes)) {
79
            return $this->routes[$group]->get($name);
80
        }
81
        return null;
82
    }
83
84
    public function getCacheProperty():array
85
    {
86
        return ['routes'];
87
    }
88
89
    public function getCacheKey():string
90
    {
91
        return 'routes:'.$this->name;
92
    }
93
}
94