1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Xervice\Core\Locator\Dynamic; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Core\Locator\Dynamic\ServiceNotParseable; |
9
|
|
|
use Xervice\Core\Client\ClientInterface; |
10
|
|
|
use Xervice\Core\Dependency\DependencyProviderInterface; |
11
|
|
|
use Xervice\Core\Facade\FacadeInterface; |
12
|
|
|
use Xervice\Core\Factory\FactoryInterface; |
13
|
|
|
use Xervice\Core\Locator\Locator; |
14
|
|
|
use Xervice\Core\Locator\Proxy\ProxyInterface; |
15
|
|
|
|
16
|
|
|
trait DynamicLocator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return \Xervice\Core\Factory\FactoryInterface |
20
|
|
|
* @throws \Core\Locator\Dynamic\ServiceNotParseable |
21
|
|
|
*/ |
22
|
1 |
|
public function getFactory(): FactoryInterface |
23
|
|
|
{ |
24
|
1 |
|
return $this->getLocator()->factory(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return \Xervice\Core\Facade\FacadeInterface |
29
|
|
|
* @throws \Core\Locator\Dynamic\ServiceNotParseable |
30
|
|
|
*/ |
31
|
4 |
|
public function getFacade(): FacadeInterface |
32
|
|
|
{ |
33
|
4 |
|
return $this->getLocator()->facade(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return \Xervice\Core\Client\ClientInterface |
38
|
|
|
* @throws \Core\Locator\Dynamic\ServiceNotParseable |
39
|
|
|
*/ |
40
|
1 |
|
public function getClient(): ClientInterface |
41
|
|
|
{ |
42
|
1 |
|
return $this->getLocator()->client(); |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function getContainer(): DependencyProviderInterface |
46
|
|
|
{ |
47
|
2 |
|
return $this->getLocator()->container(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return \Xervice\Core\Locator\Proxy\ProxyInterface |
52
|
|
|
* @throws \Core\Locator\Dynamic\ServiceNotParseable |
53
|
|
|
*/ |
54
|
4 |
|
private function getLocator(): ProxyInterface |
55
|
|
|
{ |
56
|
4 |
|
return Locator::getInstance()->{$this->getServiceName()}(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
* @throws \Core\Locator\Dynamic\ServiceNotParseable |
62
|
|
|
*/ |
63
|
4 |
|
private function getServiceName(): string |
64
|
|
|
{ |
65
|
4 |
|
if (!preg_match('@([A-Za-z]+)\\\\([A-Za-z]+)\\\\([A-Za-z\\\\]+)@', \get_class($this), $matches)) { |
66
|
|
|
throw new ServiceNotParseable(__NAMESPACE__); |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
return $matches[2]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|