1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Zalas\Toolbox\Cli; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerExceptionInterface; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Zalas\Toolbox\Cli\Command\InstallCommand; |
12
|
|
|
use Zalas\Toolbox\Cli\Command\ListCommand; |
13
|
|
|
use Zalas\Toolbox\Cli\Command\TestCommand; |
14
|
|
|
use Zalas\Toolbox\Cli\ServiceContainer\LazyRunner; |
15
|
|
|
use Zalas\Toolbox\Cli\ServiceContainer\RunnerFactory; |
16
|
|
|
use Zalas\Toolbox\Json\JsonTools; |
17
|
|
|
use Zalas\Toolbox\Runner\Runner; |
18
|
|
|
use Zalas\Toolbox\Tool\Tools; |
19
|
|
|
use Zalas\Toolbox\UseCase\InstallTools; |
20
|
|
|
use Zalas\Toolbox\UseCase\ListTools; |
21
|
|
|
use Zalas\Toolbox\UseCase\TestTools; |
22
|
|
|
|
23
|
|
|
class ServiceContainer implements ContainerInterface |
24
|
|
|
{ |
25
|
|
|
private array $services = [ |
26
|
|
|
InstallCommand::class => 'createInstallCommand', |
27
|
|
|
ListCommand::class => 'createListCommand', |
28
|
|
|
TestCommand::class => 'createTestCommand', |
29
|
|
|
Runner::class => 'createRunner', |
30
|
|
|
InstallTools::class => 'createInstallToolsUseCase', |
31
|
|
|
ListTools::class => 'createListToolsUseCase', |
32
|
|
|
TestTools::class => 'createTestToolsUseCase', |
33
|
|
|
Tools::class => 'createTools', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private array $runtimeServices = [ |
37
|
|
|
InputInterface::class => null, |
38
|
|
|
OutputInterface::class => null, |
39
|
|
|
]; |
40
|
|
|
|
41
|
13 |
|
public function set(string $id, /*object */$service): void |
42
|
|
|
{ |
43
|
13 |
|
if (!\array_key_exists($id, $this->runtimeServices)) { |
44
|
1 |
|
throw new class(\sprintf('The "%s" runtime service is not expected.', $id)) extends RuntimeException implements ContainerExceptionInterface { |
45
|
1 |
|
}; |
46
|
|
|
} |
47
|
|
|
|
48
|
13 |
|
$this->runtimeServices[$id] = $service; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
38 |
|
public function get(string $id) |
55
|
|
|
{ |
56
|
38 |
|
if (isset($this->runtimeServices[$id])) { |
57
|
3 |
|
return $this->runtimeServices[$id]; |
58
|
|
|
} |
59
|
|
|
|
60
|
37 |
|
if (isset($this->services[$id])) { |
61
|
35 |
|
return \call_user_func([$this, $this->services[$id]]); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
throw new class(\sprintf('The "%s" service is not registered in the service container.', $id)) extends RuntimeException implements NotFoundExceptionInterface { |
65
|
2 |
|
}; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
38 |
|
public function has(string $id): bool |
72
|
|
|
{ |
73
|
38 |
|
return isset($this->services[$id]) || isset($this->runtimeServices[$id]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @throws ContainerExceptionInterface |
78
|
|
|
* @throws NotFoundExceptionInterface |
79
|
|
|
*/ |
80
|
13 |
|
private function createInstallCommand(): InstallCommand |
81
|
|
|
{ |
82
|
13 |
|
return new InstallCommand($this->get(InstallTools::class), $this->get(Runner::class)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws ContainerExceptionInterface |
87
|
|
|
* @throws NotFoundExceptionInterface |
88
|
|
|
*/ |
89
|
9 |
|
private function createListCommand(): ListCommand |
90
|
|
|
{ |
91
|
9 |
|
return new ListCommand($this->get(ListTools::class)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @throws ContainerExceptionInterface |
96
|
|
|
* @throws NotFoundExceptionInterface |
97
|
|
|
*/ |
98
|
12 |
|
private function createTestCommand(): TestCommand |
99
|
|
|
{ |
100
|
12 |
|
return new TestCommand($this->get(TestTools::class), $this->get(Runner::class)); |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
private function createRunner(): Runner |
104
|
|
|
{ |
105
|
4 |
|
return new LazyRunner(new RunnerFactory($this)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @throws ContainerExceptionInterface |
110
|
|
|
* @throws NotFoundExceptionInterface |
111
|
|
|
*/ |
112
|
2 |
|
private function createInstallToolsUseCase(): InstallTools |
113
|
|
|
{ |
114
|
2 |
|
return new InstallTools($this->get(Tools::class)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @throws ContainerExceptionInterface |
119
|
|
|
* @throws NotFoundExceptionInterface |
120
|
|
|
*/ |
121
|
2 |
|
private function createListToolsUseCase(): ListTools |
122
|
|
|
{ |
123
|
2 |
|
return new ListTools($this->get(Tools::class)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @throws ContainerExceptionInterface |
128
|
|
|
* @throws NotFoundExceptionInterface |
129
|
|
|
*/ |
130
|
1 |
|
private function createTestToolsUseCase(): TestTools |
131
|
|
|
{ |
132
|
1 |
|
return new TestTools($this->get(Tools::class)); |
133
|
|
|
} |
134
|
|
|
|
135
|
5 |
|
private function createTools(): Tools |
136
|
|
|
{ |
137
|
5 |
|
return new JsonTools(function (): array { |
138
|
2 |
|
return $this->get(InputInterface::class)->getOption('tools'); |
139
|
5 |
|
}); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|