-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #9862 [FrameworkBundle] Added configuration for additionnal r…
…equest 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
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
|
||
/** | ||
* Adds configured formats to each request | ||
* | ||
* @author Gildas Quemener <gildas.quemener@gmail.com> | ||
*/ | ||
class AddRequestFormatsListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $formats; | ||
|
||
/** | ||
* @param array $formats | ||
*/ | ||
public function __construct(array $formats) | ||
{ | ||
$this->formats = $formats; | ||
} | ||
|
||
/** | ||
* Adds 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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* Test AddRequestFormatsListener class | ||
* | ||
* @author Gildas Quemener <gildas.quemener@gmail.com> | ||
*/ | ||
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 testSetAdditionalFormats() | ||
{ | ||
$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; | ||
} | ||
} |