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\Util; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\OperationType; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Extracts data used by the library form a Request instance. |
21
|
|
|
* |
22
|
|
|
* @author Kévin Dunglas <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @internal |
25
|
|
|
*/ |
26
|
|
|
final class RequestAttributesExtractor |
27
|
|
|
{ |
28
|
|
|
private function __construct() |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Extracts resource class, operation name and format request attributes. Returns an empty array if the request does |
34
|
|
|
* not contain required attributes. |
35
|
|
|
* |
36
|
|
|
* @param Request $request |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public static function extractAttributes(Request $request) |
41
|
|
|
{ |
42
|
|
|
$result = [ |
43
|
|
|
'resource_class' => $request->attributes->get('_api_resource_class'), |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
if ($subresourceContext = $request->attributes->get('_api_subresource_context')) { |
47
|
|
|
$result['subresource_context'] = $subresourceContext; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (null === $result['resource_class']) { |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$hasRequestAttributeKey = false; |
55
|
|
|
foreach (OperationType::TYPES as $operationType) { |
56
|
|
|
$attribute = "_api_{$operationType}_operation_name"; |
57
|
|
|
if ($request->attributes->has($attribute)) { |
58
|
|
|
$result["{$operationType}_operation_name"] = $request->attributes->get($attribute); |
59
|
|
|
$hasRequestAttributeKey = true; |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (false === $hasRequestAttributeKey) { |
65
|
|
|
return []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (null === $apiRequest = $request->attributes->get('_api_receive')) { |
69
|
|
|
$result['receive'] = true; |
70
|
|
|
} else { |
71
|
|
|
$result['receive'] = (bool) $apiRequest; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $result; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|