|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Routing\Matcher; |
|
4
|
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Routing\Method; |
|
6
|
|
|
use Cmobi\RabbitmqBundle\Routing\MethodCollection; |
|
7
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException; |
|
8
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
|
9
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
10
|
|
|
|
|
11
|
|
|
class MethodMatcher implements MethodMatcherInterface |
|
12
|
|
|
{ |
|
13
|
|
|
protected $context; |
|
14
|
|
|
protected $methods; |
|
15
|
|
|
protected $allow = []; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(MethodCollection $methods, Method $context) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->methods = $methods; |
|
20
|
|
|
$this->context = $context; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function match($path) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->allow = []; |
|
26
|
|
|
|
|
27
|
|
|
if ($ret = $this->matchCollection($path, $this->methods)) { |
|
28
|
|
|
return $ret; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (0 < count($this->allow)) { |
|
32
|
|
|
throw new MethodNotAllowedException(array_unique($this->allow)); |
|
33
|
|
|
} |
|
34
|
|
|
throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $path)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function matchCollection($path, MethodCollection $methods) |
|
38
|
|
|
{ |
|
39
|
|
|
foreach ($methods as $name => $method) { |
|
40
|
|
|
|
|
41
|
|
|
if ($method->getName() !== $path) { |
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->getAttributes($method, $name, $method->getOptions()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return []; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function getAttributes(Method $method, $name, array $attributes) |
|
52
|
|
|
{ |
|
53
|
|
|
$attributes['_method'] = $name; |
|
54
|
|
|
|
|
55
|
|
|
return $this->mergeDefaults($attributes, $method->getDefaults()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setContext(RequestContext $context) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->context = $context; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getContext() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->context; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function mergeDefaults($params, $defaults) |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($params as $key => $value) { |
|
77
|
|
|
if (!is_int($key)) { |
|
78
|
|
|
$defaults[$key] = $value; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $defaults; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|