|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace W2w\Lib\Apie\Core\ResourceFactories; |
|
5
|
|
|
|
|
6
|
|
|
use W2w\Lib\Apie\Exceptions\CouldNotConstructApiResourceClassException; |
|
7
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceFactoryInterface; |
|
8
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourcePersisterInterface; |
|
9
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceRetrieverInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Groups multiple resource factories together, |
|
13
|
|
|
*/ |
|
14
|
|
|
class ChainableFactory implements ApiResourceFactoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ApiResourceRetrieverInterface[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $instantiatedRetrievers = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ApiResourcePersisterInterface[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $instantiatedPersisters = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ApiResourceFactoryInterface[] |
|
28
|
|
|
*/ |
|
29
|
|
|
private $factories; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ApiResourceFactoryInterface[] |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(array $factories) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->factories = $factories; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritDoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function hasApiResourceRetrieverInstance(string $identifier): bool |
|
43
|
|
|
{ |
|
44
|
|
|
if (isset($this->instantiatedRetrievers[$identifier])) { |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
foreach ($this->factories as $factory) { |
|
48
|
|
|
if ($factory->hasApiResourceRetrieverInstance($identifier)) { |
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritDoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getApiResourceRetrieverInstance(string $identifier): ApiResourceRetrieverInterface |
|
59
|
|
|
{ |
|
60
|
|
|
if (isset($this->instantiatedRetrievers[$identifier])) { |
|
61
|
|
|
return $this->instantiatedRetrievers[$identifier]; |
|
62
|
|
|
} |
|
63
|
|
|
foreach ($this->factories as $factory) { |
|
64
|
|
|
if ($factory->hasApiResourceRetrieverInstance($identifier)) { |
|
65
|
|
|
$res = $factory->getApiResourceRetrieverInstance($identifier); |
|
66
|
|
|
if (get_class($res) === $identifier && $res instanceof ApiResourcePersisterInterface) { |
|
67
|
|
|
$this->instantiatedPersisters[$identifier] = $res; |
|
68
|
|
|
} |
|
69
|
|
|
return $this->instantiatedRetrievers[$identifier] = $res; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
throw new CouldNotConstructApiResourceClassException($identifier); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritDoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function hasApiResourcePersisterInstance(string $identifier): bool |
|
79
|
|
|
{ |
|
80
|
|
|
if (isset($this->instantiatedPersisters[$identifier])) { |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
foreach ($this->factories as $factory) { |
|
84
|
|
|
if ($factory->hasApiResourcePersisterInstance($identifier)) { |
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritDoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getApiResourcePersisterInstance(string $identifier): ApiResourcePersisterInterface |
|
95
|
|
|
{ |
|
96
|
|
|
if (isset($this->instantiatedPersisters[$identifier])) { |
|
97
|
|
|
return $this->instantiatedPersisters[$identifier]; |
|
98
|
|
|
} |
|
99
|
|
|
foreach ($this->factories as $factory) { |
|
100
|
|
|
if ($factory->hasApiResourcePersisterInstance($identifier)) { |
|
101
|
|
|
$res = $factory->getApiResourcePersisterInstance($identifier); |
|
102
|
|
|
if (get_class($res) === $identifier && $res instanceof ApiResourceRetrieverInterface) { |
|
103
|
|
|
$this->instantiatedRetrievers[$identifier] = $res; |
|
104
|
|
|
} |
|
105
|
|
|
return $this->instantiatedPersisters[$identifier] = $res; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
throw new CouldNotConstructApiResourceClassException($identifier); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|