| 1 |
<?php |
| 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 |
namespace Liuch\DmarcSrg\Database; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Exception\LogicException; |
| 35 |
|
| 36 |
abstract class DatabaseConnector |
| 37 |
{ |
| 38 |
protected static $names = [ |
| 39 |
'domain' => 'DomainMapper', |
| 40 |
'host' => 'HostMapper', |
| 41 |
'report' => 'ReportMapper', |
| 42 |
'report-log' => 'ReportLogMapper', |
| 43 |
'setting' => 'SettingMapper', |
| 44 |
'statistics' => 'StatisticsMapper', |
| 45 |
'upgrader' => 'UpgraderMapper', |
| 46 |
'user' => 'UserMapper' |
| 47 |
]; |
| 48 |
|
| 49 |
protected $host = null; |
| 50 |
protected $name = null; |
| 51 |
protected $user = null; |
| 52 |
protected $password = null; |
| 53 |
protected $prefix = ''; |
| 54 |
protected $mappers = []; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function __construct(array $conf) |
| 62 |
{ |
| 63 |
$this->host = $conf['host'] ?? ''; |
| 64 |
$this->name = $conf['name'] ?? ''; |
| 65 |
$this->user = $conf['user'] ?? ''; |
| 66 |
$this->password = $conf['password'] ?? ''; |
| 67 |
$this->prefix = $conf['table_prefix'] ?? ''; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
abstract public function dbh(): object; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
abstract public function state(): array; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function getMapper(string $name): object |
| 92 |
{ |
| 93 |
if (isset($this->mappers[$name])) { |
| 94 |
return $this->mappers[$name]; |
| 95 |
} |
| 96 |
|
| 97 |
if (!isset(self::$names[$name])) { |
| 98 |
throw new LogicException('Unknown mapper name: ' . $name); |
| 99 |
} |
| 100 |
|
| 101 |
$reflection = new \ReflectionClass($this); |
| 102 |
$mapper_name = $reflection->getNamespaceName() . '\\' . self::$names[$name]; |
| 103 |
if (!class_exists($mapper_name)) { |
| 104 |
$reflection = $reflection->getParentClass(); |
| 105 |
if ($reflection) { |
| 106 |
$mapper_name = $reflection->getNamespaceName() . '\\Common\\' . 'Common' . self::$names[$name]; |
| 107 |
} |
| 108 |
} |
| 109 |
$mapper = new $mapper_name($this); |
| 110 |
$this->mappers[$name] = $mapper; |
| 111 |
return $mapper; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
abstract public function initDb(string $version): void; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
abstract public function cleanDb(): void; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function tablePrefix(string $postfix = ''): string |
| 137 |
{ |
| 138 |
return $this->prefix . $postfix; |
| 139 |
} |
| 140 |
} |