-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDriverRegistry.php
141 lines (123 loc) · 3.32 KB
/
DriverRegistry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Aternos\Model\Driver;
use Aternos\Model\Driver\Cassandra\Cassandra;
use Aternos\Model\Driver\Elasticsearch\Elasticsearch;
use Aternos\Model\Driver\Mysqli\Mysqli;
use Aternos\Model\Driver\Redis\Redis;
use Aternos\Model\Driver\Test\TestDriver;
use InvalidArgumentException;
/**
* Class DriverRegistry
*
* @package Aternos\Model\Driver
*/
class DriverRegistry implements DriverRegistryInterface
{
/**
* @var array|string[]
*/
protected array $classes = [
Cassandra::ID => Cassandra::class,
Elasticsearch::ID => Elasticsearch::class,
Mysqli::ID => Mysqli::class,
Redis::ID => Redis::class,
TestDriver::ID => TestDriver::class
];
/**
* @var array<string, DriverInterface>
*/
protected array $drivers = [];
/**
* Register a driver object by ID
*
* @param DriverInterface $driver
*/
public function registerDriver(DriverInterface $driver)
{
$this->drivers[$driver->getId()] = $driver;
}
/**
* Register a driver class by ID, a driver instance will only be created if necessary
*
* The class has to be preconfigured with all credentials etc., no parameters are passed to constructor
*
* @param string $id
* @param string $class
*/
public function registerDriverClass(string $id, string $class)
{
$this->classes[$id] = $class;
}
/**
* Get a driver by ID, driver instances are preferred, classes will be used if necessary
*
* @param string $id
* @return DriverInterface
*/
public function getDriver(string $id): DriverInterface
{
if (isset($this->drivers[$id])) {
return $this->drivers[$id];
}
if (isset($this->classes[$id])) {
$class = $this->classes[$id];
$driver = new $class();
$this->registerDriver($driver);
return $driver;
}
throw new InvalidArgumentException("Driver with ID '" . $id . "' not found.");
}
/**
* Get the test driver
*
* @return TestDriver
*/
public function getTestDriver(): TestDriver
{
/** @var TestDriver */
return $this->getDriver(TestDriver::ID);
}
/**
* Check if a driver is instance of a class (even if the driver has no instance yet)
*
* @param string $id
* @param string $class
* @return bool
*/
public function isDriverInstanceOf(string $id, string $class): bool
{
if (isset($this->drivers[$id])) {
return is_subclass_of($this->drivers[$id], $class);
}
if (isset($this->classes[$id])) {
return is_subclass_of($this->classes[$id], $class);
}
throw new InvalidArgumentException("Driver with ID '" . $id . "' not found.");
}
/**
* @var DriverRegistry|null
*/
protected static ?DriverRegistry $instance = null;
/**
* @return DriverRegistry
*/
public static function getInstance(): DriverRegistry
{
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Prohibited for singleton
*/
protected function __clone()
{
}
/**
* Prohibited for singleton
*/
protected function __construct()
{
}
}