|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Igni\Storage\Driver\Connection; |
|
6
|
|
|
use Igni\Storage\Driver\ConnectionManager; |
|
7
|
|
|
use Igni\Storage\Exception\StorageException; |
|
8
|
|
|
|
|
9
|
|
|
final class StorageManager |
|
10
|
|
|
{ |
|
11
|
|
|
private static $connections = []; |
|
12
|
|
|
private static $defaultConnection; |
|
13
|
|
|
private static $storage; |
|
14
|
|
|
|
|
15
|
|
|
private function __construct() {} |
|
16
|
|
|
|
|
17
|
|
|
public static function configure() |
|
18
|
|
|
{ |
|
19
|
|
|
self::$storage = $storage; |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public static function addConnection(Connection $connection, string $name = 'default'): void |
|
23
|
|
|
{ |
|
24
|
|
|
ConnectionManager::addConnection($connection, $name); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function hasConnection(string $name): bool |
|
28
|
|
|
{ |
|
29
|
|
|
return ConnectionManager::hasConnection($name); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function hasDefaultConnection(): bool |
|
33
|
|
|
{ |
|
34
|
|
|
return self::$defaultConnection !== null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function getDefaultConnection(): Connection |
|
38
|
|
|
{ |
|
39
|
|
|
if (!self::hasDefaultConnection()) { |
|
40
|
|
|
throw StorageException::forNotRegisteredConnection('default'); |
|
41
|
|
|
} |
|
42
|
|
|
return self::$defaultConnection; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function getConnection(string $name): Connection |
|
46
|
|
|
{ |
|
47
|
|
|
if (!self::hasConnection($name)) { |
|
48
|
|
|
throw StorageException::forNotRegisteredConnection($name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return self::$connections[$name]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function getRepository(string $entity): Repository |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
|
|
} |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
public static function hasRepository(string $entity): bool |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
|
|
62
|
|
|
} |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
public static function addRepository(Repository ...$repositories): void |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function getEntityManager(): EntityManager |
|
70
|
|
|
{ |
|
71
|
|
|
|
|
72
|
|
|
} |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|