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\DataProvider; |
||
15 | |||
16 | use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; |
||
17 | |||
18 | /** |
||
19 | * Tries each configured data provider and returns the result of the first able to handle the resource class. |
||
20 | * |
||
21 | * @author Antoine Bluchet <[email protected]> |
||
22 | */ |
||
23 | final class ChainSubresourceDataProvider implements SubresourceDataProviderInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var iterable<SubresourceDataProviderInterface> |
||
27 | * |
||
28 | * @internal |
||
29 | */ |
||
30 | public $dataProviders; |
||
31 | |||
32 | /** |
||
33 | * @param SubresourceDataProviderInterface[] $dataProviders |
||
34 | */ |
||
35 | public function __construct(iterable $dataProviders) |
||
36 | { |
||
37 | $this->dataProviders = $dataProviders; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null) |
||
44 | { |
||
45 | foreach ($this->dataProviders as $dataProvider) { |
||
46 | try { |
||
47 | if ($dataProvider instanceof RestrictedDataProviderInterface && !$dataProvider->supports($resourceClass, $operationName, $context)) { |
||
48 | continue; |
||
49 | } |
||
50 | |||
51 | return $dataProvider->getSubresource($resourceClass, $identifiers, $context, $operationName); |
||
52 | } catch (ResourceClassNotSupportedException $e) { |
||
53 | @trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), E_USER_DEPRECATED); |
||
54 | continue; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | return ($context['collection'] ?? false) ? [] : null; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
59 | } |
||
60 | } |
||
61 |