Skip to content

Commit 9fa01ce

Browse files
committed
make it possible to configure the ExceptionController without the TwigBundle
1 parent 6ebfd94 commit 9fa01ce

File tree

7 files changed

+83
-3
lines changed

7 files changed

+83
-3
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
17+
/**
18+
* Remove the 'twig.exception_listener' service if 'fos_rest.exception_listener' is activated
19+
*/
20+
class TwigExceptionPass implements CompilerPassInterface
21+
{
22+
public function process(ContainerBuilder $container)
23+
{
24+
if ($container->has('fos_rest.exception_listener') && $container->has('twig.exception_listener')) {
25+
$container->removeDefinition('twig.exception_listener');
26+
}
27+
}
28+
}

DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ private function addExceptionSection(ArrayNodeDefinition $rootNode)
229229
->fixXmlConfig('code', 'codes')
230230
->fixXmlConfig('message', 'messages')
231231
->addDefaultsIfNotSet()
232+
->canBeEnabled()
232233
->children()
234+
->scalarNode('exception_controller')->defaultNull()->end()
233235
->arrayNode('codes')
234236
->useAttributeAsKey('name')
235237
->prototype('scalar')->end()

DependencyInjection/FOSRestExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ public function load(array $configs, ContainerBuilder $container)
132132
$container->setParameter($this->getAlias().'.routing.loader.default_format', $config['routing_loader']['default_format']);
133133
$container->setParameter($this->getAlias().'.routing.loader.include_format', $config['routing_loader']['include_format']);
134134

135+
if ($config['exception']['enabled']) {
136+
$loader->load('exception_listener.xml');
137+
if ($config['exception']['exception_controller']) {
138+
$container->setParameter('fos_rest.exception_listener.controller', $config['exception']['exception_controller']);
139+
}
140+
}
141+
135142
foreach ($config['exception']['codes'] as $exception => $code) {
136143
if (!is_numeric($code)) {
137144
$config['exception']['codes'][$exception] = constant("\FOS\RestBundle\Util\Codes::$code");

FOSRestBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpKernel\Bundle\Bundle;
1616
use FOS\RestBundle\DependencyInjection\Compiler\ConfigurationCheckPass;
1717
use FOS\RestBundle\DependencyInjection\Compiler\FormatListenerRulesPass;
18+
use FOS\RestBundle\DependencyInjection\Compiler\TwigExceptionPass;
1819

1920
/**
2021
* @author Lukas Kahwe Smith <[email protected]>
@@ -29,5 +30,6 @@ public function build(ContainerBuilder $container)
2930
{
3031
$container->addCompilerPass(new ConfigurationCheckPass());
3132
$container->addCompilerPass(new FormatListenerRulesPass());
33+
$container->addCompilerPass(new TwigExceptionPass());
3234
}
3335
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="fos_rest.exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
9+
<parameter key="fos_rest.controller.exception.class">FOS\RestBundle\Controller\ExceptionController</parameter>
10+
<parameter key="fos_rest.exception_listener.controller">fos_rest.controller.exception:showAction</parameter>
11+
12+
</parameters>
13+
14+
<services>
15+
<service id="fos_rest.exception_listener" class="%fos_rest.exception_listener.class%">
16+
<tag name="kernel.event_subscriber" />
17+
<tag name="monolog.logger" channel="request" />
18+
<argument>%fos_rest.exception_listener.controller%</argument>
19+
<argument type="service" id="logger" on-invalid="null" />
20+
</service>
21+
22+
<service id="fos_rest.controller.exception" class="%fos_rest.controller.exception.class%">
23+
<call method="setContainer">
24+
<argument type="service" id="service_container" />
25+
</call>
26+
</service>
27+
</services>
28+
</container>

Resources/doc/4-exception-controller-support.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,28 @@ provides an extra controller for that job. Using this custom ExceptionController
77
it is possible to leverage the View layer when building responses for uncaught
88
Exceptions.
99

10-
To enable the RestBundle view-layer-aware ExceptionController update the twig
11-
section of your config as follows:
10+
The ExceptionController can be enabled either via the FOSRestBundle configuration
11+
and optionally an explicit controller action can be configured as well:
12+
13+
```yaml
14+
# app/config/config.yml
15+
fos_rest:
16+
exception:
17+
enabled: true
18+
exception_controller: 'Acme\DemoBundle\Controller\ExceptionController::showAction'
19+
```
20+
21+
Alternatively the TwigBundle configuration can be used to enable the ExceptionController:
1222
1323
```yaml
1424
# app/config/config.yml
1525
twig:
1626
exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'
1727
```
1828
29+
When enabling the RestBundle view-layer-aware ExceptionController it automatically
30+
disables the TwigBundle exception listener and subsequent configuration.
31+
1932
To map Exception classes to HTTP response status codes an “exception map” may
2033
be configured, where the keys match a fully qualified class name and the values
2134
are either an integer HTTP response status code or a string matching a class

Tests/FOSRestBundleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FOSRestBundleTest extends \PHPUnit_Framework_TestCase
2323
public function testBuild()
2424
{
2525
$container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerBuilder');
26-
$container->expects($this->exactly(2))
26+
$container->expects($this->exactly(3))
2727
->method('addCompilerPass')
2828
->with($this->isInstanceOf('\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));
2929

0 commit comments

Comments
 (0)