|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Clubdeuce\Tessitura\Base; |
|
4
|
|
|
|
|
5
|
|
|
use Clubdeuce\Tessitura\Helpers; |
|
6
|
|
|
use Clubdeuce\Tessitura\Interfaces\ApiInterface; |
|
7
|
|
|
use Clubdeuce\Tessitura\Interfaces\ResourceInterface; |
|
8
|
|
|
use Clubdeuce\Tessitura\Resources; |
|
9
|
|
|
use GuzzleHttp\Client; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Psr\Log\NullLogger; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Container |
|
15
|
|
|
* @package Clubdeuce\Tessitura |
|
16
|
|
|
* |
|
17
|
|
|
* A simple service container for managing dependencies. |
|
18
|
|
|
* |
|
19
|
|
|
* This class provides a lightweight dependency injection container. |
|
20
|
|
|
* It allows you to store and retrieve services and configuration parameters, |
|
21
|
|
|
* making it easier to manage dependencies and configuration throughout your application. |
|
22
|
|
|
* |
|
23
|
|
|
* Example usage: |
|
24
|
|
|
* <code> |
|
25
|
|
|
* $container = new Container(['db_host' => 'localhost']); |
|
26
|
|
|
* $container->set('logger', new Logger()); |
|
27
|
|
|
* $logger = $container->get('logger'); |
|
28
|
|
|
* $dbHost = $container->getParameter('db_host'); |
|
29
|
|
|
* </code> |
|
30
|
|
|
*/ |
|
31
|
|
|
class Container |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var mixed[] Array of registered services. |
|
35
|
|
|
*/ |
|
36
|
|
|
private array $services = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var mixed[] Array of configuration parameters. |
|
40
|
|
|
*/ |
|
41
|
|
|
private array $parameters; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Container constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed[] $parameters Configuration parameters |
|
47
|
|
|
*/ |
|
48
|
17 |
|
public function __construct(array $parameters = []) |
|
49
|
|
|
{ |
|
50
|
17 |
|
$this->parameters = $parameters; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get a service from the container. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $id The service ID |
|
57
|
|
|
* @return mixed The service instance |
|
58
|
|
|
* @throws \Exception If the service is not found |
|
59
|
|
|
*/ |
|
60
|
12 |
|
public function get(string $id): mixed |
|
61
|
|
|
{ |
|
62
|
12 |
|
if (!isset($this->services[$id])) { |
|
63
|
9 |
|
$this->services[$id] = $this->createService($id); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
10 |
|
return $this->services[$id]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set a service in the container. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $id The service ID |
|
73
|
|
|
* @param mixed $service The service instance |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
5 |
|
public function set(string $id, mixed $service): void |
|
77
|
|
|
{ |
|
78
|
5 |
|
$this->services[$id] = $service; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get a parameter from the container. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $name The parameter name |
|
85
|
|
|
* @param mixed $default The default value if the parameter is not found |
|
86
|
|
|
* @return mixed The parameter value |
|
87
|
|
|
*/ |
|
88
|
11 |
|
public function getParameter(string $name, mixed $default = null): mixed |
|
89
|
|
|
{ |
|
90
|
11 |
|
return $this->parameters[$name] ?? $default; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set a parameter in the container. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $name The parameter name |
|
97
|
|
|
* @param mixed $value The parameter value |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function setParameter(string $name, mixed $value): void |
|
101
|
|
|
{ |
|
102
|
2 |
|
$this->parameters[$name] = $value; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Check if a service exists in the container. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $id The service ID |
|
109
|
|
|
* @return bool True if the service exists, false otherwise |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function has(string $id): bool |
|
112
|
|
|
{ |
|
113
|
1 |
|
return isset($this->services[$id]) || method_exists($this, 'create' . ucfirst($id)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Create a service. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $id The service ID |
|
120
|
|
|
* @return mixed The service instance |
|
121
|
|
|
* @throws \Exception If the service cannot be created |
|
122
|
|
|
*/ |
|
123
|
9 |
|
private function createService(string $id): mixed |
|
124
|
|
|
{ |
|
125
|
|
|
switch ($id) { |
|
126
|
9 |
|
case 'http_client': |
|
127
|
4 |
|
return $this->createHttpClient(); |
|
128
|
5 |
|
case 'logger': |
|
129
|
1 |
|
return $this->createLogger(); |
|
130
|
4 |
|
case 'api': |
|
131
|
1 |
|
return $this->createApi(); |
|
132
|
3 |
|
case 'performances': |
|
133
|
1 |
|
return $this->createPerformances(); |
|
134
|
|
|
default: |
|
135
|
2 |
|
throw new \Exception(sprintf('Service "%s" not found', $id)); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Create an HTTP client. |
|
141
|
|
|
* |
|
142
|
|
|
* @return Client |
|
143
|
|
|
*/ |
|
144
|
4 |
|
private function createHttpClient(): Client |
|
145
|
|
|
{ |
|
146
|
4 |
|
$baseRoute = $this->getParameter('base_route', ''); |
|
147
|
|
|
|
|
148
|
4 |
|
return new Client([ |
|
149
|
4 |
|
'baseRoute' => $baseRoute, |
|
150
|
4 |
|
'timeout' => $this->getParameter('timeout', 10.0), |
|
151
|
4 |
|
]); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Create a logger. |
|
156
|
|
|
* |
|
157
|
|
|
* @return LoggerInterface |
|
158
|
|
|
*/ |
|
159
|
1 |
|
private function createLogger(): LoggerInterface |
|
160
|
|
|
{ |
|
161
|
|
|
// Use a custom logger if provided, otherwise use NullLogger |
|
162
|
1 |
|
return $this->getParameter('logger', new NullLogger()); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Create an API client. |
|
167
|
|
|
* |
|
168
|
|
|
* @return ApiInterface |
|
169
|
|
|
*/ |
|
170
|
1 |
|
private function createApi(): ApiInterface |
|
171
|
|
|
{ |
|
172
|
1 |
|
$args = [ |
|
173
|
1 |
|
'base_route' => $this->getParameter('base_route', ''), |
|
174
|
1 |
|
'machine' => $this->getParameter('machine', ''), |
|
175
|
1 |
|
'password' => $this->getParameter('password', ''), |
|
176
|
1 |
|
'usergroup' => $this->getParameter('usergroup', ''), |
|
177
|
1 |
|
'username' => $this->getParameter('username', ''), |
|
178
|
1 |
|
'version' => $this->getParameter('version', '16'), |
|
179
|
1 |
|
]; |
|
180
|
|
|
|
|
181
|
1 |
|
return new Helpers\Api( |
|
182
|
1 |
|
$args, |
|
183
|
1 |
|
$this->get('http_client'), |
|
184
|
1 |
|
$this->get('logger'), |
|
185
|
1 |
|
$this->has('cache') ? $this->get('cache') : null |
|
186
|
1 |
|
); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Create a Performances resource. |
|
191
|
|
|
* |
|
192
|
|
|
* @return ResourceInterface |
|
193
|
|
|
*/ |
|
194
|
1 |
|
private function createPerformances(): ResourceInterface |
|
195
|
|
|
{ |
|
196
|
1 |
|
return new Resources\Performances($this->get('api')); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|