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