|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Borobudur\EventSourcing\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Borobudur\EventSourcing\Exception\InvalidArgumentException; |
|
6
|
|
|
use Borobudur\EventSourcing\Exception\RuntimeException; |
|
7
|
|
|
use Borobudur\EventSourcing\Snapshot\SnapshotInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Iqbal Maulana <[email protected]> |
|
11
|
|
|
* @created 8/22/16 |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractStorageManager |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var StorageInterface|SnapshotInterface[] |
|
17
|
|
|
*/ |
|
18
|
|
|
protected static $instances = array(); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Build or get an engine if already built. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $engine |
|
24
|
|
|
* @param string $database |
|
25
|
|
|
* @param string|null $host |
|
26
|
|
|
* @param string|null $user |
|
27
|
|
|
* @param string|null $pass |
|
28
|
|
|
* @param int|null $port |
|
29
|
|
|
* |
|
30
|
|
|
* @return StorageInterface|SnapshotInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
final public static function instance($engine, $database, $host = null, $user = null, $pass = null, $port = null) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!isset(static::$instances[$engine])) { |
|
35
|
|
|
static::buildInstance($engine, $host, $port, $database, $user, $pass); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return static::$instances[$engine]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Build and register the engine. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $engine |
|
45
|
|
|
* @param string $host |
|
46
|
|
|
* @param int $port |
|
47
|
|
|
* @param string $database |
|
48
|
|
|
* @param string $user |
|
49
|
|
|
* @param string $pass |
|
50
|
|
|
* |
|
51
|
|
|
* @throws InvalidArgumentException |
|
52
|
|
|
*/ |
|
53
|
|
|
protected static function buildInstance($engine, $host, $port, $database, $user, $pass) |
|
54
|
|
|
{ |
|
55
|
|
|
if (in_array($engine, static::supportEngines())) { |
|
56
|
|
|
static::$instances[$engine] = static::buildEngine($engine, $host, $port, $database, $user, $pass); |
|
57
|
|
|
} else { |
|
58
|
|
|
throw new InvalidArgumentException(sprintf('Storage engine "%s" not found.', $engine)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected static function supportEngines() |
|
66
|
|
|
{ |
|
67
|
|
|
return array(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $engine |
|
72
|
|
|
* @param string $host |
|
73
|
|
|
* @param int $port |
|
74
|
|
|
* @param string $database |
|
75
|
|
|
* @param string $user |
|
76
|
|
|
* @param string $pass |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
protected static function buildEngine($engine, $host, $port, $database, $user, $pass) |
|
81
|
|
|
{ |
|
82
|
|
|
throw new RuntimeException('There are no implement of build engine.'); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|