diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 863ce00049c21..4aad022fd6417 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -85,6 +85,7 @@ public function getConfigTreeBuilder() $this->addProfilerSection($rootNode); $this->addRouterSection($rootNode); $this->addSessionSection($rootNode); + $this->addRequestSection($rootNode); $this->addTemplatingSection($rootNode); $this->addTranslatorSection($rootNode); $this->addValidationSection($rootNode); @@ -256,6 +257,31 @@ private function addSessionSection(ArrayNodeDefinition $rootNode) ; } + private function addRequestSection(ArrayNodeDefinition $rootNode) + { + $rootNode + ->children() + ->arrayNode('request') + ->info('request configuration') + ->canBeUnset() + ->fixXmlConfig('additional_format') + ->children() + ->arrayNode('additional_formats') + ->useAttributeAsKey('name') + ->prototype('array') + ->beforeNormalization() + ->ifTrue(function($v) { return !is_array($v); }) + ->then(function($v) { return array($v); }) + ->end() + ->prototype('scalar')->end() + ->end() + ->end() + ->end() + ->end() + ->end() + ; + } + private function addTemplatingSection(ArrayNodeDefinition $rootNode) { $organizeUrls = function($urls) { diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index daf988160a6e7..a7a168cf99561 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -91,6 +91,10 @@ public function load(array $configs, ContainerBuilder $container) $this->registerSessionConfiguration($config['session'], $container, $loader); } + if (isset($config['request'])) { + $this->registerRequestConfiguration($config['request'], $container, $loader); + } + $loader->load('security.xml'); if ($this->isConfigEnabled($container, $config['form'])) { @@ -382,6 +386,21 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c $container->setParameter('session.metadata.update_threshold', $config['metadata_update_threshold']); } + /** + * Loads the request configuration. + * + * @param array $config A session configuration array + * @param ContainerBuilder $container A ContainerBuilder instance + * @param XmlFileLoader $loader An XmlFileLoader instance + */ + private function registerRequestConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) + { + if ($config['additional_formats']) { + $container->setParameter('request.additional_formats', $config['additional_formats']); + $loader->load('request.xml'); + } + } + /** * Loads the templating configuration. * diff --git a/src/Symfony/Bundle/FrameworkBundle/EventListener/AddRequestFormatsListener.php b/src/Symfony/Bundle/FrameworkBundle/EventListener/AddRequestFormatsListener.php new file mode 100644 index 0000000000000..6ba098e0183df --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/EventListener/AddRequestFormatsListener.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\EventListener; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; + +/** + * Set additional formats into the request + * + * @author Gildas Quemener + */ +class AddRequestFormatsListener implements EventSubscriberInterface +{ + /** + * @var array + */ + protected $formats; + + /** + * @param array $formats + */ + public function __construct(array $formats) + { + $this->formats = $formats; + } + + /** + * Set additionnal request formats + * + * @param GetResponseEvent $event + */ + public function onKernelRequest(GetResponseEvent $event) + { + foreach ($this->formats as $format => $mimeTypes) { + $event->getRequest()->setFormat($format, $mimeTypes); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() + { + return array( + KernelEvents::REQUEST => 'onKernelRequest', + ); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/request.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/request.xml new file mode 100644 index 0000000000000..0170c95c43120 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/request.xml @@ -0,0 +1,17 @@ + + + + + + Symfony\Bundle\FrameworkBundle\EventListener\AddRequestFormatsListener + + + + + + %request.additional_formats% + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 92c8952e48be9..4adad1c2438a8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -15,6 +15,7 @@ + @@ -101,6 +102,19 @@ + + + + + + + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php index c4eff9349228e..f65c5536774cb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php @@ -70,5 +70,16 @@ 'debug' => true, 'file_cache_dir' => '%kernel.cache_dir%/annotations', ), - 'ide' => 'file%%link%%format' + 'ide' => 'file%%link%%format', + 'request' => array( + 'additional_formats' => array( + 'csv' => array( + 'text/csv', + 'text/plain', + ), + 'pdf' => array( + 'application/pdf' + ) + ) + ) )); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/request.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/request.php new file mode 100644 index 0000000000000..06624e29b0f16 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/request.php @@ -0,0 +1,7 @@ +loadFromExtension('framework', array( + 'request' => array( + 'additional_formats' => array(), + ), +)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml index 8fd3e9b6528a0..57f6092c250e5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml @@ -36,5 +36,11 @@ + + + text/csv + text/plain + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/request.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/request.xml new file mode 100644 index 0000000000000..49849c2a381d1 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/request.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml index d013063916c38..eca9a788b944d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml @@ -55,3 +55,7 @@ framework: debug: true file_cache_dir: %kernel.cache_dir%/annotations ide: file%%link%%format + request: + additional_formats: + csv: ['text/csv', 'text/plain'] + pdf: 'application/pdf' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/request.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/request.yml new file mode 100644 index 0000000000000..50a8e227cefd4 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/request.yml @@ -0,0 +1,3 @@ +framework: + request: + additional_formats: ~ diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 09b14ecf67a46..9abc2c98adcce 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -145,6 +145,21 @@ public function testNullSessionHandler() $this->assertNull($container->getDefinition('session.storage.php_bridge')->getArgument(0)); } + public function testRequest() + { + $container = $this->createContainerFromFile('full'); + + $this->assertTrue($container->hasDefinition('request.add_request_formats_listener'), '->registerRequestConfiguration() loads request.xml'); + $this->assertEquals(array('csv' => array('text/csv', 'text/plain'), 'pdf' => array('application/pdf')), $container->getParameter('request.additional_formats')); + } + + public function testEmptyAdditionlRequestFormats() + { + $container = $this->createContainerFromFile('request'); + + $this->assertFalse($container->hasDefinition('request.add_request_formats_listener'), '->registerRequestConfiguration() does not load request.xml when no additional request formats are configured'); + } + public function testTemplating() { $container = $this->createContainerFromFile('full'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/AddRequestFormatsListenerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/AddRequestFormatsListenerTest.php new file mode 100644 index 0000000000000..b8482ddca4544 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/AddRequestFormatsListenerTest.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\EventListener; + +use Symfony\Bundle\FrameworkBundle\EventListener\AddRequestFormatsListener; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\KernelEvents; + +/** + * Test AddRequestFormatsListener class + * + * @author Gildas Quemener + */ +class AddRequestFormatsListenerTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var AddRequestFormatsListener + */ + private $listener; + + protected function setUp() + { + $this->listener = new AddRequestFormatsListener(array('csv' => array('text/csv', 'text/plain'))); + } + + protected function tearDown() + { + $this->listener = null; + } + + public function testIsAnEventSubscriber() + { + $this->assertInstanceOf('Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener); + } + + public function testRegisteredEvent() + { + $this->assertEquals(array( + KernelEvents::REQUEST => 'onKernelRequest', + ), AddRequestFormatsListener::getSubscribedEvents()); + } + + public function testSetAdditionnalFormats() + { + $request = $this->getRequestMock(); + $event = $this->getGetResponseEventMock($request); + + $request->expects($this->once()) + ->method('setFormat') + ->with('csv', array('text/csv', 'text/plain')); + + $this->listener->onKernelRequest($event); + } + + protected function getRequestMock() + { + return $this->getMock('Symfony\Component\HttpFoundation\Request'); + } + + protected function getGetResponseEventMock(Request $request) + { + $event = $this + ->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent') + ->disableOriginalConstructor() + ->getMock(); + + $event->expects($this->any()) + ->method('getRequest') + ->will($this->returnValue($request)); + + return $event; + } +}