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\DataPersister; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\DataPersister\ChainDataPersister; |
17
|
|
|
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface; |
18
|
|
|
use ApiPlatform\Core\DataPersister\DataPersisterInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Anthony GRASSIOT <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class TraceableChainDataPersister implements ContextAwareDataPersisterInterface |
24
|
|
|
{ |
25
|
|
|
private $persisters = []; |
26
|
|
|
private $persistersResponse = []; |
27
|
|
|
private $decorated; |
28
|
|
|
|
29
|
|
|
public function __construct(DataPersisterInterface $dataPersister) |
30
|
|
|
{ |
31
|
|
|
if ($dataPersister instanceof ChainDataPersister) { |
32
|
|
|
$this->decorated = $dataPersister; |
33
|
|
|
$this->persisters = $dataPersister->persisters; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getPersistersResponse(): array |
38
|
|
|
{ |
39
|
|
|
return $this->persistersResponse; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function supports($data, array $context = []): bool |
46
|
|
|
{ |
47
|
|
|
return $this->decorated->supports($data, $context); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function persist($data, array $context = []) |
54
|
|
|
{ |
55
|
|
|
if ($match = $this->tracePersisters($data, $context)) { |
56
|
|
|
return $match->persist($data, $context) ?? $data; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function remove($data, array $context = []) |
64
|
|
|
{ |
65
|
|
|
if ($match = $this->tracePersisters($data, $context)) { |
66
|
|
|
return $match->remove($data, $context); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function removeElementFromCollection($data, array $context = []) |
74
|
|
|
{ |
75
|
|
|
if ($match = $this->tracePersisters($data, $context)) { |
76
|
|
|
return $match->removeElementFromCollection($data, $context); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function tracePersisters($data, array $context = []) |
81
|
|
|
{ |
82
|
|
|
$match = null; |
83
|
|
|
foreach ($this->persisters as $persister) { |
84
|
|
|
$this->persistersResponse[\get_class($persister)] = $match ? null : false; |
85
|
|
|
if (!$match && $persister->supports($data, $context)) { |
86
|
|
|
$match = $persister; |
87
|
|
|
$this->persistersResponse[\get_class($persister)] = true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $match; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|