Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ability to define additional format/mime type mappings in the R…
…equest
- Loading branch information
Showing
6 changed files
with
162 additions
and
18 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
|
||
/** | ||
* This listener handles registering custom mime types. | ||
* | ||
* @author Lukas Kahwe Smith <smith@pooteeweet.org> | ||
*/ | ||
class MimeTypeListener | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $mimeTypes; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param array $mimeTypes key format, value mime type | ||
*/ | ||
public function __construct(array $mimeTypes) | ||
{ | ||
$this->mimeTypes = $mimeTypes; | ||
} | ||
|
||
/** | ||
* Core request handler | ||
* | ||
* @param GetResponseEvent $event The event | ||
*/ | ||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
|
||
foreach ($this->mimeTypes as $format => $mimeType) { | ||
$request->setFormat($format, $mimeType); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?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"> | ||
|
||
<services> | ||
|
||
<service id="fos_rest.mime_type_listener" class="FOS\RestBundle\EventListener\MimeTypeListener"> | ||
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="-200" /> | ||
<argument>%fos_rest.mime_types%</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSRestBundle package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\RestBundle\Tests\EventListener; | ||
|
||
use Symfony\Component\HttpFoundation\Request, | ||
FOS\RestBundle\EventListener\MimeTypeListener; | ||
|
||
/** | ||
* Request listener test | ||
* | ||
* @author Lukas Kahwe Smith <smith@pooteeweet.org> | ||
*/ | ||
class MimeTypeListenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testOnKernelRequest() | ||
{ | ||
$listener = new MimeTypeListener(array('jsonp' => 'application/javascript')); | ||
|
||
$request = new Request; | ||
$event = $this->getMockBuilder('\Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); | ||
$event->expects($this->once()) | ||
->method('getRequest') | ||
->will($this->returnValue($request)); | ||
|
||
$this->assertNull($request->getMimeType('jsonp')); | ||
|
||
$listener->onKernelRequest($event); | ||
|
||
$this->assertEquals('application/javascript', $request->getMimeType('jsonp')); | ||
} | ||
} |