|
1
|
|
|
<?php |
|
2
|
|
|
namespace suda\component\route; |
|
3
|
|
|
|
|
4
|
|
|
use Iterator; |
|
5
|
|
|
use ArrayIterator; |
|
6
|
|
|
use IteratorAggregate; |
|
7
|
|
|
use suda\component\route\RouteMatcher; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* 路由集合 |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
class RouteCollection implements IteratorAggregate |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* 属性 |
|
17
|
|
|
* |
|
18
|
|
|
* @var RouteMatcher[] |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $collection = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* 创建集合 |
|
24
|
|
|
* |
|
25
|
|
|
* @param RouteMatcher[] $collection |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(array $collection=[]) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->mergeArray($collection); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* 合并集合 |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $collection |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function mergeArray(array $collection=[]) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->collection = array_merge($this->collection, $collection); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* 合并 |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $route |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function merge(RouteCollection $route) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->collection = array_merge($this->collection, $route->collection); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* 添加集合 |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name |
|
58
|
|
|
* @param RouteMatcher $collection |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
public function add(string $name, RouteMatcher $collection) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->collection[$name] = $collection; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* 获取集合 |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* @return RouteMatcher|null |
|
71
|
|
|
*/ |
|
72
|
|
|
public function get(string $name):?RouteMatcher |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->collection[$name] ?? null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* 获取迭代器 |
|
79
|
|
|
* |
|
80
|
|
|
* @return RouteMatcher[] |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getCollection():array |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->collection; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* 从文件创建 |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $path |
|
91
|
|
|
* @return self |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function fromFile(string $path) |
|
94
|
|
|
{ |
|
95
|
|
|
$collection = \unserialize(\file_get_contents($path)); |
|
96
|
|
|
return new static($collection); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* 保存到文件 |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $path |
|
103
|
|
|
* @return boolean |
|
104
|
|
|
*/ |
|
105
|
|
|
public function save(string $path):bool |
|
106
|
|
|
{ |
|
107
|
|
|
return \file_put_contents($path, \serialize($this->collection)) > 0; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getIterator():Iterator |
|
111
|
|
|
{ |
|
112
|
|
|
return new ArrayIterator($this->collection); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|