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\Bridge\Symfony\Bundle\DataProvider; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\DataProvider\ChainItemDataProvider; |
17
|
|
|
use ApiPlatform\Core\DataProvider\DenormalizedIdentifiersAwareItemDataProviderInterface; |
18
|
|
|
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; |
19
|
|
|
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; |
20
|
|
|
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Anthony GRASSIOT <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class TraceableChainItemDataProvider implements ItemDataProviderInterface |
26
|
|
|
{ |
27
|
|
|
private $dataProviders = []; |
28
|
|
|
private $context = []; |
29
|
|
|
private $providersResponse = []; |
30
|
|
|
|
31
|
|
|
public function __construct(ItemDataProviderInterface $itemDataProvider) |
32
|
|
|
{ |
33
|
|
|
if ($itemDataProvider instanceof ChainItemDataProvider) { |
34
|
|
|
$this->dataProviders = $itemDataProvider->dataProviders; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getProvidersResponse(): array |
39
|
|
|
{ |
40
|
|
|
return $this->providersResponse; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getContext(): array |
44
|
|
|
{ |
45
|
|
|
return $this->context; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) |
49
|
|
|
{ |
50
|
|
|
$this->context = $context; |
51
|
|
|
$match = false; |
52
|
|
|
$result = null; |
53
|
|
|
|
54
|
|
|
foreach ($this->dataProviders as $dataProvider) { |
55
|
|
|
$this->providersResponse[\get_class($dataProvider)] = $match ? null : false; |
56
|
|
|
if ($match) { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
try { |
60
|
|
|
if ($dataProvider instanceof RestrictedDataProviderInterface |
61
|
|
|
&& !$dataProvider->supports($resourceClass, $operationName, $context)) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$identifier = $id; |
66
|
|
|
if (!$dataProvider instanceof DenormalizedIdentifiersAwareItemDataProviderInterface && $identifier && \is_array($identifier)) { |
67
|
|
|
if (\count($identifier) > 1) { |
68
|
|
|
@trigger_error(sprintf('Receiving "$id" as non-array in an item data provider is deprecated in 2.3 in favor of implementing "%s".', DenormalizedIdentifiersAwareItemDataProviderInterface::class), E_USER_DEPRECATED); |
69
|
|
|
$identifier = http_build_query($identifier, '', ';'); |
70
|
|
|
} else { |
71
|
|
|
$identifier = current($identifier); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$result = $dataProvider->getItem($resourceClass, $identifier, $operationName, $context); |
76
|
|
|
$this->providersResponse[\get_class($dataProvider)] = $match = true; |
77
|
|
|
} catch (ResourceClassNotSupportedException $e) { |
78
|
|
|
@trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', \get_class($e), RestrictedDataProviderInterface::class), E_USER_DEPRECATED); |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|