1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Application; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use BrainExe\Core\Annotations\Service; |
7
|
|
|
use BrainExe\Core\Traits\FileCacheTrait; |
8
|
|
|
use Psr\Log\InvalidArgumentException; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Symfony\Component\Routing\Route; |
11
|
|
|
use Symfony\Component\Routing\RouteCollection; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @Service(shared=false) |
15
|
|
|
*/ |
16
|
|
|
class SerializedRouteCollection extends RouteCollection |
17
|
|
|
{ |
18
|
|
|
use FileCacheTrait; |
19
|
|
|
|
20
|
|
|
const CACHE_FILE = 'routes'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $serializedRoutes; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Route[] |
29
|
|
|
*/ |
30
|
|
|
private $cache = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $name |
34
|
|
|
* @return Route |
35
|
|
|
* @throws InvalidArgumentException |
36
|
|
|
*/ |
37
|
3 |
|
public function get($name) |
38
|
|
|
{ |
39
|
3 |
|
$this->loadFromCache(); |
40
|
|
|
|
41
|
3 |
|
if (isset($this->cache[$name])) { |
42
|
1 |
|
return $this->cache[$name]; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
if (!isset($this->serializedRoutes[$name])) { |
46
|
1 |
|
throw new InvalidArgumentException(sprintf('invalid route: %s', $name)); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return $this->cache[$name] = unserialize( |
50
|
1 |
|
$this->serializedRoutes[$name], |
51
|
|
|
[ |
52
|
1 |
|
'allowed_classes' => [Route::class] |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function all() |
58
|
|
|
{ |
59
|
1 |
|
$this->initAll(); |
60
|
|
|
|
61
|
1 |
|
return $this->cache; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets the current RouteCollection as an Iterator that includes all routes. |
66
|
|
|
* |
67
|
|
|
* It implements \IteratorAggregate. |
68
|
|
|
* |
69
|
|
|
* @see all() |
70
|
|
|
* |
71
|
|
|
* @return ArrayIterator An \ArrayIterator object for iterating over routes |
72
|
|
|
*/ |
73
|
1 |
|
public function getIterator() |
74
|
|
|
{ |
75
|
1 |
|
$this->all(); |
76
|
|
|
|
77
|
1 |
|
return new ArrayIterator($this->cache); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets the number of Routes in this collection. |
82
|
|
|
* |
83
|
|
|
* @return int The number of routes |
84
|
|
|
*/ |
85
|
1 |
|
public function count() |
86
|
|
|
{ |
87
|
1 |
|
$this->loadFromCache(); |
88
|
|
|
|
89
|
1 |
|
return count($this->serializedRoutes); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @param Route $route |
95
|
|
|
*/ |
96
|
1 |
|
public function add($name, Route $route) |
97
|
|
|
{ |
98
|
1 |
|
$this->cache[$name] = $route; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array|string $name |
103
|
|
|
* @throws RuntimeException |
104
|
|
|
*/ |
105
|
1 |
|
public function remove($name) |
106
|
|
|
{ |
107
|
1 |
|
unset($name); |
108
|
1 |
|
throw new RuntimeException('RoutCollection::remove is not implemented'); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
private function initAll() |
112
|
|
|
{ |
113
|
1 |
|
$this->loadFromCache(); |
114
|
|
|
|
115
|
1 |
|
return array_map([$this, 'get'], array_keys($this->serializedRoutes)); |
116
|
|
|
} |
117
|
|
|
|
118
|
4 |
|
private function loadFromCache() |
119
|
|
|
{ |
120
|
4 |
|
if (null === $this->serializedRoutes) { |
121
|
4 |
|
$this->serializedRoutes = $this->includeFile(self::CACHE_FILE); |
|
|
|
|
122
|
|
|
} |
123
|
4 |
|
} |
124
|
|
|
} |
125
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..