1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Orm\Provider; |
15
|
|
|
|
16
|
|
|
use PDO; |
17
|
|
|
use Valkyrja\Application\Env; |
|
|
|
|
18
|
|
|
use Valkyrja\Container\Contract\Container; |
19
|
|
|
use Valkyrja\Container\Support\Provider; |
20
|
|
|
use Valkyrja\Orm\Contract\Manager; |
21
|
|
|
use Valkyrja\Orm\Entity\Contract\Entity; |
22
|
|
|
use Valkyrja\Orm\InMemoryManager; |
23
|
|
|
use Valkyrja\Orm\MysqlManager; |
24
|
|
|
use Valkyrja\Orm\NullManager; |
25
|
|
|
use Valkyrja\Orm\PgsqlManager; |
26
|
|
|
use Valkyrja\Orm\Repository\Repository; |
27
|
|
|
use Valkyrja\Orm\SqliteManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class ServiceProvider. |
31
|
|
|
* |
32
|
|
|
* @author Melech Mizrachi |
33
|
|
|
*/ |
34
|
|
|
final class ServiceProvider extends Provider |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public static function publishers(): array |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
Manager::class => [self::class, 'publishManager'], |
43
|
|
|
MysqlManager::class => [self::class, 'publishMysqlManager'], |
44
|
|
|
PgsqlManager::class => [self::class, 'publishPgsqlManager'], |
45
|
|
|
SqliteManager::class => [self::class, 'publishSqliteManager'], |
46
|
|
|
InMemoryManager::class => [self::class, 'publishInMemoryManager'], |
47
|
|
|
NullManager::class => [self::class, 'publishNullManager'], |
48
|
|
|
Repository::class => [self::class, 'publishRepository'], |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
|
|
public static function provides(): array |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
Manager::class, |
59
|
|
|
MysqlManager::class, |
60
|
|
|
PgsqlManager::class, |
61
|
|
|
SqliteManager::class, |
62
|
|
|
InMemoryManager::class, |
63
|
|
|
NullManager::class, |
64
|
|
|
Repository::class, |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Publish the manager service. |
70
|
|
|
*/ |
71
|
|
|
public static function publishManager(Container $container): void |
72
|
|
|
{ |
73
|
|
|
$env = $container->getSingleton(Env::class); |
74
|
|
|
/** @var class-string<Manager> $default */ |
75
|
|
|
$default = $env::ORM_DEFAULT_MANAGER; |
76
|
|
|
|
77
|
|
|
$container->setSingleton( |
78
|
|
|
Manager::class, |
79
|
|
|
$container->getSingleton($default), |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Publish the mysql manager service. |
85
|
|
|
*/ |
86
|
|
|
public static function publishMysqlManager(Container $container): void |
87
|
|
|
{ |
88
|
|
|
$env = $container->getSingleton(Env::class); |
89
|
|
|
/** @var non-empty-string $db */ |
90
|
|
|
$db = $env::ORM_MYSQL_DB; |
91
|
|
|
/** @var non-empty-string $host */ |
92
|
|
|
$host = $env::ORM_MYSQL_HOST; |
93
|
|
|
/** @var positive-int $port */ |
94
|
|
|
$port = $env::ORM_MYSQL_PORT; |
95
|
|
|
/** @var non-empty-string $user */ |
96
|
|
|
$user = $env::ORM_MYSQL_USER; |
97
|
|
|
/** @var non-empty-string $password */ |
98
|
|
|
$password = $env::ORM_MYSQL_PASSWORD; |
99
|
|
|
/** @var non-empty-string $charset */ |
100
|
|
|
$charset = $env::ORM_MYSQL_CHARSET; |
101
|
|
|
/** @var non-empty-string|null $strict */ |
102
|
|
|
$strict = $env::ORM_MYSQL_STRICT; |
103
|
|
|
/** @var non-empty-string|null $engine */ |
104
|
|
|
$engine = $env::ORM_MYSQL_ENGINE; |
105
|
|
|
/** @var array<int, int|bool>|null $options */ |
106
|
|
|
$options = $env::ORM_MYSQL_OPTIONS; |
107
|
|
|
|
108
|
|
|
$options ??= [ |
109
|
|
|
PDO::ATTR_CASE => PDO::CASE_NATURAL, |
110
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
111
|
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
112
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
113
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$dsn = 'mysql' |
117
|
|
|
. ":dbname=$db}" |
118
|
|
|
. ";host=$host" |
119
|
|
|
. ";port=$port" |
120
|
|
|
. ";user=$user" |
121
|
|
|
. ";password=$password" |
122
|
|
|
. ";charset=$charset" |
123
|
|
|
. ($strict !== null ? ";strict=$strict" : '') |
124
|
|
|
. ($engine !== null ? ";engine=$engine" : ''); |
125
|
|
|
|
126
|
|
|
$pdo = new PDO( |
127
|
|
|
dsn: $dsn, |
128
|
|
|
options: $options |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$container->setSingleton( |
132
|
|
|
MysqlManager::class, |
133
|
|
|
new MysqlManager($pdo, $container) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Publish the pgsql manager service. |
139
|
|
|
*/ |
140
|
|
|
public static function publishPgsqlManager(Container $container): void |
141
|
|
|
{ |
142
|
|
|
$env = $container->getSingleton(Env::class); |
143
|
|
|
/** @var non-empty-string $db */ |
144
|
|
|
$db = $env::ORM_PGSQL_DB; |
145
|
|
|
/** @var non-empty-string $host */ |
146
|
|
|
$host = $env::ORM_PGSQL_HOST; |
147
|
|
|
/** @var positive-int $port */ |
148
|
|
|
$port = $env::ORM_PGSQL_PORT; |
149
|
|
|
/** @var non-empty-string $user */ |
150
|
|
|
$user = $env::ORM_PGSQL_USER; |
151
|
|
|
/** @var non-empty-string $password */ |
152
|
|
|
$password = $env::ORM_PGSQL_PASSWORD; |
153
|
|
|
/** @var non-empty-string $charset */ |
154
|
|
|
$charset = $env::ORM_PGSQL_CHARSET; |
155
|
|
|
/** @var non-empty-string $schema */ |
156
|
|
|
$schema = $env::ORM_PGSQL_SCHEMA; |
157
|
|
|
/** @var non-empty-string $sslmode */ |
158
|
|
|
$sslmode = $env::ORM_PGSQL_SSL_MODE; |
159
|
|
|
/** @var array<int, int|bool>|null $options */ |
160
|
|
|
$options = $env::ORM_PGSQL_OPTIONS; |
161
|
|
|
|
162
|
|
|
$options ??= [ |
163
|
|
|
PDO::ATTR_PERSISTENT => true, |
164
|
|
|
PDO::ATTR_CASE => PDO::CASE_NATURAL, |
165
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
166
|
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
167
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
168
|
|
|
]; |
169
|
|
|
|
170
|
|
|
$dsn = 'pgsql' |
171
|
|
|
. ":dbname=$db}" |
172
|
|
|
. ";host=$host" |
173
|
|
|
. ";port=$port" |
174
|
|
|
. ";user=$user" |
175
|
|
|
. ";password=$password" |
176
|
|
|
. ";sslmode=$sslmode" |
177
|
|
|
. ";options='--client_encoding=$charset"; |
178
|
|
|
|
179
|
|
|
$pdo = new PDO( |
180
|
|
|
dsn: $dsn, |
181
|
|
|
options: $options |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$pdo->query("set search_path to $schema"); |
185
|
|
|
|
186
|
|
|
$container->setSingleton( |
187
|
|
|
PgsqlManager::class, |
188
|
|
|
new PgsqlManager($pdo, $container) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Publish the sqlite manager service. |
194
|
|
|
*/ |
195
|
|
|
public static function publishSqliteManager(Container $container): void |
196
|
|
|
{ |
197
|
|
|
$env = $container->getSingleton(Env::class); |
198
|
|
|
/** @var non-empty-string $db */ |
199
|
|
|
$db = $env::ORM_SQLITE_DB; |
200
|
|
|
/** @var non-empty-string $host */ |
201
|
|
|
$host = $env::ORM_SQLITE_HOST; |
202
|
|
|
/** @var positive-int $port */ |
203
|
|
|
$port = $env::ORM_SQLITE_PORT; |
204
|
|
|
/** @var non-empty-string $user */ |
205
|
|
|
$user = $env::ORM_SQLITE_USER; |
206
|
|
|
/** @var non-empty-string $password */ |
207
|
|
|
$password = $env::ORM_SQLITE_PASSWORD; |
208
|
|
|
/** @var non-empty-string $charset */ |
209
|
|
|
$charset = $env::ORM_SQLITE_CHARSET; |
210
|
|
|
/** @var array<int, int|bool>|null $options */ |
211
|
|
|
$options = $env::ORM_SQLITE_OPTIONS; |
212
|
|
|
|
213
|
|
|
$options ??= [ |
214
|
|
|
PDO::ATTR_CASE => PDO::CASE_NATURAL, |
215
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
216
|
|
|
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
217
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
218
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
219
|
|
|
]; |
220
|
|
|
|
221
|
|
|
$dsn = 'sqlite' |
222
|
|
|
. ":dbname=$db}" |
223
|
|
|
. ";host=$host" |
224
|
|
|
. ";port=$port" |
225
|
|
|
. ";user=$user" |
226
|
|
|
. ";charset=$charset" |
227
|
|
|
. ";password=$password"; |
228
|
|
|
|
229
|
|
|
$pdo = new PDO( |
230
|
|
|
dsn: $dsn, |
231
|
|
|
options: $options |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$container->setSingleton( |
235
|
|
|
SqliteManager::class, |
236
|
|
|
new SqliteManager($pdo, $container) |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Publish the in memory manager service. |
242
|
|
|
*/ |
243
|
|
|
public static function publishInMemoryManager(Container $container): void |
244
|
|
|
{ |
245
|
|
|
$container->setSingleton( |
246
|
|
|
InMemoryManager::class, |
247
|
|
|
new InMemoryManager() |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Publish the null manager service. |
253
|
|
|
*/ |
254
|
|
|
public static function publishNullManager(Container $container): void |
255
|
|
|
{ |
256
|
|
|
$container->setSingleton( |
257
|
|
|
NullManager::class, |
258
|
|
|
new NullManager() |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Publish the repository service. |
264
|
|
|
*/ |
265
|
|
|
public static function publishRepository(Container $container): void |
266
|
|
|
{ |
267
|
|
|
$container->setCallable( |
268
|
|
|
Repository::class, |
269
|
|
|
[self::class, 'createRepository'], |
270
|
|
|
); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Create a repository service. |
275
|
|
|
* |
276
|
|
|
* @param class-string<Entity> $entity The entity |
|
|
|
|
277
|
|
|
*/ |
278
|
|
|
public static function createRepository(Container $container, Manager $manager, string $entity): Repository |
279
|
|
|
{ |
280
|
|
|
return new Repository( |
281
|
|
|
manager: $manager, |
282
|
|
|
entity: $entity |
283
|
|
|
); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths