Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aca3271

Browse filesBrowse files
committed
feature #9862 [FrameworkBundle] Added configuration for additionnal request formats (gquemener)
This PR was squashed before being merged into the 2.5-dev branch (closes #9862). Discussion ---------- [FrameworkBundle] Added configuration for additionnal request formats | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8934 | License | MIT | Doc PR | symfony/symfony-docs#3402 Reopening of #8944 # TODO - [x] Fix wrong xml configuration definition (Thanks @wouterj) - [x] Change configuration key `additional_formats` to a more meaningful one - [x] Write documentation (new entry or replace http://symfony.com/doc/current/cookbook/request/mime_type.html ?) Commits ------- f90ba11 [FrameworkBundle] Added configuration for additionnal request formats
2 parents 6e9358a + f90ba11 commit aca3271
Copy full SHA for aca3271

File tree

Expand file treeCollapse file tree

13 files changed

+283
-1
lines changed
Filter options
Expand file treeCollapse file tree

13 files changed

+283
-1
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function getConfigTreeBuilder()
8585
$this->addProfilerSection($rootNode);
8686
$this->addRouterSection($rootNode);
8787
$this->addSessionSection($rootNode);
88+
$this->addRequestSection($rootNode);
8889
$this->addTemplatingSection($rootNode);
8990
$this->addTranslatorSection($rootNode);
9091
$this->addValidationSection($rootNode);
@@ -256,6 +257,35 @@ private function addSessionSection(ArrayNodeDefinition $rootNode)
256257
;
257258
}
258259

