|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: batanov.pavel |
|
5
|
|
|
* Date: 12.02.2016 |
|
6
|
|
|
* Time: 13:15 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Bankiru\Api\Rpc\Routing; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\Config\Resource\ResourceInterface; |
|
12
|
|
|
|
|
13
|
|
|
class MethodCollection implements \IteratorAggregate, \Countable |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Route[] */ |
|
16
|
|
|
private $routes = []; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ResourceInterface[] |
|
19
|
|
|
*/ |
|
20
|
|
|
private $resources = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param $method |
|
24
|
|
|
* |
|
25
|
|
|
* @return Route |
|
26
|
|
|
* @throws \OutOfBoundsException |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function get($method) |
|
29
|
|
|
{ |
|
30
|
1 |
|
if (!$this->has($method)) { |
|
31
|
|
|
throw new \OutOfBoundsException(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
return $this->routes[$method]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
8 |
|
public function has($method) |
|
38
|
|
|
{ |
|
39
|
8 |
|
return array_key_exists($method, $this->routes); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
8 |
|
public function add($method, Route $route) |
|
43
|
|
|
{ |
|
44
|
8 |
|
if ($this->has($method)) { |
|
45
|
|
|
throw new \LogicException(sprintf('Trying to replace method %s', $method)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
8 |
|
$this->replace($method, $route); |
|
49
|
8 |
|
} |
|
50
|
|
|
|
|
51
|
8 |
|
public function replace($method, Route $route) |
|
52
|
|
|
{ |
|
53
|
8 |
|
unset($this->routes[$method]); |
|
54
|
|
|
|
|
55
|
8 |
|
$this->routes[$method] = $route; |
|
56
|
8 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function addPrefix($prefix) |
|
59
|
|
|
{ |
|
60
|
|
|
if ('' === $prefix) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
foreach ($this->routes as $name => $route) { |
|
65
|
|
|
unset($this->routes[$name]); |
|
66
|
|
|
$method = $prefix . $route->getMethod(); |
|
67
|
|
|
$route->setMethod($method); |
|
68
|
|
|
$this->routes[$method] = $route; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function addContext($context) |
|
73
|
|
|
{ |
|
74
|
|
|
foreach ($this->routes as $route) { |
|
75
|
|
|
if ($route->inheritContext()) { |
|
76
|
|
|
$route->addContext($context); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
8 |
|
public function addCollection(MethodCollection $collection) |
|
82
|
|
|
{ |
|
83
|
|
|
// we need to remove all routes with the same names first because just replacing them |
|
84
|
|
|
// would not place the new route at the end of the merged array |
|
85
|
8 |
|
foreach ($collection->all() as $name => $route) { |
|
86
|
8 |
|
unset($this->routes[$name]); |
|
87
|
8 |
|
$this->routes[$name] = $route; |
|
88
|
8 |
|
} |
|
89
|
|
|
|
|
90
|
8 |
|
$this->resources = array_merge($this->resources, $collection->getResources()); |
|
91
|
8 |
|
} |
|
92
|
|
|
|
|
93
|
8 |
|
public function all() |
|
94
|
|
|
{ |
|
95
|
8 |
|
return $this->routes; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns an array of resources loaded to build this collection. |
|
100
|
|
|
* |
|
101
|
|
|
* @return ResourceInterface[] An array of resources |
|
102
|
|
|
*/ |
|
103
|
8 |
|
public function getResources() |
|
104
|
|
|
{ |
|
105
|
8 |
|
return array_unique($this->resources); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Gets the current RouteCollection as an Iterator that includes all routes. |
|
110
|
|
|
* |
|
111
|
|
|
* It implements \IteratorAggregate. |
|
112
|
|
|
* |
|
113
|
|
|
* @see all() |
|
114
|
|
|
* |
|
115
|
|
|
* @return \ArrayIterator An \ArrayIterator object for iterating over routes |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getIterator() |
|
118
|
|
|
{ |
|
119
|
|
|
return new \ArrayIterator($this->routes); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Gets the number of Routes in this collection. |
|
124
|
|
|
* |
|
125
|
|
|
* @return int The number of routes |
|
126
|
|
|
*/ |
|
127
|
|
|
public function count() |
|
128
|
|
|
{ |
|
129
|
|
|
return count($this->routes); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param ResourceInterface $resource |
|
134
|
|
|
*/ |
|
135
|
|
|
public function addResource(ResourceInterface $resource) { $this->resources[] = $resource; } |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $method |
|
139
|
|
|
* |
|
140
|
|
|
* @return Route|null |
|
141
|
|
|
*/ |
|
142
|
7 |
|
public function match($method) |
|
143
|
|
|
{ |
|
144
|
7 |
|
foreach ($this->routes as $route) { |
|
145
|
7 |
|
if ($route->getMethod() === $method) { |
|
146
|
6 |
|
return $route; |
|
147
|
|
|
} |
|
148
|
1 |
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|