|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\APIBundle\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
15
|
|
|
|
|
16
|
|
|
class VersionService |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var RequestStack */ |
|
19
|
|
|
private $requestStack; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private $supportedVersions; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* VersionService constructor. |
|
26
|
|
|
* @param RequestStack $requestStack |
|
27
|
|
|
* @param array $supportedVersions |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(RequestStack $requestStack, array $supportedVersions) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->requestStack = $requestStack; |
|
32
|
|
|
$this->supportedVersions = $supportedVersions; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param int|null $major |
|
37
|
|
|
* @param int|null $minor |
|
38
|
|
|
* @param int|null $patch |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getLatestVersion($major = null, $minor = null, $patch = null) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($major === null) { |
|
44
|
|
|
$major = max(array_keys($this->supportedVersions)); |
|
45
|
|
|
} |
|
46
|
|
|
if ($minor === null) { |
|
47
|
|
|
$minor = max(array_keys($this->supportedVersions[$major])); |
|
48
|
|
|
} |
|
49
|
|
|
if ($patch === null) { |
|
50
|
|
|
$patch = max($this->supportedVersions[$major][$minor]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (false === array_key_exists($major, $this->supportedVersions) |
|
54
|
|
|
|| false === array_key_exists($minor, $this->supportedVersions[$major]) |
|
55
|
|
|
|| false === array_search($patch, $this->supportedVersions[$major][$minor])) { |
|
56
|
|
|
throw new \InvalidArgumentException("Invalid API version"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
|
|
'major' => $major, |
|
61
|
|
|
'minor' => $minor, |
|
62
|
|
|
'patch' => $patch, |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Request $request |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getVersionFromRequest(Request $request = null) |
|
71
|
|
|
{ |
|
72
|
|
|
if ($request === null) { |
|
73
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$pathVersion = $request->attributes->get('version'); |
|
77
|
|
|
$version = explode('.', $pathVersion, 3); |
|
78
|
|
|
|
|
79
|
|
|
$major = $minor = $patch = null; |
|
80
|
|
|
if (array_key_exists(0, $version)) { |
|
81
|
|
|
$major = $version[0]; |
|
82
|
|
|
} |
|
83
|
|
|
if (array_key_exists(1, $version)) { |
|
84
|
|
|
$minor = $version[1]; |
|
85
|
|
|
} |
|
86
|
|
|
if (array_key_exists(2, $version)) { |
|
87
|
|
|
$patch = $version[2]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->getLatestVersion($major, $minor, $patch); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $components |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getString(array $components) |
|
98
|
|
|
{ |
|
99
|
|
|
return implode('.', $components); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|