|
1
|
|
|
<?php |
|
2
|
|
|
namespace PSB\Core\Persistence; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use PSB\Core\Exception\InvalidArgumentException; |
|
6
|
|
|
use PSB\Core\Util\Settings; |
|
7
|
|
|
|
|
8
|
|
|
abstract class PersistenceDefinition |
|
9
|
|
|
{ |
|
10
|
|
|
private $storageTypeToInitializerMap = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Creates the PersistenceConfigurator specific to the transport implementation. |
|
14
|
|
|
* |
|
15
|
|
|
* @param Settings $settings |
|
16
|
|
|
* |
|
17
|
|
|
* @return PersistenceConfigurator |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract public function createConfigurator(Settings $settings); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This is where subclasses declare what storage types they support together with |
|
23
|
|
|
* the initializers for those types. |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract public function formalize(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param StorageType $storageType |
|
29
|
|
|
* @param callable $storageTypeInitializer |
|
30
|
|
|
*/ |
|
31
|
7 |
|
protected function supports(StorageType $storageType, callable $storageTypeInitializer) |
|
32
|
|
|
{ |
|
33
|
7 |
|
if (isset($this->storageTypeToInitializerMap[$storageType->getValue()])) { |
|
34
|
1 |
|
throw new InvalidArgumentException( |
|
35
|
1 |
|
"Storage type initializer for type '{$storageType->getValue()}' is already defined." |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
7 |
|
$this->storageTypeToInitializerMap[$storageType->getValue()] = $storageTypeInitializer; |
|
40
|
7 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param StorageType $storageType |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function hasSupportFor(StorageType $storageType) |
|
48
|
|
|
{ |
|
49
|
5 |
|
return isset($this->storageTypeToInitializerMap[$storageType->getValue()]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param StorageType $storageType |
|
54
|
|
|
* @param Settings $settings |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function applyFor(StorageType $storageType, Settings $settings) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$storageTypeInitializer = $this->storageTypeToInitializerMap[$storageType->getValue()]; |
|
59
|
2 |
|
$storageTypeInitializer($settings); |
|
60
|
2 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param StorageType|null $storageType |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function getSupportedStorages(StorageType $storageType = null) |
|
68
|
|
|
{ |
|
69
|
3 |
|
if ($storageType) { |
|
70
|
2 |
|
return [$storageType->getValue()]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
return array_keys($this->storageTypeToInitializerMap); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|