1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace W2w\Laravel\Apie\Plugins\Illuminate\ResourceFactories; |
5
|
|
|
|
6
|
|
|
use Illuminate\Container\Container; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use W2w\Laravel\Apie\Services\ApieContext; |
9
|
|
|
use W2w\Lib\Apie\Annotations\ApiResource; |
10
|
|
|
use W2w\Lib\Apie\Exceptions\InvalidClassTypeException; |
11
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceFactoryInterface; |
12
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourcePersisterInterface; |
13
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceRetrieverInterface; |
14
|
|
|
|
15
|
|
|
class FromIlluminateContainerFactory implements ApiResourceFactoryInterface |
16
|
|
|
{ |
17
|
|
|
private $container; |
18
|
|
|
|
19
|
|
|
public function __construct(Container $container) |
20
|
|
|
{ |
21
|
|
|
$this->container = $container; |
22
|
|
|
} |
23
|
|
|
/** |
24
|
|
|
* Returns true if this factory can create this identifier. |
25
|
|
|
* |
26
|
|
|
* @param string $identifier |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
public function hasApiResourceRetrieverInstance(string $identifier): bool |
30
|
|
|
{ |
31
|
|
|
return $this->validIdentifier($identifier); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Gets an instance of ApiResourceRetrieverInstance |
36
|
|
|
* @param string $identifier |
37
|
|
|
* @return ApiResourceRetrieverInterface |
38
|
|
|
*/ |
39
|
|
|
public function getApiResourceRetrieverInstance(string $identifier): ApiResourceRetrieverInterface |
40
|
|
|
{ |
41
|
|
|
$result = $this->container->make($identifier); |
42
|
|
|
if (!($result instanceof ApiResourceRetrieverInterface)) { |
43
|
|
|
throw new InvalidClassTypeException($identifier, 'ApiResourceRetrieverInterface'); |
44
|
|
|
} |
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns true if this factory can create this identifier. |
50
|
|
|
* |
51
|
|
|
* @param string $identifier |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function hasApiResourcePersisterInstance(string $identifier): bool |
55
|
|
|
{ |
56
|
|
|
return $this->validIdentifier($identifier); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Gets an instance of ApiResourceRetrieverInstance |
61
|
|
|
* @param string $identifier |
62
|
|
|
* @return ApiResourcePersisterInterface |
63
|
|
|
*/ |
64
|
|
|
public function getApiResourcePersisterInstance(string $identifier): ApiResourcePersisterInterface |
65
|
|
|
{ |
66
|
|
|
$result = $this->container->make($identifier); |
67
|
|
|
if (!($result instanceof ApiResourcePersisterInterface)) { |
68
|
|
|
throw new InvalidClassTypeException($identifier, 'ApiResourcePersisterInterface'); |
69
|
|
|
} |
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function validIdentifier(string $identifier): bool |
74
|
|
|
{ |
75
|
|
|
if ($this->container->has($identifier) || $this->container->bound($identifier)) { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
if ($this->container->bound(ApieContext::class)) { |
79
|
|
|
$context = $this->container->make(ApieContext::class); |
80
|
|
|
foreach ($context->allContexts() as $apieContext) { |
81
|
|
|
/** @var ApiResource[] $config */ |
82
|
|
|
$config = $apieContext->getConfig('resource-config'); |
83
|
|
|
foreach ($config as $resourceConfigEntry) { |
84
|
|
|
if ($identifier === $resourceConfigEntry->retrieveClass) { |
85
|
|
|
$this->container->singleton($identifier); |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
if ($identifier === $resourceConfigEntry->persistClass) { |
89
|
|
|
$this->container->singleton($identifier); |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return Str::startsWith($identifier, 'W2w\Laravel\Apie\Plugins\Illuminate\DataLayers\\'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|