1 | <?php |
||
31 | class Configuration implements ConfigurationInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | private $debug; |
||
37 | |||
38 | /** |
||
39 | * @param bool $debug Whether to use the debug mode |
||
40 | */ |
||
41 | 36 | public function __construct($debug) |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 36 | public function getConfigTreeBuilder() |
|
50 | { |
||
51 | 36 | $treeBuilder = new TreeBuilder(); |
|
52 | 36 | $rootNode = $treeBuilder->root('fos_http_cache'); |
|
53 | |||
54 | $rootNode |
||
55 | 36 | ->validate() |
|
56 | ->ifTrue(function ($v) { |
||
57 | 34 | return $v['cache_manager']['enabled'] |
|
58 | 34 | && !isset($v['proxy_client']) |
|
59 | 34 | && !isset($v['cache_manager']['custom_proxy_client']) |
|
60 | ; |
||
61 | 36 | }) |
|
62 | ->then(function ($v) { |
||
63 | 8 | if ('auto' === $v['cache_manager']['enabled']) { |
|
64 | 7 | $v['cache_manager']['enabled'] = false; |
|
65 | |||
66 | 7 | return $v; |
|
67 | } |
||
68 | 1 | throw new InvalidConfigurationException('You need to configure a proxy_client or specify a custom_proxy_client to use the cache_manager.'); |
|
69 | 36 | }) |
|
70 | 36 | ->end() |
|
71 | 36 | ->validate() |
|
72 | ->ifTrue(function ($v) { |
||
73 | 33 | return $v['tags']['enabled'] && !$v['cache_manager']['enabled']; |
|
74 | 36 | }) |
|
75 | ->then(function ($v) { |
||
76 | 9 | if ('auto' === $v['tags']['enabled']) { |
|
77 | 8 | $v['tags']['enabled'] = false; |
|
78 | |||
79 | 8 | return $v; |
|
80 | } |
||
81 | 1 | throw new InvalidConfigurationException('You need to configure a proxy_client to get the cache_manager needed for tag handling.'); |
|
82 | 36 | }) |
|
83 | 36 | ->end() |
|
84 | 36 | ->validate() |
|
85 | ->ifTrue(function ($v) { |
||
86 | 32 | return $v['invalidation']['enabled'] && !$v['cache_manager']['enabled']; |
|
87 | 36 | }) |
|
88 | ->then(function ($v) { |
||
89 | 8 | if ('auto' === $v['invalidation']['enabled']) { |
|
90 | 7 | $v['invalidation']['enabled'] = false; |
|
91 | |||
92 | 7 | return $v; |
|
93 | } |
||
94 | 1 | throw new InvalidConfigurationException('You need to configure a proxy_client to get the cache_manager needed for invalidation handling.'); |
|
95 | 36 | }) |
|
96 | 36 | ->end() |
|
97 | 36 | ->validate() |
|
98 | 36 | ->ifTrue( |
|
99 | function ($v) { |
||
100 | 31 | return $v['user_context']['logout_handler']['enabled'] |
|
101 | 31 | && !isset($v['proxy_client']); |
|
102 | 36 | } |
|
103 | ) |
||
104 | ->then(function ($v) { |
||
105 | 9 | if ('auto' === $v['user_context']['logout_handler']['enabled']) { |
|
106 | 9 | $v['user_context']['logout_handler']['enabled'] = false; |
|
107 | |||
108 | 9 | return $v; |
|
109 | } |
||
110 | throw new InvalidConfigurationException('You need to configure a proxy_client for the logout_handler.'); |
||
111 | 36 | }) |
|
112 | ; |
||
113 | |||
114 | 36 | $this->addCacheControlSection($rootNode); |
|
115 | 36 | $this->addProxyClientSection($rootNode); |
|
116 | 36 | $this->addCacheManagerSection($rootNode); |
|
117 | 36 | $this->addTagSection($rootNode); |
|
118 | 36 | $this->addInvalidationSection($rootNode); |
|
119 | 36 | $this->addUserContextListenerSection($rootNode); |
|
120 | 36 | $this->addFlashMessageSection($rootNode); |
|
121 | 36 | $this->addTestSection($rootNode); |
|
122 | 36 | $this->addDebugSection($rootNode); |
|
123 | |||
124 | 36 | return $treeBuilder; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Cache header control main section. |
||
129 | * |
||
130 | * @param ArrayNodeDefinition $rootNode |
||
131 | */ |
||
132 | 36 | private function addCacheControlSection(ArrayNodeDefinition $rootNode) |
|
133 | { |
||
134 | $rules = $rootNode |
||
135 | 36 | ->children() |
|
136 | 36 | ->arrayNode('cache_control') |
|
137 | 36 | ->fixXmlConfig('rule') |
|
138 | 36 | ->children() |
|
139 | 36 | ->arrayNode('defaults') |
|
140 | 36 | ->addDefaultsIfNotSet() |
|
141 | 36 | ->children() |
|
142 | 36 | ->booleanNode('overwrite') |
|
143 | 36 | ->info('Whether to overwrite existing cache headers') |
|
144 | 36 | ->defaultFalse() |
|
145 | 36 | ->end() |
|
146 | 36 | ->end() |
|
147 | 36 | ->end() |
|
148 | 36 | ->arrayNode('rules') |
|
149 | 36 | ->prototype('array') |
|
150 | 36 | ->children(); |
|
151 | |||
152 | 36 | $this->addMatch($rules); |
|
153 | $rules |
||
154 | 36 | ->arrayNode('headers') |
|
155 | 36 | ->isRequired() |
|
156 | // todo validate there is some header defined |
||
157 | 36 | ->children() |
|
158 | 36 | ->enumNode('overwrite') |
|
159 | 36 | ->info('Whether to overwrite cache headers for this rule, defaults to the cache_control.defaults.overwrite setting') |
|
160 | 36 | ->values(['default', true, false]) |
|
161 | 36 | ->defaultValue('default') |
|
162 | 36 | ->end() |
|
163 | 36 | ->arrayNode('cache_control') |
|
164 | 36 | ->info('Add the specified cache control directives.') |
|
165 | 36 | ->children() |
|
166 | 36 | ->scalarNode('max_age')->end() |
|
167 | 36 | ->scalarNode('s_maxage')->end() |
|
168 | 36 | ->booleanNode('private')->end() |
|
169 | 36 | ->booleanNode('public')->end() |
|
170 | 36 | ->booleanNode('must_revalidate')->end() |
|
171 | 36 | ->booleanNode('proxy_revalidate')->end() |
|
172 | 36 | ->booleanNode('no_transform')->end() |
|
173 | 36 | ->booleanNode('no_cache')->end() |
|
174 | 36 | ->scalarNode('stale_if_error')->end() |
|
175 | 36 | ->scalarNode('stale_while_revalidate')->end() |
|
176 | 36 | ->end() |
|
177 | 36 | ->end() |
|
178 | 36 | ->booleanNode('etag') |
|
179 | 36 | ->defaultValue(false) |
|
180 | 36 | ->info('Set a simple ETag which is just the md5 hash of the response body') |
|
181 | 36 | ->end() |
|
182 | 36 | ->scalarNode('last_modified') |
|
183 | 36 | ->validate() |
|
184 | ->ifTrue(function ($v) { |
||
185 | 2 | if (is_string($v)) { |
|
186 | 2 | new \DateTime($v); |
|
187 | } |
||
188 | |||
189 | 1 | return false; |
|
190 | 36 | }) |
|
191 | 36 | ->thenInvalid('') // this will never happen as new DateTime will throw an exception if $v is no date |
|
192 | 36 | ->end() |
|
193 | 36 | ->info('Set a default last modified timestamp if none is set yet. Value must be parseable by DateTime') |
|
194 | 36 | ->end() |
|
195 | 36 | ->scalarNode('reverse_proxy_ttl') |
|
196 | 36 | ->defaultNull() |
|
197 | 36 | ->info('Specify an X-Reverse-Proxy-TTL header with a time in seconds for a caching proxy under your control.') |
|
198 | 36 | ->end() |
|
199 | 36 | ->arrayNode('vary') |
|
200 | ->beforeNormalization()->ifString()->then(function ($v) { |
||
201 | 2 | return preg_split('/\s*,\s*/', $v); |
|
202 | 36 | })->end() |
|
203 | 36 | ->prototype('scalar')->end() |
|
204 | 36 | ->info('Define a list of additional headers on which the response varies.') |
|
205 | 36 | ->end() |
|
206 | 36 | ->end() |
|
207 | 36 | ->end() |
|
208 | ; |
||
209 | 36 | } |
|
210 | |||
211 | /** |
||
212 | * Shared configuration between cache control, tags and invalidation. |
||
213 | * |
||
214 | * @param NodeBuilder $rules |
||
215 | */ |
||
216 | 36 | private function addMatch(NodeBuilder $rules) |
|
217 | { |
||
218 | $rules |
||
219 | 36 | ->arrayNode('match') |
|
220 | 36 | ->cannotBeOverwritten() |
|
221 | 36 | ->isRequired() |
|
222 | 36 | ->fixXmlConfig('method') |
|
223 | 36 | ->fixXmlConfig('ip') |
|
224 | 36 | ->fixXmlConfig('attribute') |
|
225 | 36 | ->validate() |
|
226 | ->ifTrue(function ($v) { |
||
227 | 9 | return !empty($v['additional_cacheable_status']) && !empty($v['match_response']); |
|
228 | 36 | }) |
|
229 | 36 | ->thenInvalid('You may not set both additional_cacheable_status and match_response.') |
|
230 | 36 | ->end() |
|
231 | 36 | ->validate() |
|
232 | ->ifTrue(function ($v) { |
||
233 | 8 | return !empty($v['match_response']) && !class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage'); |
|
234 | 36 | }) |
|
235 | 36 | ->thenInvalid('Configured a match_response but ExpressionLanguage is not available') |
|
236 | 36 | ->end() |
|
237 | 36 | ->children() |
|
238 | 36 | ->scalarNode('path') |
|
239 | 36 | ->defaultNull() |
|
240 | 36 | ->info('Request path.') |
|
241 | 36 | ->end() |
|
242 | 36 | ->scalarNode('host') |
|
243 | 36 | ->defaultNull() |
|
244 | 36 | ->info('Request host name.') |
|
245 | 36 | ->end() |
|
246 | 36 | ->arrayNode('methods') |
|
247 | ->beforeNormalization()->ifString()->then(function ($v) { |
||
248 | 3 | return preg_split('/\s*,\s*/', $v); |
|
249 | 36 | })->end() |
|
250 | 36 | ->useAttributeAsKey('name') |
|
251 | 36 | ->prototype('scalar')->end() |
|
252 | 36 | ->info('Request HTTP methods.') |
|
253 | 36 | ->end() |
|
254 | 36 | ->arrayNode('ips') |
|
255 | ->beforeNormalization()->ifString()->then(function ($v) { |
||
256 | 3 | return preg_split('/\s*,\s*/', $v); |
|
257 | 36 | })->end() |
|
258 | 36 | ->useAttributeAsKey('name') |
|
259 | 36 | ->prototype('scalar')->end() |
|
260 | 36 | ->info('List of client IPs.') |
|
261 | 36 | ->end() |
|
262 | 36 | ->arrayNode('attributes') |
|
263 | 36 | ->useAttributeAsKey('name') |
|
264 | 36 | ->prototype('scalar')->end() |
|
265 | 36 | ->info('Regular expressions on request attributes.') |
|
266 | 36 | ->end() |
|
267 | 36 | ->arrayNode('additional_cacheable_status') |
|
268 | 36 | ->prototype('scalar')->end() |
|
269 | 36 | ->info('Additional response HTTP status codes that will match.') |
|
270 | 36 | ->end() |
|
271 | 36 | ->scalarNode('match_response') |
|
272 | 36 | ->defaultNull() |
|
273 | 36 | ->info('Expression to decide whether response should be matched. Replaces HTTP code check and additional_cacheable_status.') |
|
274 | 36 | ->end() |
|
275 | 36 | ->end() |
|
276 | 36 | ->end() |
|
277 | ; |
||
278 | 36 | } |
|
279 | |||
280 | 36 | private function addProxyClientSection(ArrayNodeDefinition $rootNode) |
|
281 | { |
||
282 | $rootNode |
||
283 | 36 | ->children() |
|
284 | 36 | ->arrayNode('proxy_client') |
|
285 | 36 | ->children() |
|
286 | 36 | ->enumNode('default') |
|
287 | 36 | ->values(['varnish', 'nginx', 'symfony', 'noop']) |
|
288 | 36 | ->info('If you configure more than one proxy client, you need to specify which client is the default.') |
|
289 | 36 | ->end() |
|
290 | 36 | ->arrayNode('varnish') |
|
291 | 36 | ->children() |
|
292 | 36 | ->append($this->getHttpDispatcherNode()) |
|
293 | 36 | ->end() |
|
294 | 36 | ->end() |
|
295 | |||
296 | 36 | ->arrayNode('nginx') |
|
297 | 36 | ->children() |
|
298 | 36 | ->scalarNode('purge_location') |
|
299 | 36 | ->defaultValue(false) |
|
300 | 36 | ->info('Path to trigger the purge on Nginx for different location purge.') |
|
301 | 36 | ->end() |
|
302 | 36 | ->append($this->getHttpDispatcherNode()) |
|
303 | 36 | ->end() |
|
304 | 36 | ->end() |
|
305 | |||
306 | 36 | ->arrayNode('symfony') |
|
307 | 36 | ->children() |
|
308 | 36 | ->append($this->getHttpDispatcherNode()) |
|
309 | 36 | ->end() |
|
310 | 36 | ->end() |
|
311 | |||
312 | 36 | ->booleanNode('noop')->end() |
|
313 | |||
314 | 36 | ->end() |
|
315 | 36 | ->end() |
|
316 | 36 | ->end(); |
|
317 | 36 | } |
|
318 | |||
319 | /** |
||
320 | * Get the configuration node for a HTTP dispatcher in a proxy client. |
||
321 | * |
||
322 | * @return NodeDefinition |
||
323 | */ |
||
324 | 36 | private function getHttpDispatcherNode() |
|
353 | |||
354 | 36 | private function addTestSection(ArrayNodeDefinition $rootNode) |
|
393 | |||
394 | /** |
||
395 | * Cache manager main section. |
||
396 | * |
||
397 | * @param ArrayNodeDefinition $rootNode |
||
398 | */ |
||
399 | 36 | private function addCacheManagerSection(ArrayNodeDefinition $rootNode) |
|
438 | |||
439 | 36 | private function addTagSection(ArrayNodeDefinition $rootNode) |
|
440 | { |
||
441 | $rules = $rootNode |
||
442 | 36 | ->children() |
|
443 | 36 | ->arrayNode('tags') |
|
444 | 36 | ->addDefaultsIfNotSet() |
|
445 | 36 | ->fixXmlConfig('rule') |
|
446 | 36 | ->children() |
|
447 | 36 | ->enumNode('enabled') |
|
448 | 36 | ->values([true, false, 'auto']) |
|
449 | 36 | ->defaultValue('auto') |
|
450 | 36 | ->info('Allows to disable the event subscriber for tag configuration and annotations when your project does not use the annotations. Enabled by default if you configured the cache manager.') |
|
451 | 36 | ->end() |
|
452 | 36 | ->booleanNode('strict')->defaultFalse()->end() |
|
453 | 36 | ->scalarNode('expression_language') |
|
454 | 36 | ->defaultNull() |
|
455 | 36 | ->info('Service name of a custom ExpressionLanugage to use.') |
|
456 | 36 | ->end() |
|
457 | 36 | ->scalarNode('header') |
|
458 | 36 | ->defaultValue('X-Cache-Tags') |
|
459 | 36 | ->info('HTTP header that contains cache tags') |
|
460 | 36 | ->end() |
|
461 | 36 | ->arrayNode('rules') |
|
462 | 36 | ->prototype('array') |
|
463 | 36 | ->fixXmlConfig('tag') |
|
464 | 36 | ->fixXmlConfig('tag_expression') |
|
465 | 36 | ->validate() |
|
466 | 36 | ->ifTrue(function ($v) { |
|
467 | 3 | return !empty($v['tag_expressions']) && !class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage'); |
|
468 | 36 | }) |
|
469 | 36 | ->thenInvalid('Configured a tag_expression but ExpressionLanugage is not available') |
|
470 | 36 | ->end() |
|
471 | 36 | ->children(); |
|
472 | |||
473 | 36 | $this->addMatch($rules); |
|
474 | |||
475 | $rules |
||
476 | 36 | ->arrayNode('tags') |
|
477 | 36 | ->prototype('scalar') |
|
478 | 36 | ->info('Tags to add to the response on safe requests, to invalidate on unsafe requests') |
|
479 | 36 | ->end()->end() |
|
480 | 36 | ->arrayNode('tag_expressions') |
|
481 | 36 | ->prototype('scalar') |
|
482 | 36 | ->info('Tags to add to the response on safe requests, to invalidate on unsafe requests') |
|
483 | 36 | ->end() |
|
484 | ; |
||
485 | 36 | } |
|
486 | |||
487 | 36 | private function addInvalidationSection(ArrayNodeDefinition $rootNode) |
|
524 | |||
525 | /** |
||
526 | * User context main section. |
||
527 | * |
||
528 | * @param ArrayNodeDefinition $rootNode |
||
529 | */ |
||
530 | 36 | private function addUserContextListenerSection(ArrayNodeDefinition $rootNode) |
|
594 | |||
595 | 36 | private function addFlashMessageSection(ArrayNodeDefinition $rootNode) |
|
624 | |||
625 | 36 | private function addDebugSection(ArrayNodeDefinition $rootNode) |
|
645 | } |
||
646 |