260+
private function addRequestSection(ArrayNodeDefinition $rootNode)
261+
{
262+
$rootNode
263+
->children()
264+
->arrayNode('request')
265+
->info('request configuration')
266+
->canBeUnset()
267+
->fixXmlConfig('format')
268+
->children()
269+
->arrayNode('formats')
270+
->useAttributeAsKey('name')
271+
->prototype('array')
272+
->beforeNormalization()
273+
->ifTrue(function ($v) { return is_array($v) && isset($v['mime_type']); })
274+
->then(function ($v) { return $v['mime_type']; })
275+
->end()
276+
->beforeNormalization()
277+
->ifTrue(function ($v) { return !is_array($v); })
278+
->then(function ($v) { return array($v); })
279+
->end()
280+
->prototype('scalar')->end()
281+
->end()
282+
->end()
283+
->end()
284+
->end()
285+
->end()
286+
;
287+
}
288+
259289
private function addTemplatingSection(ArrayNodeDefinition $rootNode)
260290
{
261291
$organizeUrls = function ($urls) {

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public function load(array $configs, ContainerBuilder $container)
9191
$this->registerSessionConfiguration($config['session'], $container, $loader);
9292
}
9393

94+
if (isset($config['request'])) {
95+
$this->registerRequestConfiguration($config['request'], $container, $loader);
96+
}
97+
9498
$loader->load('security.xml');
9599

96100
if ($this->isConfigEnabled($container, $config['form'])) {
@@ -382,6 +386,24 @@ private function registerSessionConfiguration(array $config, ContainerBuilder $c
382386
$container->setParameter('session.metadata.update_threshold', $config['metadata_update_threshold']);
383387
}
384388

389+
/**
390+
* Loads the request configuration.
391+
*
392+
* @param array $config A session configuration array
393+
* @param ContainerBuilder $container A ContainerBuilder instance
394+
* @param XmlFileLoader $loader An XmlFileLoader instance
395+
*/
396+
private function registerRequestConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
397+
{
398+
if ($config['formats']) {
399+
$loader->load('request.xml');
400+
$container
401+
->getDefinition('request.add_request_formats_listener')
402+
->replaceArgument(0, $config['formats'])
403+
;
404+
}
405+
}
406+
385407
/**
386408
* Loads the templating configuration.
387409
*
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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="request.add_request_formats_listener.class">Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener</parameter>
9+
</parameters>
10+
11+
<services>
12+
<service id="request.add_request_formats_listener" class="%request.add_request_formats_listener.class%">
13+
<tag name="kernel.event_subscriber" />
14+
<argument/>
15+
</service>
16+
</services>
17+
</container>

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<xsd:element name="profiler" type="profiler" minOccurs="0" maxOccurs="1" />
1717
<xsd:element name="router" type="router" minOccurs="0" maxOccurs="1" />
1818
<xsd:element name="session" type="session" minOccurs="0" maxOccurs="1" />
19+
<xsd:element name="request" type="request" minOccurs="0" maxOccurs="1" />
1920
<xsd:element name="templating" type="templating" minOccurs="0" maxOccurs="1" />
2021
<xsd:element name="translator" type="translator" minOccurs="0" maxOccurs="1" />
2122
<xsd:element name="validation" type="validation" minOccurs="0" maxOccurs="1" />
@@ -101,6 +102,19 @@
101102
<xsd:attribute name="save-path" type="xsd:string" />
102103
</xsd:complexType>
103104

105+
<xsd:complexType name="request">
106+
<xsd:sequence>
107+
<xsd:element name="format" type="format" minOccurs="0" maxOccurs="unbounded" />
108+
</xsd:sequence>
109+
</xsd:complexType>
110+
111+
<xsd:complexType name="format">
112+
<xsd:choice minOccurs="1" maxOccurs="unbounded">
113+
<xsd:element name="mime-type" type="xsd:string" />
114+
</xsd:choice>
115+
<xsd:attribute name="name" type="xsd:string" use="required"/>
116+
</xsd:complexType>
117+
104118
<xsd:complexType name="templating">
105119
<xsd:sequence>
106120
<xsd:element name="loader" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,14 @@
7070
'debug' => true,
7171
'file_cache_dir' => '%kernel.cache_dir%/annotations',
7272
),
73-
'ide' => 'file%%link%%format'
73+
'ide' => 'file%%link%%format',
74+
'request' => array(
75+
'formats' => array(
76+
'csv' => array(
77+
'text/csv',
78+
'text/plain',
79+
),
80+
'pdf' => 'application/pdf'
81+
)
82+
)
7483
));
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', array(
4+
'request' => array(
5+
'formats' => array(),
6+
),
7+
));

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
<framework:profiler only-exceptions="true" enabled="false" />
1414
<framework:router resource="%kernel.root_dir%/config/routing.xml" type="xml" />
1515
<framework:session gc-maxlifetime="90000" gc-probability="1" gc-divisor="108" storage-id="session.storage.native" handler-id="session.handler.native_file" name="_SYMFONY" cookie-lifetime="86400" cookie-path="/" cookie-domain="example.com" cookie-secure="true" cookie-httponly="true" save-path="/path/to/sessions" />
16+
<framework:request>
17+
<framework:format name="csv">
18+
<framework:mime-type>text/csv</framework:mime-type>
19+
<framework:mime-type>text/plain</framework:mime-type>
20+
</framework:format>
21+
<framework:format name="pdf">
22+
<framework:mime-type>application/pdf</framework:mime-type>
23+
</framework:format>
24+
</framework:request>
1625
<framework:templating assets-version="SomeVersionScheme" cache="/path/to/cache" >
1726
<framework:loader>loader.foo</framework:loader>
1827
<framework:loader>loader.bar</framework:loader>
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
<framework:config>
9+
<framework:request />
10+
</framework:config>
11+
</container>

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ framework:
5555
debug: true
5656
file_cache_dir: %kernel.cache_dir%/annotations
5757
ide: file%%link%%format
58+
request:
59+
formats:
60+
csv: ['text/csv', 'text/plain']
61+
pdf: 'application/pdf'
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
framework:
2+
request:
3+
formats: ~

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ public function testNullSessionHandler()
145145
$this->assertNull($container->getDefinition('session.storage.php_bridge')->getArgument(0));
146146
}
147147

148+
public function testRequest()
149+
{
150+
$container = $this->createContainerFromFile('full');
151+
152+
$this->assertTrue($container->hasDefinition('request.add_request_formats_listener'), '->registerRequestConfiguration() loads request.xml');
153+
$listenerDef = $container->getDefinition('request.add_request_formats_listener');
154+
$this->assertEquals(array('csv' => array('text/csv', 'text/plain'), 'pdf' => array('application/pdf')), $listenerDef->getArgument(0));
155+
}
156+
157+
public function testEmptyRequestFormats()
158+
{
159+
$container = $this->createContainerFromFile('request');
160+
161+
$this->assertFalse($container->hasDefinition('request.add_request_formats_listener'), '->registerRequestConfiguration() does not load request.xml when no request formats are defined');
162+
}
163+
148164
public function testTemplating()
149165
{
150166
$container = $this->createContainerFromFile('full');
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\HttpKernel\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\HttpKernel\KernelEvents;
16+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17+
18+
/**
19+
* Adds configured formats to each request
20+
*
21+
* @author Gildas Quemener <gildas.quemener@gmail.com>
22+
*/
23+
class AddRequestFormatsListener implements EventSubscriberInterface
24+
{
25+
/**
26+
* @var array
27+
*/
28+
protected $formats;
29+
30+
/**
31+
* @param array $formats
32+
*/
33+
public function __construct(array $formats)
34+
{
35+
$this->formats = $formats;
36+
}
37+
38+
/**
39+
* Adds request formats
40+
*
41+
* @param GetResponseEvent $event
42+
*/
43+
public function onKernelRequest(GetResponseEvent $event)
44+
{
45+
foreach ($this->formats as $format => $mimeTypes) {
46+
$event->getRequest()->setFormat($format, $mimeTypes);
47+
}
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public static function getSubscribedEvents()
54+
{
55+
return array(KernelEvents::REQUEST => 'onKernelRequest');
56+
}
57+
}
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpKernel\KernelEvents;
17+
18+
/**
19+
* Test AddRequestFormatsListener class
20+
*
21+
* @author Gildas Quemener <gildas.quemener@gmail.com>
22+
*/
23+
class AddRequestFormatsListenerTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @var AddRequestFormatsListener
27+
*/
28+
private $listener;
29+
30+
protected function setUp()
31+
{
32+
$this->listener = new AddRequestFormatsListener(array('csv' => array('text/csv', 'text/plain')));
33+
}
34+
35+
protected function tearDown()
36+
{
37+
$this->listener = null;
38+
}
39+
40+
public function testIsAnEventSubscriber()
41+
{
42+
$this->assertInstanceOf('Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener);
43+
}
44+
45+
public function testRegisteredEvent()
46+
{
47+
$this->assertEquals(
48+
array(KernelEvents::REQUEST => 'onKernelRequest'),
49+
AddRequestFormatsListener::getSubscribedEvents()
50+
);
51+
}
52+
53+
public function testSetAdditionalFormats()
54+
{
55+
$request = $this->getRequestMock();
56+
$event = $this->getGetResponseEventMock($request);
57+
58+
$request->expects($this->once())
59+
->method('setFormat')
60+
->with('csv', array('text/csv', 'text/plain'));
61+
62+
$this->listener->onKernelRequest($event);
63+
}
64+
65+
protected function getRequestMock()
66+
{
67+
return $this->getMock('Symfony\Component\HttpFoundation\Request');
68+
}
69+
70+
protected function getGetResponseEventMock(Request $request)
71+
{
72+
$event = $this
73+
->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
74+
->disableOriginalConstructor()
75+
->getMock();
76+
77+
$event->expects($this->any())
78+
->method('getRequest')
79+
->will($this->returnValue($request));
80+
81+
return $event;
82+
}
83+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.