Skip to content

Commit

Permalink
added ability to define additional format/mime type mappings in the R…
Browse files Browse the repository at this point in the history
…equest
  • Loading branch information
lsmith77 committed Oct 4, 2011
1 parent 974e862 commit 5da256c
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 18 deletions.
48 changes: 31 additions & 17 deletions DependencyInjection/Configuration.php
Expand Up @@ -45,8 +45,33 @@ public function getConfigTreeBuilder()
->scalarNode('default_format')->defaultNull()->end()
->end()
->end()
->arrayNode('service')
->addDefaultsIfNotSet()
->children()
->scalarNode('router')->defaultValue('router')->end()
->scalarNode('templating')->defaultValue('templating')->end()
->scalarNode('serializer')->defaultValue('jms_serializer.serializer')->end()
->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end()
->end()
->end()
->end()
->end();

$this->addViewSection($rootNode);
$this->addExceptionSection($rootNode);
$this->addBodyListenerSection($rootNode);
$this->addFormatListenerSection($rootNode);

return $treeBuilder;
}

private function addViewSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('view')
->fixXmlConfig('format', 'formats')
->fixXmlConfig('mime_type', 'mime_types')
->fixXmlConfig('templating_format', 'templating_formats')
->fixXmlConfig('force_redirect', 'force_redirects')
->addDefaultsIfNotSet()
Expand All @@ -57,6 +82,11 @@ public function getConfigTreeBuilder()
->defaultValue(array('html' => true))
->prototype('boolean')->end()
->end()
->arrayNode('mime_types')
->useAttributeAsKey('name')
->defaultValue(array())
->prototype('scalar')->end()
->end()
->arrayNode('formats')
->useAttributeAsKey('name')
->defaultValue(array('json' => true, 'xml' => true))
Expand All @@ -71,23 +101,7 @@ public function getConfigTreeBuilder()
->scalarNode('failed_validation')->defaultValue(Codes::HTTP_BAD_REQUEST)->end()
->end()
->end()
->arrayNode('service')
->addDefaultsIfNotSet()
->children()
->scalarNode('router')->defaultValue('router')->end()
->scalarNode('templating')->defaultValue('templating')->end()
->scalarNode('serializer')->defaultValue('jms_serializer.serializer')->end()
->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end()
->end()
->end()
->end()
->end();

$this->addExceptionSection($rootNode);
$this->addBodyListenerSection($rootNode);
$this->addFormatListenerSection($rootNode);

return $treeBuilder;
->end();
}

private function addBodyListenerSection(ArrayNodeDefinition $rootNode)
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/FOSRestExtension.php
Expand Up @@ -97,5 +97,13 @@ public function load(array $configs, ContainerBuilder $container)
} else {
$container->setParameter($this->getAlias().'.default_priorities', array());
}

if (!empty($config['view']['mime_types'])) {
$loader->load('mime_types_listener.xml');

$container->setParameter($this->getAlias().'.mime_types', $config['view']['mime_types']);
} else {
$container->setParameter($this->getAlias().'.default_priorities', array());
}
}
}
51 changes: 51 additions & 0 deletions EventListener/MimeTypeListener.php
@@ -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);
}
}
}
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -240,7 +240,8 @@ https://github.com/schmittjoh/JMSSerializerBundle/blob/master/Resources/doc/inde
Listener support
----------------

All listeners of this bundle are enabled by default. You can disable one or more the listeners.
All but the ``mime_type`` listeners of this bundle are enabled by default.
You can disable one or more the listeners.
For example, below you can see how to disable the body listener:

```yaml
Expand Down Expand Up @@ -401,6 +402,21 @@ Note that the format needs to either be supported by the ``Request`` class nativ
it needs to be added as documented here:
http://symfony.com/doc/current/cookbook/request/mime_type.html

### Mime type listener

This listener allows registering additional mime types in the ``Request`` class.
It works similar to the following cookbook entry:
http://symfony.com/doc/current/cookbook/request/mime_type.html


```yaml
# app/config/config.yml
fos_rest:
view:
mime_types: ['jsonp': 'application/javascript']
```


ExceptionController support
---------------------------

Expand Down
15 changes: 15 additions & 0 deletions Resources/config/mime_type_listener.xml
@@ -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>
40 changes: 40 additions & 0 deletions Tests/EventListener/MimeTypeListenerTest.php
@@ -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'));
}
}

0 comments on commit 5da256c

Please sign in to comment.