Skip to content

Commit 936b9e5

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 c8da30a + 586d347 commit 936b9e5

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines 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 <[email protected]>
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 <[email protected]>
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+
}
Lines 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 <[email protected]>
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 <[email protected]>
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)