-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathRouteCollection.php
114 lines (95 loc) · 2.43 KB
/
RouteCollection.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Enqueue\Client;
class RouteCollection
{
/**
* @var Route[]
*/
private $routes;
/**
* @var Route[]
*/
private $commandRoutes;
/**
* @var Route[]
*/
private $topicRoutes;
/**
* @param Route[] $routes
*/
public function __construct(array $routes)
{
$this->routes = $routes;
}
public function add(Route $route): void
{
$this->routes[] = $route;
$this->topicRoutes = null;
$this->commandRoutes = null;
}
/**
* @return Route[]
*/
public function all(): array
{
return $this->routes;
}
/**
* @return Route[]
*/
public function command(string $command): ?Route
{
if (null === $this->commandRoutes) {
$commandRoutes = [];
foreach ($this->routes as $route) {
if ($route->isCommand()) {
$commandRoutes[$route->getSource()] = $route;
}
}
$this->commandRoutes = $commandRoutes;
}
return array_key_exists($command, $this->commandRoutes) ? $this->commandRoutes[$command] : null;
}
/**
* @return Route[]
*/
public function topic(string $topic): array
{
if (null === $this->topicRoutes) {
$topicRoutes = [];
foreach ($this->routes as $route) {
if ($route->isTopic()) {
$topicRoutes[$route->getSource()][$route->getProcessor()] = $route;
}
}
$this->topicRoutes = $topicRoutes;
}
return array_key_exists($topic, $this->topicRoutes) ? $this->topicRoutes[$topic] : [];
}
public function topicAndProcessor(string $topic, string $processor): ?Route
{
$routes = $this->topic($topic);
foreach ($routes as $route) {
if ($route->getProcessor() === $processor) {
return $route;
}
}
return null;
}
public function toArray(): array
{
$rawRoutes = [];
foreach ($this->routes as $route) {
$rawRoutes[] = $route->toArray();
}
return $rawRoutes;
}
public static function fromArray(array $rawRoutes): self
{
$routes = [];
foreach ($rawRoutes as $rawRoute) {
$routes[] = Route::fromArray($rawRoute);
}
return new self($routes);
}
}