1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Xervice\Core\Business\Model\Locator\Proxy; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Xervice\Core\Business\Model\Config\AbstractConfig; |
8
|
|
|
use Xervice\Core\Business\Model\Config\ConfigInterface; |
9
|
|
|
use Xervice\Core\Business\Model\Dependency\AbstractDependencyContainer; |
10
|
|
|
use Xervice\Core\Business\Model\Dependency\DependencyContainerInterface; |
11
|
|
|
|
12
|
|
|
class AbstractLocatorProxy implements LocatorProxyInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $coreNamespaces; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $projectNamespaces; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $serviceName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Xervice\Core\Business\Model\Config\ConfigInterface |
31
|
|
|
*/ |
32
|
|
|
protected $config; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Xervice\Core\Business\Model\Dependency\DependencyContainerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $container; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* AbstractLocatorProxy constructor. |
41
|
|
|
* |
42
|
|
|
* @param array $coreNamespaces |
43
|
|
|
* @param array $projectNamespaces |
44
|
|
|
* @param string $serviceName |
45
|
|
|
*/ |
46
|
3 |
|
public function __construct(array $coreNamespaces, array $projectNamespaces, string $serviceName) |
47
|
|
|
{ |
48
|
3 |
|
$this->coreNamespaces = $coreNamespaces; |
49
|
3 |
|
$this->projectNamespaces = $projectNamespaces; |
50
|
3 |
|
$this->serviceName = $serviceName; |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
3 |
|
public function name(): string |
57
|
|
|
{ |
58
|
3 |
|
return $this->serviceName; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return \Xervice\Core\Business\Model\Config\ConfigInterface |
63
|
|
|
*/ |
64
|
2 |
|
protected function config(): ConfigInterface |
65
|
|
|
{ |
66
|
2 |
|
if ($this->config === null) { |
67
|
2 |
|
$class = $this->getServiceClass('Config') ?: AbstractConfig::class; |
68
|
2 |
|
$this->config = new $class(); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
return $this->config; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return \Xervice\Core\Business\Model\Dependency\DependencyContainerInterface |
76
|
|
|
*/ |
77
|
2 |
|
protected function container(): DependencyContainerInterface |
78
|
|
|
{ |
79
|
2 |
|
if ($this->container === null) { |
80
|
2 |
|
$this->container = new AbstractDependencyContainer( |
81
|
2 |
|
$this->config() |
82
|
|
|
); |
83
|
|
|
|
84
|
2 |
|
$provider = $this->getServiceClass('DependencyProvider'); |
85
|
2 |
|
if ($provider) { |
86
|
2 |
|
$this->container->register( |
87
|
2 |
|
new $provider($this->config()) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
return $this->container; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $suffix |
97
|
|
|
* @param string|null $directory |
98
|
|
|
* |
99
|
|
|
* @return string|null |
100
|
|
|
*/ |
101
|
2 |
|
protected function getServiceClass(string $suffix, string $directory = null): ?string |
102
|
|
|
{ |
103
|
2 |
|
return $this->getServiceClassFromNamespaces( |
104
|
2 |
|
$this->projectNamespaces, |
105
|
2 |
|
$suffix, |
106
|
2 |
|
$directory |
107
|
2 |
|
) ?: $this->getServiceClassFromNamespaces( |
108
|
2 |
|
$this->coreNamespaces, |
109
|
2 |
|
$suffix, |
110
|
2 |
|
$directory |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $namespaces |
116
|
|
|
* @param string $suffix |
117
|
|
|
* @param string|null $directory |
118
|
|
|
* |
119
|
|
|
* @return null|string |
120
|
|
|
*/ |
121
|
2 |
|
protected function getServiceClassFromNamespaces( |
122
|
|
|
array $namespaces, |
123
|
|
|
string $suffix, |
124
|
|
|
string $directory = null |
125
|
|
|
): ?string { |
126
|
2 |
|
$serviceClass = null; |
127
|
|
|
|
128
|
2 |
|
foreach ($namespaces as $namespace) { |
129
|
2 |
|
$class = $this->getNamespace($namespace, $suffix, $directory); |
130
|
2 |
|
if (class_exists($class)) { |
131
|
2 |
|
$serviceClass = $class; |
132
|
2 |
|
break; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
2 |
|
return $serviceClass; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $primaryNamespace |
141
|
|
|
* @param string $suffix |
142
|
|
|
* @param string|null $directory |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
2 |
|
protected function getNamespace(string $primaryNamespace, string $suffix, string $directory = null): string |
147
|
|
|
{ |
148
|
2 |
|
return sprintf( |
149
|
2 |
|
'\\%1$s\\%2$s%4$s\\%2$s%3$s', |
150
|
2 |
|
$primaryNamespace, |
151
|
2 |
|
$this->name(), |
152
|
2 |
|
$suffix, |
153
|
2 |
|
$directory ? '\\' . $directory : '' |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
} |