Permalink
Browse files

added ability to define additional format/mime type mappings in the R…

…equest
  • Loading branch information...
1 parent 974e862 commit 5da256ca25f9f5a991ec57bc9b72e7dcf396dd36 @lsmith77 lsmith77 committed Oct 4, 2011
@@ -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()
@@ -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))
@@ -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)
@@ -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());
+ }
}
}
@@ -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);
+ }
+ }
+}
View
@@ -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
@@ -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
---------------------------
@@ -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>
@@ -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.