1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nicofuma\SwaggerBundle\FosRest; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\Request\ParamFetcherInterface; |
6
|
|
|
use Nicofuma\SwaggerBundle\Exception\NoValidatorException; |
7
|
|
|
use Nicofuma\SwaggerBundle\Validator\Validator; |
8
|
|
|
use Nicofuma\SwaggerBundle\Validator\ValidatorMap; |
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
10
|
|
|
|
11
|
|
|
class ParamFetcher implements ParamFetcherInterface |
12
|
|
|
{ |
13
|
|
|
/** @var ParamFetcherInterface */ |
14
|
|
|
private $decorated; |
15
|
|
|
|
16
|
|
|
/** @var ValidatorMap */ |
17
|
|
|
private $validatorMap; |
18
|
|
|
|
19
|
|
|
/** @var RequestStack */ |
20
|
|
|
private $requestStack; |
21
|
|
|
|
22
|
|
|
/** @var Validator */ |
23
|
|
|
private $currentValidator; |
24
|
|
|
|
25
|
|
|
/** @var \stdClass[] */ |
26
|
|
|
private $currentSchema; |
27
|
|
|
|
28
|
|
|
private $usingSwagger = false; |
29
|
|
|
|
30
|
6 |
|
public function __construct(ParamFetcherInterface $decorated, ValidatorMap $validatorMap, RequestStack $requestStack) |
31
|
|
|
{ |
32
|
6 |
|
$this->decorated = $decorated; |
33
|
6 |
|
$this->validatorMap = $validatorMap; |
34
|
6 |
|
$this->requestStack = $requestStack; |
35
|
6 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
6 |
|
public function setController($controller) |
41
|
|
|
{ |
42
|
6 |
|
$request = $this->requestStack->getCurrentRequest(); |
43
|
|
|
|
44
|
|
|
try { |
45
|
6 |
|
$this->currentValidator = $this->validatorMap->getValidator($request); |
|
|
|
|
46
|
5 |
|
$this->currentSchema = null; |
|
|
|
|
47
|
5 |
|
$this->usingSwagger = true; |
48
|
6 |
|
} catch (NoValidatorException $e) { |
49
|
1 |
|
$this->usingSwagger = false; |
50
|
1 |
|
$this->decorated->setController($controller); |
51
|
|
|
} |
52
|
6 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
6 |
|
public function get($name, $strict = null) |
58
|
|
|
{ |
59
|
6 |
|
if ($this->usingSwagger) { |
60
|
5 |
|
$currentSchema = $this->getCurrentSchema(); |
61
|
5 |
|
if (array_key_exists($name, $currentSchema)) { |
62
|
4 |
|
$default = isset($currentSchema[$name]->default) ? $currentSchema[$name]->default : null; |
63
|
|
|
|
64
|
4 |
|
return $this->requestStack->getCurrentRequest()->query->get($name, $default); |
65
|
|
|
} else { |
66
|
1 |
|
throw new \InvalidArgumentException(sprintf("No swagger definition for parameter '%s'.", $name)); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
1 |
|
return $this->decorated->get($name, $strict); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
2 |
|
public function all($strict = false) |
77
|
|
|
{ |
78
|
2 |
|
if ($this->usingSwagger) { |
79
|
1 |
|
$parameters = []; |
80
|
1 |
|
foreach ($this->getCurrentSchema() as $item) { |
81
|
1 |
|
$parameters[$item->name] = $this->get($item->name); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
1 |
|
return $parameters; |
85
|
|
|
} else { |
86
|
1 |
|
return $this->decorated->all($strict); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return \stdClass[] |
92
|
|
|
*/ |
93
|
5 |
|
private function getCurrentSchema() |
94
|
|
|
{ |
95
|
5 |
|
$request = $this->requestStack->getCurrentRequest(); |
96
|
5 |
|
$path = $request->getPathInfo(); |
97
|
|
|
|
98
|
5 |
|
if ($this->currentSchema === null) { |
99
|
5 |
|
$schemaManager = $this->currentValidator->getSchemaManager(); |
100
|
|
|
|
101
|
5 |
|
$this->currentSchema = []; |
102
|
5 |
|
if ($schemaManager->findPathInTemplates($path, $template, $params)) { |
103
|
5 |
|
$currentSchema = $schemaManager->getRequestQueryParameters($template, $request->getMethod()); |
104
|
|
|
|
105
|
5 |
|
foreach ($currentSchema as $keySchema) { |
106
|
5 |
|
$this->currentSchema[$keySchema->name] = $keySchema; |
107
|
5 |
|
} |
108
|
5 |
|
} |
109
|
5 |
|
} |
110
|
|
|
|
111
|
5 |
|
return $this->currentSchema; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: