1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\Routing\Conventional; |
4
|
|
|
|
5
|
|
|
class Config |
6
|
|
|
{ |
7
|
|
|
public $name; |
8
|
|
|
public $parent; |
9
|
|
|
private $config; |
10
|
|
|
|
11
|
|
|
public function __construct($name, $config = null, Config $parent = null) |
12
|
|
|
{ |
13
|
|
|
$this->name = $name; |
14
|
|
|
if (!is_array($config)) { |
15
|
|
|
$config = array('pattern' => $config); |
16
|
|
|
} |
17
|
|
|
$this->config = $config; |
18
|
|
|
$this->parent = $parent; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getController() |
22
|
|
|
{ |
23
|
|
|
if ($this->get('controller')) { |
24
|
|
|
return $this->get('controller'); |
25
|
|
|
} |
26
|
|
|
if ($this->parent) { |
27
|
|
|
return $this->parent->getController(); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getView() |
32
|
|
|
{ |
33
|
|
|
if ($this->get('view')) { |
34
|
|
|
return $this->get('view'); |
35
|
|
|
} |
36
|
|
|
if ($this->parent) { |
37
|
|
|
return $this->parent->getView(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getPattern() |
42
|
|
|
{ |
43
|
|
|
return $this->get('pattern'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getPrefix() |
47
|
|
|
{ |
48
|
|
|
return $this->get('prefix'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getDefaults() |
52
|
|
|
{ |
53
|
|
|
$defaults = $this->get('defaults', array( |
54
|
|
|
'_format' => 'html' |
55
|
|
|
)); |
56
|
|
|
if ($this->parent) { |
57
|
|
|
$defaults = array_merge($this->parent->getDefaults(), $defaults); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $defaults; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getRequirements() |
64
|
|
|
{ |
65
|
|
|
$requirements = $this->get('requirements', array()); |
66
|
|
|
if ($this->parent) { |
67
|
|
|
$requirements = array_merge($this->parent->getRequirements(), $requirements); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $requirements; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getMethods() |
74
|
|
|
{ |
75
|
|
|
$default = array(); |
76
|
|
|
if ($this->parent) { |
77
|
|
|
$default = $this->parent->getMethods(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->get('methods', $default); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getElements() |
84
|
|
|
{ |
85
|
|
|
$elements = $this->getDefaultElements(); |
86
|
|
|
|
87
|
|
|
foreach ($elements as $key => $element) { |
88
|
|
|
if (!in_array($key, $this->get('elements', array_keys($elements)))) { |
89
|
|
|
unset($elements[$key]); // TODO improve this filter, couldn't get it work with array_intersect_* |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$ignore = array_flip(array( |
94
|
|
|
'parent', |
95
|
|
|
'elements', |
96
|
|
|
'pattern', |
97
|
|
|
'prefix', |
98
|
|
|
'controller', |
99
|
|
|
'methods', |
100
|
|
|
'defaults', |
101
|
|
|
'requirements', |
102
|
|
|
)); |
103
|
|
|
|
104
|
|
|
$others = array_keys(array_diff_key($this->config, $ignore, $elements)); |
105
|
|
|
foreach ($others as $name) { |
106
|
|
|
$config = $this->config[$name] ?: array(); |
107
|
|
|
if (!is_array($config)) { |
108
|
|
|
$config = array('pattern' => $config); |
109
|
|
|
} |
110
|
|
|
$prefix = false === $this->getPrefix() ?: $this->name; |
111
|
|
|
$elements[$name] = new static( |
112
|
|
|
$name, |
113
|
|
|
array_merge(array('pattern' => $name, 'prefix' => $prefix), $config), |
114
|
|
|
$this |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $elements; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function isRepresentant() |
122
|
|
|
{ |
123
|
|
|
return $this->get('is_representant', false); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getRepresentant() |
127
|
|
|
{ |
128
|
|
|
$elements = $this->getElements(); |
129
|
|
|
return current(array_filter($elements, function($element) { |
|
|
|
|
130
|
|
|
return $element->isRepresentant(); |
131
|
|
|
})); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function merge($path, array $config) |
135
|
|
|
{ |
136
|
|
|
return array_merge($config, $this->get($path, array())); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function get($path, $default = null) |
140
|
|
|
{ |
141
|
|
|
if (isset($this->config[$path])) { |
142
|
|
|
return $this->config[$path]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $default; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function getDefaultElements() |
149
|
|
|
{ |
150
|
|
|
return array( |
151
|
|
|
'index' => new static('index', $this->merge('index', array( |
152
|
|
|
'methods' => array('GET'), |
153
|
|
|
'prefix' => $this->name, |
154
|
|
|
)), $this), |
155
|
|
|
'new' => new static('new', $this->merge('new', array( |
156
|
|
|
'methods' => array('GET'), |
157
|
|
|
'prefix' => $this->name, |
158
|
|
|
)), $this), |
159
|
|
|
'create' => new static('create', $this->merge('create', array( |
160
|
|
|
'methods' => array('POST'), |
161
|
|
|
'prefix' => $this->name, |
162
|
|
|
)), $this), |
163
|
|
|
'edit' => new static('edit', $this->merge('edit', array( |
164
|
|
|
'methods' => array('GET'), |
165
|
|
|
'requirements' => array('id' => '\d+'), |
166
|
|
|
'prefix' => $this->name, |
167
|
|
|
)), $this), |
168
|
|
|
'update' => new static('update', $this->merge('update', array( |
169
|
|
|
'methods' => array('PUT'), |
170
|
|
|
'requirements' => array('id' => '\d+'), |
171
|
|
|
'prefix' => $this->name, |
172
|
|
|
)), $this), |
173
|
|
|
'show' => new static('show', $this->merge('show', array( |
174
|
|
|
'methods' => array('GET'), |
175
|
|
|
'requirements' => array('id' => '\d+'), |
176
|
|
|
'is_representant' => true, |
177
|
|
|
'prefix' => $this->name, |
178
|
|
|
)), $this), |
179
|
|
|
'delete' => new static('delete', $this->merge('delete', array( |
180
|
|
|
'methods' => array('DELETE'), |
181
|
|
|
'requirements' => array('id' => '\d+'), |
182
|
|
|
'prefix' => $this->name, |
183
|
|
|
)), $this), |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|