|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Routing; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Exception\FileLoaderLoadException; |
|
6
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
|
7
|
|
|
use Symfony\Component\Config\Resource\ResourceInterface; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
9
|
|
|
|
|
10
|
|
|
class MethodCollectionBuilder |
|
11
|
|
|
{ |
|
12
|
|
|
private $methods = []; |
|
13
|
|
|
|
|
14
|
|
|
private $loader; |
|
15
|
|
|
private $defaults = []; |
|
16
|
|
|
private $resources = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param LoaderInterface $loader |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(LoaderInterface $loader = null) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->loader = $loader; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function import($resource, $type = null) |
|
27
|
|
|
{ |
|
28
|
|
|
$collection = $this->load($resource, $type); |
|
29
|
|
|
|
|
30
|
|
|
// create a builder from the MethodCollection |
|
31
|
|
|
$builder = $this->createBuilder(); |
|
32
|
|
|
foreach ($collection->all() as $name => $route) { |
|
33
|
|
|
$builder->addMethod($route, $name); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
foreach ($collection->getResources() as $resource) { |
|
37
|
|
|
$builder->addResource($resource); |
|
38
|
|
|
} |
|
39
|
|
|
$this->mount($builder); |
|
40
|
|
|
|
|
41
|
|
|
return $builder; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function add($path, $controller, $name) |
|
45
|
|
|
{ |
|
46
|
|
|
$method = new Method(null, $path); |
|
47
|
|
|
$method->setDefault('_controller', $controller); |
|
48
|
|
|
$this->addMethod($method, $name); |
|
49
|
|
|
|
|
50
|
|
|
return $method; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function createBuilder() |
|
54
|
|
|
{ |
|
55
|
|
|
return new self($this->loader); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function mount(MethodCollectionBuilder $builder) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->methods[] = $builder; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function addMethod(Method $route, $name) |
|
64
|
|
|
{ |
|
65
|
|
|
if (null === $name) { |
|
66
|
|
|
throw new InvalidArgumentException('name can\'t be null'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->methods[$name] = $route; |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function setDefault($key, $value) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->defaults[$key] = $value; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function addResource(ResourceInterface $resource) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->resources[] = $resource; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function build() |
|
89
|
|
|
{ |
|
90
|
|
|
$methodCollection = new MethodCollection(); |
|
91
|
|
|
|
|
92
|
|
|
foreach ($this->methods as $name => $method) { |
|
93
|
|
|
if ($method instanceof Method) { |
|
94
|
|
|
$method->setDefaults(array_merge($this->defaults, $method->getDefaults())); |
|
95
|
|
|
|
|
96
|
|
|
$methodCollection->add($name, $method); |
|
97
|
|
|
} else { |
|
98
|
|
|
/* @var self $route */ |
|
99
|
|
|
$subCollection = $route->build(); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
$methodCollection->addCollection($subCollection); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($this->resources as $resource) { |
|
105
|
|
|
$methodCollection->addResource($resource); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $methodCollection; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param $resource |
|
114
|
|
|
* @param null $type |
|
115
|
|
|
* @return mixed |
|
116
|
|
|
* @throws FileLoaderLoadException |
|
117
|
|
|
*/ |
|
118
|
|
|
private function load($resource, $type = null) |
|
119
|
|
|
{ |
|
120
|
|
|
if (null === $this->loader) { |
|
121
|
|
|
throw new \BadMethodCallException('Cannot import other routing resources: you must pass a LoaderInterface when constructing RouteCollectionBuilder.'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($this->loader->supports($resource, $type)) { |
|
125
|
|
|
return $this->loader->load($resource, $type); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (null === $resolver = $this->loader->getResolver()) { |
|
129
|
|
|
throw new FileLoaderLoadException($resource); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (false === $loader = $resolver->resolve($resource, $type)) { |
|
133
|
|
|
throw new FileLoaderLoadException($resource); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $loader->load($resource, $type); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.