Skip to content

Commit 9aa214d

Browse files
author
karelwintersky
committed
2.3.0
- внедряем репозиторий таблиц в Connection. - репозиторий таблиц создается на этапе `Config`. - к классу `Config` добавлены методы-хелперы `initTables`, `addTable`, `getTable` - после коннекта репозиторий таблиц доступен через публичное поле `tables` - после коннекта таблицы доступны через `$pdo->tables[key]` или `$pdo->tables->getTable(key)`
1 parent 0d83042 commit 9aa214d

File tree

5 files changed

+96
-7
lines changed

5 files changed

+96
-7
lines changed

interfaces/Database/ConfigInterface.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,45 @@ interface ConfigInterface
88
{
99
public function __construct(array $connection_config = [], ?LoggerInterface $logger = null);
1010

11+
/**
12+
* Инициализирует репозиторий таблиц
13+
*
14+
* @param string $prefix
15+
* @param array $tables
16+
* @param array $havePrefix
17+
* @param array $haveAlias
18+
* @return self
19+
*/
20+
public function initTables(string $prefix = '',
21+
array $tables = [],
22+
array $havePrefix = [],
23+
array $haveAlias = []): self;
24+
25+
/**
26+
* Добавляет таблицу в репозиторий. Хелпер.
27+
*
28+
* @param string $key
29+
* @param string|null $tableName
30+
* @param string|null $replacement
31+
* @param bool $withPrefix
32+
* @param string|null $alias
33+
* @return self
34+
*/
35+
public function addTable(string $key,
36+
?string $tableName = null,
37+
?string $replacement = null,
38+
bool $withPrefix = false,
39+
?string $alias = null):self;
40+
41+
/**
42+
* Возвращает таблицу по ключу с учетом всех правил
43+
* Если ключ не указан - вернет все таблицы
44+
*
45+
* @param $key
46+
* @return string|array
47+
*/
48+
public function getTable($key = null):string|array;
49+
1150
public function setDriver(?string $driver):self;
1251
public function setHost(string $host = 'localhost'):self;
1352
public function setPort(mixed $port): self;

interfaces/Database/TablesInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ public function addTable(
1919
?string $alias = null
2020
): Tables;
2121

22+
public function getTable(?string $key = null):array|string;
23+
2224
}

sources/Database/Config.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Config implements ConfigInterface
3232
public ?float $slowQueryThreshold = 1.0; // ms
3333
public bool $collectBacktrace = true;
3434

35+
public Tables $tables;
36+
3537
public function __construct(array $connection_config = [], ?LoggerInterface $logger = null)
3638
{
3739
$this->logger = $logger ?? new NullLogger();
@@ -54,6 +56,32 @@ public function __construct(array $connection_config = [], ?LoggerInterface $log
5456
} else {
5557
$this->charset = self::DEFAULT_CHARSET;
5658
}
59+
60+
$this->tables = new Tables();
61+
}
62+
63+
public function initTables(string $prefix = '',
64+
array $tables = [],
65+
array $havePrefix = [],
66+
array $haveAlias = []): self
67+
{
68+
$this->tables = new Tables($prefix, $tables, $havePrefix, $haveAlias);
69+
return $this;
70+
}
71+
72+
public function addTable(string $key,
73+
?string $tableName = null,
74+
?string $replacement = null,
75+
bool $withPrefix = false,
76+
?string $alias = null):self
77+
{
78+
$this->tables->addTable($key, $tableName, $replacement, $withPrefix, $alias);
79+
return $this;
80+
}
81+
82+
public function getTable($key = null):string|array
83+
{
84+
return $this->tables->getTable($key);
5785
}
5886

5987
public function setDriver(?string $driver):self

sources/Database/Connector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Connector extends PDO implements ConnectorInterface
1212
private ?PDO $pdo_connector = null;
1313
private ?Config $config;
1414
private ?Stats $stats;
15+
public Tables $tables;
1516

1617
private string $dsn;
1718
private ?string $username;
@@ -29,6 +30,7 @@ public function __construct(
2930
$this->password = $config->getPassword();
3031
$this->options = $config->getOptions() ?? [];
3132
$this->stats = new Stats($config);
33+
$this->tables = $config->tables;
3234
}
3335

3436
/**
@@ -90,7 +92,7 @@ private function initConnection(): void
9092
* Check real connection
9193
* @return void
9294
*/
93-
private function ensureConnection()
95+
private function ensureConnection(): void
9496
{
9597
if (empty($this->pdo_connector)) {
9698
$this->initConnection();

sources/Database/Tables.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,33 @@ public function addTable(
7676
$this->haveAlias[$key] = $alias;
7777
}
7878

79-
return $this; // Для цепочечных вызовов
79+
return $this;
8080
}
8181

82-
//
82+
/**
83+
* Возвращает список таблиц (в репозитории) с учетом правил замен
84+
* Или одну таблицу по ключу.
85+
*
86+
* @param string|null $key
87+
* @return array|string
88+
*/
89+
public function getTable(?string $key = null):array|string
90+
{
91+
if (is_null($key)) {
92+
$tables = [];
93+
foreach ($this->tables as $key => $v) {
94+
$tables[] = $this->offsetGet($key);
95+
}
96+
97+
return $tables;
98+
}
99+
100+
return $this->offsetGet($key);
101+
}
83102

84103
/**
85104
* Проверяет, существует ли ключ
86-
* (таблица в репозитории) *
105+
* (таблица в репозитории)
87106
*
88107
* @param mixed $offset
89108
* @return bool
@@ -111,9 +130,8 @@ public function offsetGet(mixed $offset): string
111130
// Если аргумент есть в таблице замен - подставляем значение из have_prefix
112131
if (isset($this->havePrefix[$offset])) {
113132
$result = $this->havePrefix[$offset];
114-
}
115-
// Иначе если есть в таблице have_prefix - ставим префикс
116-
elseif (in_array($offset, $this->havePrefix, true)) {
133+
} elseif (in_array($offset, $this->havePrefix, true)) {
134+
// Иначе если есть в таблице have_prefix - ставим префикс
117135
$result = $this->prefix . $tableName;
118136
}
119137
// Иначе ставим значение ключа (уже в $tableName)

0 commit comments

Comments
 (0)