|
14 | 14 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
15 | 15 | use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
16 | 16 | use Symfony\Component\Config\Definition\ConfigurationInterface;
|
| 17 | +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
17 | 18 |
|
18 | 19 | /**
|
19 | 20 | * TwigExtension configuration structure.
|
@@ -42,6 +43,7 @@ public function getConfigTreeBuilder()
|
42 | 43 | $this->addGlobalsSection($rootNode);
|
43 | 44 | $this->addTwigOptions($rootNode);
|
44 | 45 | $this->addTwigFormatOptions($rootNode);
|
| 46 | + $this->addHttpExceptionLogLevels($rootNode); |
45 | 47 |
|
46 | 48 | return $treeBuilder;
|
47 | 49 | }
|
@@ -194,4 +196,43 @@ private function addTwigFormatOptions(ArrayNodeDefinition $rootNode)
|
194 | 196 | ->end()
|
195 | 197 | ;
|
196 | 198 | }
|
| 199 | + |
| 200 | + private function addHttpExceptionLogLevels(ArrayNodeDefinition $rootNode) |
| 201 | + { |
| 202 | + $rootNode |
| 203 | + ->children() |
| 204 | + ->arrayNode('http_exception_log_levels') |
| 205 | + ->info('The override log levels for http exceptions') |
| 206 | + ->example(array('403' => 'NOTICE', '404' => 'INFO')) |
| 207 | + ->useAttributeAsKey('http_exception_log_levels') |
| 208 | + ->prototype('variable')->end() |
| 209 | + ->validate() |
| 210 | + ->always(function ($v) { |
| 211 | + $map = array(); |
| 212 | + foreach ($v as $status => $level) { |
| 213 | + if (!(is_int($status) && 100 <= $status && $status <= 599)) { |
| 214 | + throw new InvalidConfigurationException(sprintf( |
| 215 | + 'The configured status code "%s" in twig.http_exception_log_levels is not a valid http status code.', |
| 216 | + $status |
| 217 | + )); |
| 218 | + } |
| 219 | + |
| 220 | + $levelConstant = 'Psr\Log\LogLevel::'.$level; |
| 221 | + if (!defined($levelConstant)) { |
| 222 | + throw new InvalidConfigurationException(sprintf( |
| 223 | + 'The configured log level "%s" in twig.http_exception_log_levels is invalid as it is not defined in Psr\\Log\\LogLevel.', |
| 224 | + $level |
| 225 | + )); |
| 226 | + } |
| 227 | + |
| 228 | + $map[$status] = constant($levelConstant); |
| 229 | + } |
| 230 | + |
| 231 | + return $map; |
| 232 | + }) |
| 233 | + ->end() |
| 234 | + ->end() |
| 235 | + ->end() |
| 236 | + ; |
| 237 | + } |
197 | 238 | }
|
0 commit comments