Skip to content

Commit c5ca20d

Browse files
amenophislsmith77
authored andcommitted
add VersionListener
1 parent 5e266a9 commit c5ca20d

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

DependencyInjection/Configuration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ private function addFormatListenerSection(ArrayNodeDefinition $rootNode)
203203
->end()
204204
->end()
205205
->end()
206+
->arrayNode('media_type')
207+
->children()
208+
->scalarNode('version_regex')
209+
->defaultValue('/(v|version)=(?P<version>[0-9\.]+)/')
210+
->end()
211+
->end()
212+
->end()
206213
->end()
207214
->end()
208215
->end();

DependencyInjection/FOSRestExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ public function load(array $configs, ContainerBuilder $container)
144144
$container->getDefinition($this->getAlias().'.format_negotiator')
145145
->addMethodCall('add', array($matcher, $rule));
146146
}
147+
148+
if (!empty($config['format_listener']['media_type']['version_regex'])) {
149+
$container->setParameter($this->getAlias().'.format_listener.media_type.version_regex', $config['format_listener']['media_type']['version_regex']);
150+
} else {
151+
$container->removeDefinition('fos_rest.version_listener');
152+
}
147153
}
148154

149155
if (!empty($config['view']['exception_wrapper_handler'])) {

EventListener/VersionListener.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\EventListener;
13+
14+
use FOS\RestBundle\View\ConfigurableViewHandlerInterface;
15+
use FOS\RestBundle\View\ViewHandlerInterface;
16+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17+
18+
class VersionListener
19+
{
20+
/** @var ViewHandlerInterface */
21+
private $viewHandler;
22+
23+
/** @var string */
24+
private $regex;
25+
26+
/** @var string */
27+
private $version = false;
28+
29+
public function getVersion()
30+
{
31+
return $this->version;
32+
}
33+
34+
public function __construct(ViewHandlerInterface $viewHandler)
35+
{
36+
$this->viewHandler = $viewHandler;
37+
}
38+
39+
public function setRegex($regex)
40+
{
41+
$this->regex = $regex;
42+
}
43+
44+
public function onKernelRequest(GetResponseEvent $event)
45+
{
46+
$request = $event->getRequest();
47+
48+
$mediaType = $request->attributes->get('media_type');
49+
50+
if (1 === preg_match($this->regex, $mediaType, $matches)) {
51+
$this->version = $matches['version'];
52+
$request->attributes->set('version', $this->version);
53+
54+
if ($this->viewHandler instanceof ConfigurableViewHandlerInterface) {
55+
$this->viewHandler->setExclusionStrategyVersion($this->version);
56+
}
57+
}
58+
}
59+
}

Resources/config/format_listener.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
98
<parameter key="fos_rest.format_listener.class">FOS\RestBundle\EventListener\FormatListener</parameter>
10-
9+
<parameter key="fos_rest.version_listener.class">FOS\RestBundle\EventListener\VersionListener</parameter>
1110
</parameters>
1211

1312
<services>
@@ -17,5 +16,13 @@
1716
<argument type="service" id="fos_rest.format_negotiator" />
1817
</service>
1918

19+
<service id="fos_rest.version_listener" class="%fos_rest.version_listener.class%">
20+
<argument type="service" id="fos_rest.view_handler.default" />
21+
<call method="setRegex">
22+
<argument type="string">%fos_rest.format_listener.media_type.version_regex%</argument>
23+
</call>
24+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
25+
</service>
26+
2027
</services>
2128
</container>

Resources/doc/3-listener-support.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,23 @@ natively or it needs to be added as documented here or using the mime type
375375
listener explained below:
376376
http://symfony.com/doc/current/cookbook/request/mime_type.html
377377

378+
The format listener can also determine the version of the selected media type
379+
based on a regular expression. The regular expression can be configured as
380+
follows. Setting it to an empty value will disable the behavior entirely.
381+
382+
```
383+
fos_rest:
384+
format_listener:
385+
media_type:
386+
version_regex: '/(v|version)=(?P<version>[0-9\.]+)/'
387+
```
388+
389+
The matched version is set as a Request attribute with the name ``version``,
390+
and when using JMS serializer it is also set as an exclusion strategy
391+
automatically in the ``ViewHandler``. See the following documentation
392+
for details:
393+
http://jmsyst.com/libs/serializer/master/cookbook/exclusion_strategies#versioning-objects
394+
378395
### Mime type listener
379396
380397
This listener allows registering additional mime types in the ``Request``

Resources/doc/configuration-reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,7 @@ fos_rest:
8686

8787
# Prototype
8888
name: []
89+
media_type:
90+
version_regex: '/(v|version)=(?P<version>[0-9\.]+)/'
8991
```
9092

0 commit comments

Comments
 (0)