-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] Added configuration for additionnal request formats
- Loading branch information
Showing
13 changed files
with
283 additions
and
1 deletion.
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
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
17 changes: 17 additions & 0 deletions
17
src/Symfony/Bundle/FrameworkBundle/Resources/config/request.xml
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,17 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="request.add_request_formats_listener.class">Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="request.add_request_formats_listener" class="%request.add_request_formats_listener.class%"> | ||
<tag name="kernel.event_subscriber" /> | ||
<argument/> | ||
</service> | ||
</services> | ||
</container> |
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
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
7 changes: 7 additions & 0 deletions
7
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/request.php
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,7 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('framework', array( | ||
'request' => array( | ||
'formats' => array(), | ||
), | ||
)); |
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
11 changes: 11 additions & 0 deletions
11
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/request.xml
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,11 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
<framework:config> | ||
<framework:request /> | ||
</framework:config> | ||
</container> |
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
3 changes: 3 additions & 0 deletions
3
src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/request.yml
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,3 @@ | ||
framework: | ||
request: | ||
formats: ~ |
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
57 changes: 57 additions & 0 deletions
57
src/Symfony/Component/HttpKernel/EventListener/AddRequestFormatsListener.php
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'); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/HttpKernel/Tests/EventListener/AddRequestFormatsListenerTest.php
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; | ||
} | ||
} |