|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Routing\Matcher\MethodMatcherInterface; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
7
|
|
|
use Symfony\Component\Config\ConfigCacheInterface; |
|
8
|
|
|
use Symfony\Component\Config\ConfigCacheFactoryInterface; |
|
9
|
|
|
use Symfony\Component\Config\ConfigCacheFactory; |
|
10
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
11
|
|
|
|
|
12
|
|
|
class MethodRouter |
|
13
|
|
|
{ |
|
14
|
|
|
protected $matcher; |
|
15
|
|
|
protected $context; |
|
16
|
|
|
protected $loader; |
|
17
|
|
|
protected $collection; |
|
18
|
|
|
protected $resource; |
|
19
|
|
|
protected $options = []; |
|
20
|
|
|
private $configCacheFactory; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(ContainerInterface $loader, $resource, array $options = []) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->loader = $loader; |
|
25
|
|
|
$this->resource = $resource; |
|
26
|
|
|
$this->context = new Method(null, ''); |
|
27
|
|
|
$this->setOptions($options); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function setOptions(array $options) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->options = [ |
|
33
|
|
|
'cache_dir' => null, |
|
34
|
|
|
'debug' => false, |
|
35
|
|
|
'matcher_class' => 'Cmobi\\RabbitmqBundle\\Routing\\Matcher\\MethodMatcher', |
|
36
|
|
|
'matcher_dumper_class' => 'Cmobi\\RabbitmqBundle\\Routing\\Matcher\\Dumper\\PhpMatcherDumper', |
|
37
|
|
|
'matcher_cache_class' => 'ProjectMethodMatcher', |
|
38
|
|
|
'resource_type' => null, |
|
39
|
|
|
'strict_requirements' => true, |
|
40
|
|
|
]; |
|
41
|
|
|
$invalid = []; |
|
42
|
|
|
|
|
43
|
|
|
foreach ($options as $key => $value) { |
|
44
|
|
|
|
|
45
|
|
|
if (array_key_exists($key, $this->options)) { |
|
46
|
|
|
$this->options[$key] = $value; |
|
47
|
|
|
} else { |
|
48
|
|
|
$invalid[] = $key; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ($invalid) { |
|
|
|
|
|
|
53
|
|
|
throw new \InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid))); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
public function setOption($key, $value) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
if (!array_key_exists($key, $this->options)) { |
|
60
|
|
|
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->options[$key] = $value; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
public function getOption($key) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
if (!array_key_exists($key, $this->options)) { |
|
69
|
|
|
throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->options[$key]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getMethodCollection() |
|
79
|
|
|
{ |
|
80
|
|
|
if (null === $this->collection) { |
|
81
|
|
|
$this->collection = $this->loader->get('routing.loader')->load($this->resource, $this->options['resource_type']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->collection; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setContext(RequestContext $context) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->context = $context; |
|
93
|
|
|
|
|
94
|
|
|
if (null !== $this->matcher) { |
|
95
|
|
|
$this->getMatcher()->setContext($context); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getContext() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->context; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->configCacheFactory = $configCacheFactory; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function warmUp($cacheDir) |
|
113
|
|
|
{ |
|
114
|
|
|
$currentDir = $this->getOption('cache_dir'); |
|
115
|
|
|
|
|
116
|
|
|
// force cache generation |
|
117
|
|
|
$this->setOption('cache_dir', $cacheDir); |
|
118
|
|
|
$this->getMatcher(); |
|
119
|
|
|
|
|
120
|
|
|
$this->setOption('cache_dir', $currentDir); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function match($pathinfo) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->getMatcher()->match($pathinfo); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return MethodMatcherInterface A MethodMatcherInterface instance |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getMatcher() |
|
135
|
|
|
{ |
|
136
|
|
|
if (null !== $this->matcher) { |
|
137
|
|
|
return $this->matcher; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) { |
|
141
|
|
|
$this->matcher = new $this->options['matcher_class']($this->getMethodCollection(), $this->context); |
|
142
|
|
|
|
|
143
|
|
|
return $this->matcher; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$class = $this->options['matcher_cache_class']; |
|
147
|
|
|
$that = $this; |
|
148
|
|
|
|
|
149
|
|
|
$cache = $this->getConfigCacheFactory()->cache($this->options['cache_dir'].'/'.$class.'.php', |
|
150
|
|
|
function (ConfigCacheInterface $cache) use ($that, $class) { |
|
151
|
|
|
$dumper = $that->getMatcherDumperInstance(); |
|
152
|
|
|
|
|
153
|
|
|
$options = array( |
|
154
|
|
|
'class' => $class |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
$cache->write($dumper->dump($options), $that->getMethodCollection()->getResources()); |
|
158
|
|
|
} |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
|
|
require_once $cache->getPath(); |
|
162
|
|
|
|
|
163
|
|
|
return $this->matcher = new $class($this->context); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function getGeneratorDumperInstance() |
|
167
|
|
|
{ |
|
168
|
|
|
return new $this->options['generator_dumper_class']($this->getMethodCollection()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function getMatcherDumperInstance() |
|
172
|
|
|
{ |
|
173
|
|
|
return new $this->options['matcher_dumper_class']($this->getMethodCollection()); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
private function getConfigCacheFactory() |
|
177
|
|
|
{ |
|
178
|
|
|
if (null === $this->configCacheFactory) { |
|
179
|
|
|
$this->configCacheFactory = new ConfigCacheFactory($this->options['debug']); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $this->configCacheFactory; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.