1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\EventListener; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface; |
17
|
|
|
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
18
|
|
|
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface; |
19
|
|
|
use ApiPlatform\Core\Exception\PropertyNotFoundException; |
20
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
21
|
|
|
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
22
|
|
|
use ApiPlatform\Core\Util\RequestAttributesExtractor; |
23
|
|
|
use ApiPlatform\Core\Util\RequestParser; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
26
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Retrieves data from the applicable data provider and sets it as a request parameter called data. |
30
|
|
|
* |
31
|
|
|
* @author Kévin Dunglas <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
final class ReadListener |
34
|
|
|
{ |
35
|
|
|
private $collectionDataProvider; |
36
|
|
|
private $itemDataProvider; |
37
|
|
|
private $subresourceDataProvider; |
38
|
|
|
private $serializerContextBuilder; |
39
|
|
|
|
40
|
|
|
public function __construct(CollectionDataProviderInterface $collectionDataProvider, ItemDataProviderInterface $itemDataProvider, SubresourceDataProviderInterface $subresourceDataProvider = null, SerializerContextBuilderInterface $serializerContextBuilder = null) |
41
|
|
|
{ |
42
|
|
|
$this->collectionDataProvider = $collectionDataProvider; |
43
|
|
|
$this->itemDataProvider = $itemDataProvider; |
44
|
|
|
$this->subresourceDataProvider = $subresourceDataProvider; |
45
|
|
|
$this->serializerContextBuilder = $serializerContextBuilder; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Calls the data provider and sets the data attribute. |
50
|
|
|
* |
51
|
|
|
* @param GetResponseEvent $event |
52
|
|
|
* |
53
|
|
|
* @throws NotFoundHttpException |
54
|
|
|
*/ |
55
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
56
|
|
|
{ |
57
|
|
|
$request = $event->getRequest(); |
58
|
|
|
if ( |
59
|
|
|
!($attributes = RequestAttributesExtractor::extractAttributes($request)) |
60
|
|
|
|| !$attributes['receive'] |
61
|
|
|
) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (null === $filters = $request->attributes->get('_api_filters')) { |
66
|
|
|
$queryString = $request->getQueryString(); |
67
|
|
|
$filters = $queryString ? RequestParser::parseRequestParams($queryString) : null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$context = null === $filters ? [] : ['filters' => $filters]; |
71
|
|
|
if ($this->serializerContextBuilder) { |
72
|
|
|
// Builtin data providers are able to use the serialization context to automatically add join clauses |
73
|
|
|
$context['normalization_context'] = $this->serializerContextBuilder->createFromRequest($request, true, $attributes); |
74
|
|
|
$request->attributes->set('_api_normalization_context', $context['normalization_context']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$data = []; |
78
|
|
|
if (isset($attributes['item_operation_name'])) { |
79
|
|
|
$data = $this->getItemData($request, $attributes, $context); |
80
|
|
|
} elseif (isset($attributes['collection_operation_name'])) { |
81
|
|
|
$data = $this->getCollectionData($request, $attributes, $context); |
82
|
|
|
} elseif (isset($attributes['subresource_operation_name'])) { |
83
|
|
|
$data = $this->getSubresourceData($request, $attributes, $context); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$request->attributes->set('data', $data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieves data for a collection operation. |
91
|
|
|
* |
92
|
|
|
* @return array|\Traversable|null |
93
|
|
|
*/ |
94
|
|
|
private function getCollectionData(Request $request, array $attributes, array $context) |
95
|
|
|
{ |
96
|
|
|
if ($request->isMethod(Request::METHOD_POST)) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->collectionDataProvider->getCollection($attributes['resource_class'], $attributes['collection_operation_name'], $context); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets data for an item operation. |
105
|
|
|
* |
106
|
|
|
* @throws NotFoundHttpException |
107
|
|
|
* |
108
|
|
|
* @return object|null |
109
|
|
|
*/ |
110
|
|
|
private function getItemData(Request $request, array $attributes, array $context) |
111
|
|
|
{ |
112
|
|
|
$id = $request->attributes->get('id'); |
113
|
|
|
try { |
114
|
|
|
$data = $this->itemDataProvider->getItem($attributes['resource_class'], $id, $attributes['item_operation_name'], $context); |
115
|
|
|
} catch (PropertyNotFoundException $e) { |
116
|
|
|
$data = null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (null === $data) { |
120
|
|
|
throw new NotFoundHttpException('Not Found'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $data; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Gets data for a nested operation. |
128
|
|
|
* |
129
|
|
|
* @throws NotFoundHttpException |
130
|
|
|
* @throws RuntimeException |
131
|
|
|
* |
132
|
|
|
* @return object|null |
133
|
|
|
*/ |
134
|
|
|
private function getSubresourceData(Request $request, array $attributes, array $context) |
135
|
|
|
{ |
136
|
|
|
if (null === $this->subresourceDataProvider) { |
137
|
|
|
throw new RuntimeException('No subresource data provider.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$attributes['subresource_context'] += $context; |
141
|
|
|
$identifiers = []; |
142
|
|
|
foreach ($attributes['subresource_context']['identifiers'] as $key => list($id, , $hasIdentifier)) { |
143
|
|
|
if (true === $hasIdentifier) { |
144
|
|
|
$identifiers[$id] = $request->attributes->get($id); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$data = $this->subresourceDataProvider->getSubresource($attributes['resource_class'], $identifiers, $attributes['subresource_context'], $attributes['subresource_operation_name']); |
149
|
|
|
|
150
|
|
|
return $data; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.