|
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
|
|
|
// Workaround for https://github.com/symfony/symfony/issues/19749 |
|
45
|
6 |
|
if ($this->requestStack->getMasterRequest() !== $request) { |
|
46
|
|
|
$this->usingSwagger = false; |
|
47
|
|
|
$this->decorated->setController($controller); |
|
48
|
|
|
|
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
try { |
|
53
|
6 |
|
$this->currentValidator = $this->validatorMap->getValidator($request); |
|
|
|
|
|
|
54
|
5 |
|
$this->currentSchema = null; |
|
|
|
|
|
|
55
|
5 |
|
$this->usingSwagger = true; |
|
56
|
6 |
|
} catch (NoValidatorException $e) { |
|
57
|
1 |
|
$this->usingSwagger = false; |
|
58
|
1 |
|
$this->decorated->setController($controller); |
|
59
|
|
|
} |
|
60
|
6 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
6 |
|
public function get($name, $strict = null) |
|
66
|
|
|
{ |
|
67
|
6 |
|
if ($this->usingSwagger) { |
|
68
|
5 |
|
$currentSchema = $this->getCurrentSchema(); |
|
69
|
5 |
|
if (array_key_exists($name, $currentSchema)) { |
|
70
|
4 |
|
$default = isset($currentSchema[$name]->default) ? $currentSchema[$name]->default : null; |
|
71
|
|
|
|
|
72
|
4 |
|
return $this->requestStack->getCurrentRequest()->query->get($name, $default); |
|
73
|
|
|
} else { |
|
74
|
1 |
|
throw new \InvalidArgumentException(sprintf("No swagger definition for parameter '%s'.", $name)); |
|
75
|
|
|
} |
|
76
|
|
|
} else { |
|
77
|
1 |
|
return $this->decorated->get($name, $strict); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function all($strict = null) |
|
85
|
|
|
{ |
|
86
|
2 |
|
if ($this->usingSwagger) { |
|
87
|
1 |
|
$parameters = []; |
|
88
|
1 |
|
foreach ($this->getCurrentSchema() as $item) { |
|
89
|
1 |
|
$parameters[$item->name] = $this->get($item->name); |
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
return $parameters; |
|
93
|
|
|
} else { |
|
94
|
1 |
|
return $this->decorated->all($strict); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return \stdClass[] |
|
100
|
|
|
*/ |
|
101
|
5 |
|
private function getCurrentSchema() |
|
102
|
|
|
{ |
|
103
|
5 |
|
$request = $this->requestStack->getCurrentRequest(); |
|
104
|
5 |
|
$path = $request->getPathInfo(); |
|
105
|
|
|
|
|
106
|
5 |
|
if ($this->currentSchema === null) { |
|
107
|
5 |
|
$schemaManager = $this->currentValidator->getSchemaManager(); |
|
108
|
|
|
|
|
109
|
5 |
|
$this->currentSchema = []; |
|
110
|
5 |
|
if ($schemaManager->findPathInTemplates($path, $template, $params)) { |
|
111
|
5 |
|
$currentSchema = $schemaManager->getRequestQueryParameters($template, $request->getMethod()); |
|
112
|
|
|
|
|
113
|
5 |
|
foreach ($currentSchema as $keySchema) { |
|
114
|
5 |
|
$this->currentSchema[$keySchema->name] = $keySchema; |
|
115
|
5 |
|
} |
|
116
|
5 |
|
} |
|
117
|
5 |
|
} |
|
118
|
|
|
|
|
119
|
5 |
|
return $this->currentSchema; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
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: