|
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\ChainSubresourceDataProvider; |
|
17
|
|
|
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; |
|
18
|
|
|
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface; |
|
19
|
|
|
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Anthony GRASSIOT <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class TraceableChainSubresourceDataProvider implements SubresourceDataProviderInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private $dataProviders = []; |
|
27
|
|
|
private $context = []; |
|
28
|
|
|
private $providersResponse = []; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(SubresourceDataProviderInterface $subresourceDataProvider) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($subresourceDataProvider instanceof ChainSubresourceDataProvider) { |
|
33
|
|
|
$reflection = new \ReflectionProperty(ChainSubresourceDataProvider::class, 'dataProviders'); |
|
34
|
|
|
$reflection->setAccessible(true); |
|
35
|
|
|
$this->dataProviders = $reflection->getValue($subresourceDataProvider); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getProvidersResponse(): array |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->providersResponse; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getContext(): array |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->context; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->context = $context; |
|
52
|
|
|
foreach ($this->dataProviders as $dataProvider) { |
|
53
|
|
|
$this->providersResponse[\get_class($dataProvider)] = null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
foreach ($this->dataProviders as $dataProvider) { |
|
57
|
|
|
try { |
|
58
|
|
|
if ($dataProvider instanceof RestrictedDataProviderInterface && !$dataProvider->supports($resourceClass, $operationName, $context)) { |
|
59
|
|
|
$this->providersResponse[\get_class($dataProvider)] = false; |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
$this->providersResponse[\get_class($dataProvider)] = true; |
|
63
|
|
|
|
|
64
|
|
|
return $dataProvider->getSubresource($resourceClass, $identifiers, $context, $operationName); |
|
65
|
|
|
} catch (ResourceClassNotSupportedException $e) { |
|
66
|
|
|
@trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), E_USER_DEPRECATED); |
|
67
|
|
|
$this->providersResponse[\get_class($dataProvider)] = false; |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return ($context['collection'] ?? false) ? [] : null; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|