Skip to content

Commit 24de140

Browse files
committed
Calculate Request parameter within the __construct of BaseProvider
1 parent 39e67c5 commit 24de140

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

src/ApiPlatform/State/Base/BaseProvider.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,24 @@ abstract class BaseProvider implements ProviderInterface, ProcessorInterface
5858

5959
protected const TEXT_UNDEFINED_METHOD = 'Please overwrite the "%s" method in your provider to use this function.';
6060

61+
protected Request $request;
62+
6163
/**
6264
* @param ParameterBagInterface $parameterBag
63-
* @param RequestStack $request
65+
* @param RequestStack $requestStack
66+
* @throws CaseUnsupportedException
6467
*/
65-
public function __construct(protected ParameterBagInterface $parameterBag, protected RequestStack $request)
68+
public function __construct(protected ParameterBagInterface $parameterBag, RequestStack $requestStack)
6669
{
6770
$this->input = new ArrayInput([]);
71+
72+
$request = $requestStack->getCurrentRequest();
73+
74+
if (is_null($request)) {
75+
throw new CaseUnsupportedException('Can\'t get the CurrentRequest class(<code>$this->getRequest()->getCurrentRequest();</code>).');
76+
}
77+
78+
$this->request = $request;
6879
}
6980

7081
/**
@@ -170,38 +181,29 @@ protected function getProjectDir(): string
170181
* Returns the current request.
171182
*
172183
* @return Request
173-
* @throws CaseUnsupportedException
174184
*/
175-
protected function getCurrentRequest(): Request
185+
protected function getRequest(): Request
176186
{
177-
$currentRequest = $this->request->getCurrentRequest();
178-
179-
if (is_null($currentRequest)) {
180-
throw new CaseUnsupportedException('Can\'t get the CurrentRequest class(<code>$this->getRequest()->getCurrentRequest();</code>).');
181-
}
182-
183-
return $currentRequest;
187+
return $this->request;
184188
}
185189

186190
/**
187191
* Returns the header bag.
188192
*
189193
* @return HeaderBag
190-
* @throws CaseUnsupportedException
191194
*/
192195
protected function getHeaderBag(): HeaderBag
193196
{
194-
$currentRequest = $this->getCurrentRequest();
197+
$request = $this->getRequest();
195198

196-
return $currentRequest->headers;
199+
return $request->headers;
197200
}
198201

199202
/**
200203
* Returns if the given name exists as a header request.
201204
*
202205
* @param string $name
203206
* @return bool
204-
* @throws CaseUnsupportedException
205207
*/
206208
public function hasHeader(string $name): bool
207209
{
@@ -216,7 +218,6 @@ public function hasHeader(string $name): bool
216218
* @param string $name
217219
* @return string|null
218220
* @throws ArrayKeyNotFoundException
219-
* @throws CaseUnsupportedException
220221
*/
221222
public function getHeader(string $name): ?string
222223
{
@@ -232,7 +233,6 @@ public function getHeader(string $name): ?string
232233
*
233234
* @param string $name
234235
* @return string|null
235-
* @throws CaseUnsupportedException
236236
*/
237237
public function getHeaderAsStringOrNull(string $name): ?string
238238
{
@@ -264,7 +264,6 @@ public function getHeaderAsString(string $name): string
264264
*
265265
* @param string $name
266266
* @return float|null
267-
* @throws CaseUnsupportedException
268267
*/
269268
public function getHeaderAsFloatOrNull(string $name): ?float
270269
{
@@ -296,7 +295,6 @@ public function getHeaderAsFloat(string $name): float
296295
*
297296
* @param string $name
298297
* @return int|null
299-
* @throws CaseUnsupportedException
300298
*/
301299
public function getHeaderAsIntOrNull(string $name): ?int
302300
{
@@ -329,7 +327,6 @@ public function getHeaderAsInt(string $name): int
329327
* @param string $name
330328
* @return bool
331329
* @throws CaseInvalidException
332-
* @throws CaseUnsupportedException
333330
* @throws TypeInvalidException
334331
*/
335332
public function isHeaderAsBoolean(string $name): bool

src/ApiPlatform/State/Base/Raw/BaseRawProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ abstract class BaseRawProvider extends BaseProvider
4141
/**
4242
* BaseDirectProvider constructor.
4343
*/
44-
public function __construct(protected ParameterBagInterface $parameterBag, protected RequestStack $request)
44+
public function __construct(protected ParameterBagInterface $parameterBag, protected RequestStack $requestStack)
4545
{
46-
parent::__construct($this->parameterBag, $this->request);
46+
parent::__construct($this->parameterBag, $this->requestStack);
4747
}
4848

4949
/**

src/ApiPlatform/State/Base/Wrapper/BaseResourceWrapperProvider.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ abstract class BaseResourceWrapperProvider extends BaseProvider
8080
/**
8181
* @param Version $version
8282
* @param ParameterBagInterface $parameterBag
83-
* @param RequestStack $request
83+
* @param RequestStack $requestStack
84+
* @throws CaseUnsupportedException
8485
*/
8586
public function __construct(
8687
protected Version $version,
8788
protected ParameterBagInterface $parameterBag,
88-
protected RequestStack $request
89+
RequestStack $requestStack
8990
)
9091
{
91-
parent::__construct($parameterBag, $request);
92+
parent::__construct($parameterBag, $requestStack);
9293
}
9394

9495
/**
@@ -223,13 +224,12 @@ private function setContext(array $context): void
223224
* Returns the request body as string.
224225
*
225226
* @return string|null
226-
* @throws CaseUnsupportedException
227227
*/
228228
protected function getRequestBody(): ?string
229229
{
230-
$currentRequest = $this->getCurrentRequest();
230+
$request = $this->getRequest();
231231

232-
$requestBody = $currentRequest->getContent();
232+
$requestBody = $request->getContent();
233233

234234
if (empty($requestBody)) {
235235
return null;
@@ -242,7 +242,6 @@ protected function getRequestBody(): ?string
242242
* Returns the request body as JSON object.
243243
*
244244
* @return Json|null
245-
* @throws CaseUnsupportedException
246245
* @throws FileNotFoundException
247246
* @throws FileNotReadableException
248247
* @throws FunctionJsonEncodeException
@@ -643,11 +642,10 @@ protected function isValid(): bool
643642
* Returns the endpoint.
644643
*
645644
* @return string
646-
* @throws CaseUnsupportedException
647645
*/
648646
protected function getEndpoint(): string
649647
{
650-
$pathInfo = explode('/', $this->getCurrentRequest()->getPathInfo());
648+
$pathInfo = explode('/', $this->getRequest()->getPathInfo());
651649

652650
return implode('/', array_slice($pathInfo, 3));
653651
}
@@ -657,11 +655,10 @@ protected function getEndpoint(): string
657655
*
658656
* @param string $word
659657
* @return bool
660-
* @throws CaseUnsupportedException
661658
*/
662659
protected function endpointContains(string $word): bool
663660
{
664-
$pathInfo = explode('/', $this->getCurrentRequest()->getPathInfo());
661+
$pathInfo = explode('/', $this->getRequest()->getPathInfo());
665662

666663
return in_array($word, $pathInfo);
667664
}

src/ApiPlatform/State/VersionProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ final class VersionProvider extends BaseRawProvider
4242
* VersionProvider constructor.
4343
*
4444
* @param ParameterBagInterface $parameterBag
45-
* @param RequestStack $request
45+
* @param RequestStack $requestStack
4646
*/
47-
public function __construct(protected ParameterBagInterface $parameterBag, protected RequestStack $request)
47+
public function __construct(protected ParameterBagInterface $parameterBag, protected RequestStack $requestStack)
4848
{
4949
$this->version = new Version();
5050

51-
parent::__construct($parameterBag, $request);
51+
parent::__construct($parameterBag, $requestStack);
5252
}
5353

5454
/**

0 commit comments

Comments
 (0)