8000 feature #9862 [FrameworkBundle] Added configuration for additionnal r… · symfony/framework-bundle@b25aa0c · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit b25aa0c

Browse 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 | symfony/symfony#8934 | License | MIT | Doc PR | symfony/symfony-docs#3402 Reopening of symfony/symfony#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 b8f01e6 + 5eff833 commit b25aa0c

File tree

11 files changed

+143
-1
lines changed

11 files changed

+143
-1
lines changed

DependencyInjection/Configuration.php

Lines 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) {

DependencyInjection/FrameworkExtension.php

Lines 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
*

Resources/config/request.xml

Lines ch F438 anged: 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>

Resources/config/schema/symfony-1.0.xsd

Lines 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" />

Tests/DependencyInjection/Fixtures/php/full.php

Lines 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
));
Lines 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+
));

Tests/DependencyInjection/Fixtures/xml/full.xml

Lines 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>
Lines 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>

Tests/DependencyInjection/Fixtures/yml/full.yml

Lines 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'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
framework:
2+
request:
3+
formats: ~

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines 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');

0 commit comments

Comments
 (0)
0