|
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\FOSRestBundle; |
|
15
|
|
|
use FOS\RestBundle\Version\ChainVersionResolver; |
|
16
|
|
|
use FOS\RestBundle\Version\VersionResolverInterface; |
|
17
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @internal |
|
21
|
|
|
*/ |
|
22
|
|
|
class VersionListener |
|
23
|
|
|
{ |
|
24
|
|
|
private $versionResolver; |
|
25
|
|
|
private $defaultVersion; |
|
26
|
|
|
|
|
27
|
8 |
|
public function __construct(VersionResolverInterface $versionResolver, $defaultVersion = null) |
|
28
|
|
|
{ |
|
29
|
8 |
|
$this->versionResolver = $versionResolver; |
|
30
|
8 |
|
$this->defaultVersion = $defaultVersion; |
|
31
|
8 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param RequestEvent $event |
|
35
|
|
|
*/ |
|
36
|
8 |
|
public function onKernelRequest($event) |
|
37
|
|
|
{ |
|
38
|
8 |
|
$request = $event->getRequest(); |
|
39
|
|
|
|
|
40
|
8 |
|
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) { |
|
41
|
1 |
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
7 |
|
$version = $this->versionResolver->resolve($request); |
|
45
|
|
|
|
|
46
|
7 |
|
if (!$this->versionResolver instanceof ChainVersionResolver && null !== $version && !is_string($version)) { |
|
47
|
|
|
@trigger_error(sprintf('Not returning a string or null from %s::resolve() when implementing the %s is deprecated since FOSRestBundle 2.8.', get_class($this->versionResolver), VersionResolverInterface::class), E_USER_DEPRECATED); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
7 |
|
if ((false === $version || null === $version) && null !== $this->defaultVersion) { |
|
51
|
3 |
|
$version = $this->defaultVersion; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Return if nothing to do |
|
55
|
7 |
|
if (false === $version || null === $version) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
7 |
|
$request->attributes->set('version', $version); |
|
60
|
7 |
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: