forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApiPlatformController.php
116 lines (95 loc) · 4.15 KB
/
ApiPlatformController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Laravel\Controller;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Symfony\Component\HttpFoundation\Response;
class ApiPlatformController extends Controller
{
/**
* @param ProviderInterface<object> $provider
* @param ProcessorInterface<iterable<object>|object|null, Response> $processor
*/
public function __construct(
protected OperationMetadataFactoryInterface $operationMetadataFactory,
protected ProviderInterface $provider,
protected ProcessorInterface $processor,
) {
}
/**
* Display a listing of the resource.
*/
public function __invoke(Request $request): Response
{
$operation = $request->attributes->get('_api_operation');
if (!$operation) {
throw new \RuntimeException('Operation not found.');
}
if (!$operation instanceof HttpOperation) {
throw new \LogicException('Operation is not an HttpOperation.');
}
$uriVariables = $this->getUriVariables($request, $operation);
$request->attributes->set('_api_uri_variables', $uriVariables);
// at some point we could introduce that back
// if ($this->uriVariablesConverter) {
// $context = ['operation' => $operation, 'uri_variables_map' => $uriVariablesMap];
// $identifiers = $this->uriVariablesConverter->convert($identifiers, $operation->getClass() ?? $resourceClass, $context);
// }
$context = [
'request' => $request,
'uri_variables' => $uriVariables,
'resource_class' => $operation->getClass(),
];
if (null === $operation->canValidate()) {
$operation = $operation->withValidate(!$request->isMethodSafe() && !$request->isMethod('DELETE'));
}
if (null === $operation->canRead()) {
$operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe());
}
if (null === $operation->canDeserialize()) {
$operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true));
}
$body = $this->provider->provide($operation, $uriVariables, $context);
// The provider can change the Operation, extract it again from the Request attributes
if ($request->attributes->get('_api_operation') !== $operation) {
$operation = $request->attributes->get('_api_operation');
$uriVariables = $this->getUriVariables($request, $operation);
}
$context['previous_data'] = $request->attributes->get('previous_data');
$context['data'] = $request->attributes->get('data');
if (null === $operation->canWrite()) {
$operation = $operation->withWrite(!$request->isMethodSafe());
}
if (null === $operation->canSerialize()) {
$operation = $operation->withSerialize(true);
}
return $this->processor->process($body, $operation, $uriVariables, $context);
}
/**
* @return array<string, mixed>
*/
private function getUriVariables(Request $request, HttpOperation $operation): array
{
$uriVariables = [];
foreach ($operation->getUriVariables() ?? [] as $parameterName => $_) {
$parameter = $request->route($parameterName);
if (\is_string($parameter) && ($format = $request->attributes->get('_format')) && str_contains($parameter, $format)) {
$parameter = substr($parameter, 0, \strlen($parameter) - (\strlen($format) + 1));
}
$uriVariables[(string) $parameterName] = $parameter;
}
return $uriVariables;
}
}