|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Xervice\Kernel\Business\Model\Service; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
use Xervice\Kernel\Business\Plugin\BootInterface; |
|
9
|
|
|
use Xervice\Kernel\Business\Plugin\ClearServiceInterface; |
|
10
|
|
|
use Xervice\Kernel\Business\Plugin\ExecuteInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ServiceProvider implements ServiceProviderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var \Xervice\Kernel\Business\Model\Service\ServiceInterface[] |
|
16
|
|
|
*/ |
|
17
|
|
|
private $serviceCollection; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ServiceProvider constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param array $serviceCollection |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public function __construct(array $serviceCollection) |
|
25
|
|
|
{ |
|
26
|
1 |
|
$this->serviceCollection = []; |
|
27
|
|
|
|
|
28
|
1 |
|
foreach ($serviceCollection as $name => $service) { |
|
29
|
1 |
|
$this->set($name, new $service()); |
|
30
|
|
|
} |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function boot(): void |
|
34
|
|
|
{ |
|
35
|
1 |
|
foreach ($this->serviceCollection as $service) { |
|
36
|
1 |
|
if ($service instanceof BootInterface) { |
|
37
|
1 |
|
$service->boot($this); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function execute(): void |
|
43
|
|
|
{ |
|
44
|
1 |
|
foreach ($this->serviceCollection as $service) { |
|
45
|
1 |
|
if ($service instanceof ExecuteInterface) { |
|
46
|
1 |
|
$service->execute($this); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $serviceName |
|
53
|
|
|
* |
|
54
|
|
|
* @return null|\Xervice\Kernel\Business\Plugin\ClearServiceInterface |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function get(string $serviceName): ?ClearServiceInterface |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->serviceCollection[$serviceName] ?? null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $serviceName |
|
63
|
|
|
* @param \Xervice\Kernel\Business\Plugin\ClearServiceInterface $service |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function set(string $serviceName, ClearServiceInterface $service): void |
|
66
|
|
|
{ |
|
67
|
1 |
|
$this->serviceCollection[$serviceName] = $service; |
|
68
|
|
|
} |
|
69
|
|
|
} |