Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db70c1a

Browse files
committed
Add to twig bundle configuration
1 parent cc5dffb commit db70c1a

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1515
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1616
use Symfony\Component\Config\Definition\ConfigurationInterface;
17+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1718

1819
/**
1920
* TwigExtension configuration structure.
@@ -42,6 +43,7 @@ public function getConfigTreeBuilder()
4243
$this->addGlobalsSection($rootNode);
4344
$this->addTwigOptions($rootNode);
4445
$this->addTwigFormatOptions($rootNode);
46+
$this->addHttpExceptionLogLevels($rootNode);
4547

4648
return $treeBuilder;
4749
}
@@ -194,4 +196,43 @@ private function addTwigFormatOptions(ArrayNodeDefinition $rootNode)
194196
->end()
195197
;
196198
}
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+
}
197238
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function load(array $configs, ContainerBuilder $container)
7878
$config = $this->processConfiguration($configuration, $configs);
7979

8080
$container->setParameter('twig.exception_listener.controller', $config['exception_controller']);
81+
$container->setParameter('twig.exception_listener.http_log_levels', $config['http_exception_log_levels']);
8182

8283
$container->setParameter('twig.form.resources', $config['form_themes']);
8384
$container->setParameter('twig.default_path', $config['default_path']);

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
<tag name="monolog.logger" channel="request" />
129129
<argument>%twig.exception_listener.controller%</argument>
130130
<argument type="service" id="logger" on-invalid="null" />
131-
<argument>%framework.http_exception.log_levels%</argument>
131+
<argument>%twig.exception_listener.http_log_levels%</argument>
132132
</service>
133133

134134
<service id="twig.controller.exception" class="Symfony\Bundle\TwigBundle\Controller\ExceptionController" public="true">

0 commit comments

Comments
 (0)