1 | <?php |
||
33 | class Configuration implements ConfigurationInterface |
||
34 | { |
||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $debug; |
||
39 | |||
40 | /** |
||
41 | * @param bool $debug Whether to use the debug mode |
||
42 | */ |
||
43 | 39 | public function __construct($debug) |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 39 | public function getConfigTreeBuilder() |
|
52 | { |
||
53 | 39 | $treeBuilder = new TreeBuilder(); |
|
54 | 39 | $rootNode = $treeBuilder->root('fos_http_cache'); |
|
55 | |||
56 | $rootNode |
||
57 | 39 | ->validate() |
|
58 | 39 | ->ifTrue(function ($v) { |
|
59 | 38 | return $v['cache_manager']['enabled'] |
|
60 | 38 | && !isset($v['proxy_client']) |
|
61 | 38 | && !isset($v['cache_manager']['custom_proxy_client']) |
|
62 | ; |
||
63 | 39 | }) |
|
64 | 39 | ->then(function ($v) { |
|
65 | 12 | if ('auto' === $v['cache_manager']['enabled']) { |
|
66 | 11 | $v['cache_manager']['enabled'] = false; |
|
67 | |||
68 | 11 | return $v; |
|
69 | } |
||
70 | 1 | throw new InvalidConfigurationException('You need to configure a proxy_client or specify a custom_proxy_client to use the cache_manager.'); |
|
71 | 39 | }) |
|
72 | 39 | ->end() |
|
73 | 39 | ->validate() |
|
74 | 39 | ->ifTrue(function ($v) { |
|
75 | 37 | return $v['tags']['enabled'] && !$v['cache_manager']['enabled']; |
|
76 | 39 | }) |
|
77 | 39 | ->then(function ($v) { |
|
78 | 13 | if ('auto' === $v['tags']['enabled']) { |
|
79 | 12 | $v['tags']['enabled'] = false; |
|
80 | |||
81 | 12 | return $v; |
|
82 | } |
||
83 | 1 | throw new InvalidConfigurationException('You need to configure a proxy_client to get the cache_manager needed for tag handling.'); |
|
84 | 39 | }) |
|
85 | 39 | ->end() |
|
86 | 39 | ->validate() |
|
87 | 39 | ->ifTrue(function ($v) { |
|
88 | 36 | return $v['invalidation']['enabled'] && !$v['cache_manager']['enabled']; |
|
89 | 39 | }) |
|
90 | 39 | ->then(function ($v) { |
|
91 | 12 | if ('auto' === $v['invalidation']['enabled']) { |
|
92 | 11 | $v['invalidation']['enabled'] = false; |
|
93 | |||
94 | 11 | return $v; |
|
95 | } |
||
96 | 1 | throw new InvalidConfigurationException('You need to configure a proxy_client to get the cache_manager needed for invalidation handling.'); |
|
97 | 39 | }) |
|
98 | 39 | ->end() |
|
99 | 39 | ->validate() |
|
100 | 39 | ->ifTrue( |
|
101 | 39 | function ($v) { |
|
102 | 35 | return $v['user_context']['logout_handler']['enabled'] |
|
103 | 35 | && !isset($v['proxy_client']); |
|
104 | 39 | } |
|
105 | ) |
||
106 | 39 | ->then(function ($v) { |
|
107 | 13 | if ('auto' === $v['user_context']['logout_handler']['enabled']) { |
|
108 | 13 | $v['user_context']['logout_handler']['enabled'] = false; |
|
109 | |||
110 | 13 | return $v; |
|
111 | } |
||
112 | throw new InvalidConfigurationException('You need to configure a proxy_client for the logout_handler.'); |
||
113 | 39 | }) |
|
114 | ; |
||
115 | |||
116 | 39 | $this->addCacheableResponseSection($rootNode); |
|
117 | 39 | $this->addCacheControlSection($rootNode); |
|
118 | 39 | $this->addProxyClientSection($rootNode); |
|
119 | 39 | $this->addCacheManagerSection($rootNode); |
|
120 | 39 | $this->addTagSection($rootNode); |
|
121 | 39 | $this->addInvalidationSection($rootNode); |
|
122 | 39 | $this->addUserContextListenerSection($rootNode); |
|
123 | 39 | $this->addFlashMessageSection($rootNode); |
|
124 | 39 | $this->addTestSection($rootNode); |
|
125 | 39 | $this->addDebugSection($rootNode); |
|
126 | |||
127 | 39 | return $treeBuilder; |
|
128 | } |
||
129 | |||
130 | 39 | private function addCacheableResponseSection(ArrayNodeDefinition $rootNode) |
|
131 | { |
||
132 | $rootNode |
||
133 | 39 | ->children() |
|
134 | 39 | ->arrayNode('cacheable') |
|
135 | 39 | ->addDefaultsIfNotSet() |
|
136 | 39 | ->children() |
|
137 | 39 | ->arrayNode('response') |
|
138 | 39 | ->addDefaultsIfNotSet() |
|
139 | 39 | ->children() |
|
140 | 39 | ->arrayNode('additional_status') |
|
141 | 39 | ->prototype('scalar')->end() |
|
142 | 39 | ->info('Additional response HTTP status codes that will be considered cacheable.') |
|
143 | 39 | ->end() |
|
144 | 39 | ->scalarNode('expression') |
|
145 | 39 | ->defaultNull() |
|
146 | 39 | ->info('Expression to decide whether response is cacheable. Replaces the default status codes.') |
|
147 | 39 | ->end() |
|
148 | 39 | ->end() |
|
149 | |||
150 | 39 | ->validate() |
|
151 | 39 | ->ifTrue(function ($v) { |
|
152 | 3 | return !empty($v['additional_status']) && !empty($v['expression']); |
|
153 | 39 | }) |
|
154 | 39 | ->thenInvalid('You may not set both additional_status and expression.') |
|
155 | 39 | ->end() |
|
156 | 39 | ->end() |
|
157 | 39 | ->end() |
|
158 | 39 | ->end(); |
|
159 | 39 | } |
|
160 | |||
161 | /** |
||
162 | * Cache header control main section. |
||
163 | * |
||
164 | * @param ArrayNodeDefinition $rootNode |
||
165 | */ |
||
166 | 39 | private function addCacheControlSection(ArrayNodeDefinition $rootNode) |
|
167 | { |
||
168 | $rules = $rootNode |
||
169 | 39 | ->children() |
|
170 | 39 | ->arrayNode('cache_control') |
|
171 | 39 | ->fixXmlConfig('rule') |
|
172 | 39 | ->children() |
|
173 | 39 | ->arrayNode('defaults') |
|
174 | 39 | ->addDefaultsIfNotSet() |
|
175 | 39 | ->children() |
|
176 | 39 | ->booleanNode('overwrite') |
|
177 | 39 | ->info('Whether to overwrite existing cache headers') |
|
178 | 39 | ->defaultFalse() |
|
179 | 39 | ->end() |
|
180 | 39 | ->end() |
|
181 | 39 | ->end() |
|
182 | 39 | ->arrayNode('rules') |
|
183 | 39 | ->prototype('array') |
|
184 | 39 | ->children(); |
|
185 | |||
186 | 39 | $this->addMatch($rules, true); |
|
187 | $rules |
||
188 | 39 | ->arrayNode('headers') |
|
189 | 39 | ->isRequired() |
|
190 | // todo validate there is some header defined |
||
191 | 39 | ->children() |
|
192 | 39 | ->enumNode('overwrite') |
|
193 | 39 | ->info('Whether to overwrite cache headers for this rule, defaults to the cache_control.defaults.overwrite setting') |
|
194 | 39 | ->values(['default', true, false]) |
|
195 | 39 | ->defaultValue('default') |
|
196 | 39 | ->end() |
|
197 | 39 | ->arrayNode('cache_control') |
|
198 | 39 | ->info('Add the specified cache control directives.') |
|
199 | 39 | ->children() |
|
200 | 39 | ->scalarNode('max_age')->end() |
|
201 | 39 | ->scalarNode('s_maxage')->end() |
|
202 | 39 | ->booleanNode('private')->end() |
|
203 | 39 | ->booleanNode('public')->end() |
|
204 | 39 | ->booleanNode('must_revalidate')->end() |
|
205 | 39 | ->booleanNode('proxy_revalidate')->end() |
|
206 | 39 | ->booleanNode('no_transform')->end() |
|
207 | 39 | ->booleanNode('no_cache')->end() |
|
208 | 39 | ->booleanNode('no_store')->end() |
|
209 | 39 | ->scalarNode('stale_if_error')->end() |
|
210 | 39 | ->scalarNode('stale_while_revalidate')->end() |
|
211 | 39 | ->end() |
|
212 | 39 | ->end() |
|
213 | 39 | ->booleanNode('etag') |
|
214 | 39 | ->defaultValue(false) |
|
215 | 39 | ->info('Set a simple ETag which is just the md5 hash of the response body') |
|
216 | 39 | ->end() |
|
217 | 39 | ->scalarNode('last_modified') |
|
218 | 39 | ->validate() |
|
219 | 39 | ->ifTrue(function ($v) { |
|
220 | 2 | if (is_string($v)) { |
|
221 | 2 | new \DateTime($v); |
|
222 | } |
||
223 | |||
224 | 1 | return false; |
|
225 | 39 | }) |
|
226 | 39 | ->thenInvalid('') // this will never happen as new DateTime will throw an exception if $v is no date |
|
227 | 39 | ->end() |
|
228 | 39 | ->info('Set a default last modified timestamp if none is set yet. Value must be parseable by DateTime') |
|
229 | 39 | ->end() |
|
230 | 39 | ->scalarNode('reverse_proxy_ttl') |
|
231 | 39 | ->defaultNull() |
|
232 | 39 | ->info('Specify an X-Reverse-Proxy-TTL header with a time in seconds for a caching proxy under your control.') |
|
233 | 39 | ->end() |
|
234 | 39 | ->arrayNode('vary') |
|
235 | 39 | ->beforeNormalization()->ifString()->then(function ($v) { |
|
236 | 2 | return preg_split('/\s*,\s*/', $v); |
|
237 | 39 | })->end() |
|
238 | 39 | ->prototype('scalar')->end() |
|
239 | 39 | ->info('Define a list of additional headers on which the response varies.') |
|
240 | 39 | ->end() |
|
241 | 39 | ->end() |
|
242 | 39 | ->end() |
|
243 | ; |
||
244 | 39 | } |
|
245 | |||
246 | /** |
||
247 | * Shared configuration between cache control, tags and invalidation. |
||
248 | * |
||
249 | * @param NodeBuilder $rules |
||
250 | * @param bool $matchResponse whether to also add fields to match response |
||
251 | */ |
||
252 | 39 | private function addMatch(NodeBuilder $rules, $matchResponse = false) |
|
253 | { |
||
254 | $match = $rules |
||
255 | 39 | ->arrayNode('match') |
|
256 | 39 | ->cannotBeOverwritten() |
|
257 | 39 | ->isRequired() |
|
258 | 39 | ->fixXmlConfig('method') |
|
259 | 39 | ->fixXmlConfig('ip') |
|
260 | 39 | ->fixXmlConfig('attribute') |
|
261 | 39 | ->validate() |
|
262 | 39 | ->ifTrue(function ($v) { |
|
263 | 11 | return !empty($v['additional_response_status']) && !empty($v['match_response']); |
|
264 | 39 | }) |
|
265 | 39 | ->thenInvalid('You may not set both additional_response_status and match_response.') |
|
266 | 39 | ->end() |
|
267 | 39 | ->children() |
|
268 | 39 | ->scalarNode('path') |
|
269 | 39 | ->defaultNull() |
|
270 | 39 | ->info('Request path.') |
|
271 | 39 | ->end() |
|
272 | 39 | ->scalarNode('host') |
|
273 | 39 | ->defaultNull() |
|
274 | 39 | ->info('Request host name.') |
|
275 | 39 | ->end() |
|
276 | 39 | ->arrayNode('methods') |
|
277 | 39 | ->beforeNormalization()->ifString()->then(function ($v) { |
|
278 | 3 | return preg_split('/\s*,\s*/', $v); |
|
279 | 39 | })->end() |
|
280 | 39 | ->useAttributeAsKey('name') |
|
281 | 39 | ->prototype('scalar')->end() |
|
282 | 39 | ->info('Request HTTP methods.') |
|
283 | 39 | ->end() |
|
284 | 39 | ->arrayNode('ips') |
|
285 | 39 | ->beforeNormalization()->ifString()->then(function ($v) { |
|
286 | 3 | return preg_split('/\s*,\s*/', $v); |
|
287 | 39 | })->end() |
|
288 | 39 | ->useAttributeAsKey('name') |
|
289 | 39 | ->prototype('scalar')->end() |
|
290 | 39 | ->info('List of client IPs.') |
|
291 | 39 | ->end() |
|
292 | 39 | ->arrayNode('attributes') |
|
293 | 39 | ->useAttributeAsKey('name') |
|
294 | 39 | ->prototype('scalar')->end() |
|
295 | 39 | ->info('Regular expressions on request attributes.') |
|
296 | 39 | ->end() |
|
297 | ; |
||
298 | 39 | if ($matchResponse) { |
|
299 | $match |
||
300 | 39 | ->arrayNode('additional_response_status') |
|
301 | 39 | ->prototype('scalar')->end() |
|
302 | 39 | ->info('Additional response HTTP status codes that will match. Replaces cacheable configuration.') |
|
303 | 39 | ->end() |
|
304 | 39 | ->scalarNode('match_response') |
|
305 | 39 | ->defaultNull() |
|
306 | 39 | ->info('Expression to decide whether response should be matched. Replaces cacheable configuration.') |
|
307 | 39 | ->end() |
|
308 | ; |
||
309 | } |
||
310 | 39 | } |
|
311 | |||
312 | 39 | private function addProxyClientSection(ArrayNodeDefinition $rootNode) |
|
313 | { |
||
314 | $rootNode |
||
|
|||
315 | 39 | ->children() |
|
316 | 39 | ->arrayNode('proxy_client') |
|
317 | 39 | ->children() |
|
318 | 39 | ->enumNode('default') |
|
319 | 39 | ->values(['varnish', 'nginx', 'symfony', 'noop']) |
|
320 | 39 | ->info('If you configure more than one proxy client, you need to specify which client is the default.') |
|
321 | 39 | ->end() |
|
322 | 39 | ->arrayNode('varnish') |
|
323 | 39 | ->fixXmlConfig('default_ban_header') |
|
324 | 39 | ->validate() |
|
325 | 39 | ->always(function ($v) { |
|
326 | 15 | if (!count($v['default_ban_headers'])) { |
|
327 | 14 | unset($v['default_ban_headers']); |
|
328 | } |
||
329 | |||
330 | 15 | return $v; |
|
331 | 39 | }) |
|
332 | 39 | ->end() |
|
333 | 39 | ->children() |
|
334 | 39 | ->scalarNode('tags_header') |
|
335 | 39 | ->defaultValue(Varnish::DEFAULT_HTTP_HEADER_CACHE_TAGS) |
|
336 | 39 | ->info('HTTP header to use when sending tag invalidation requests to Varnish') |
|
337 | 39 | ->end() |
|
338 | 39 | ->scalarNode('header_length') |
|
339 | 39 | ->info('Maximum header length when invalidating tags. If there are more tags to invalidate than fit into the header, the invalidation request is split into several requests.') |
|
340 | 39 | ->end() |
|
341 | 39 | ->arrayNode('default_ban_headers') |
|
342 | 39 | ->useAttributeAsKey('name') |
|
343 | 39 | ->info('Map of additional headers to include in each ban request.') |
|
344 | 39 | ->prototype('scalar')->end() |
|
345 | 39 | ->end() |
|
346 | 39 | ->append($this->getHttpDispatcherNode()) |
|
347 | 39 | ->end() |
|
348 | 39 | ->end() |
|
349 | |||
350 | 39 | ->arrayNode('nginx') |
|
351 | 39 | ->children() |
|
352 | 39 | ->scalarNode('purge_location') |
|
353 | 39 | ->defaultValue(false) |
|
354 | 39 | ->info('Path to trigger the purge on Nginx for different location purge.') |
|
355 | 39 | ->end() |
|
356 | 39 | ->append($this->getHttpDispatcherNode()) |
|
357 | 39 | ->end() |
|
358 | 39 | ->end() |
|
359 | |||
360 | 39 | ->arrayNode('symfony') |
|
361 | 39 | ->children() |
|
362 | 39 | ->append($this->getHttpDispatcherNode()) |
|
363 | 39 | ->end() |
|
364 | 39 | ->end() |
|
365 | |||
366 | 39 | ->booleanNode('noop')->end() |
|
367 | |||
368 | 39 | ->end() |
|
369 | 39 | ->end() |
|
370 | 39 | ->end(); |
|
371 | 39 | } |
|
372 | |||
373 | /** |
||
374 | * Get the configuration node for a HTTP dispatcher in a proxy client. |
||
375 | * |
||
376 | * @return NodeDefinition |
||
377 | */ |
||
378 | 39 | private function getHttpDispatcherNode() |
|
407 | |||
408 | 39 | private function addTestSection(ArrayNodeDefinition $rootNode) |
|
447 | |||
448 | /** |
||
449 | * Cache manager main section. |
||
450 | * |
||
451 | * @param ArrayNodeDefinition $rootNode |
||
452 | */ |
||
453 | 39 | private function addCacheManagerSection(ArrayNodeDefinition $rootNode) |
|
454 | { |
||
455 | $rootNode |
||
456 | 39 | ->children() |
|
457 | 39 | ->arrayNode('cache_manager') |
|
458 | 39 | ->addDefaultsIfNotSet() |
|
459 | 39 | ->beforeNormalization() |
|
460 | 39 | ->ifArray() |
|
461 | 39 | ->then(function ($v) { |
|
462 | 6 | $v['enabled'] = isset($v['enabled']) ? $v['enabled'] : true; |
|
463 | |||
464 | 6 | return $v; |
|
465 | 39 | }) |
|
466 | 39 | ->end() |
|
467 | 39 | ->info('Configure the cache manager. Needs a proxy_client to be configured.') |
|
468 | 39 | ->children() |
|
469 | 39 | ->enumNode('enabled') |
|
470 | 39 | ->values([true, false, 'auto']) |
|
471 | 39 | ->defaultValue('auto') |
|
472 | 39 | ->info('Allows to disable the invalidation manager. Enabled by default if you configure a proxy client.') |
|
473 | 39 | ->end() |
|
474 | 39 | ->scalarNode('custom_proxy_client') |
|
475 | 39 | ->info('Service name of a custom proxy client to use. With a custom client, generate_url_type defaults to ABSOLUTE_URL and tag support needs to be explicitly enabled. If no custom proxy client is specified, the first proxy client you configured is used.') |
|
476 | 39 | ->cannotBeEmpty() |
|
477 | 39 | ->end() |
|
478 | 39 | ->enumNode('generate_url_type') |
|
479 | 39 | ->values([ |
|
480 | 39 | 'auto', |
|
481 | UrlGeneratorInterface::ABSOLUTE_PATH, |
||
482 | UrlGeneratorInterface::ABSOLUTE_URL, |
||
483 | UrlGeneratorInterface::NETWORK_PATH, |
||
484 | UrlGeneratorInterface::RELATIVE_PATH, |
||
485 | ]) |
||
486 | 39 | ->defaultValue('auto') |
|
487 | 39 | ->info('Set what URLs to generate on invalidate/refresh Route. Auto means path if base_url is set on the default proxy client, full URL otherwise.') |
|
488 | 39 | ->end() |
|
489 | 39 | ->end() |
|
490 | ; |
||
491 | 39 | } |
|
492 | |||
493 | 39 | private function addTagSection(ArrayNodeDefinition $rootNode) |
|
540 | |||
541 | 39 | private function addInvalidationSection(ArrayNodeDefinition $rootNode) |
|
578 | |||
579 | /** |
||
580 | * User context main section. |
||
581 | * |
||
582 | * @param ArrayNodeDefinition $rootNode |
||
583 | */ |
||
584 | 39 | private function addUserContextListenerSection(ArrayNodeDefinition $rootNode) |
|
648 | |||
649 | 39 | private function addFlashMessageSection(ArrayNodeDefinition $rootNode) |
|
678 | |||
679 | 39 | private function addDebugSection(ArrayNodeDefinition $rootNode) |
|
699 | } |
||
700 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: