-
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathDatabaseExtension.php
More file actions
170 lines (140 loc) 路 5.26 KB
/
Copy pathDatabaseExtension.php
File metadata and controls
170 lines (140 loc) 路 5.26 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php declare(strict_types=1);
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Bridges\DatabaseDI;
use Nette;
use Nette\Schema\Expect;
use Tracy;
use function is_array, is_string;
/**
* Registers database Connection, Structure, and Explorer services in the DI container.
*/
class DatabaseExtension extends Nette\DI\CompilerExtension
{
public function __construct(
private readonly bool $debugMode = false,
) {
}
public function getConfigSchema(): Nette\Schema\Schema
{
return Expect::arrayOf(
Expect::structure([
'dsn' => Expect::string()->required()->dynamic(),
'user' => Expect::string()->nullable()->dynamic(),
'password' => Expect::string()->nullable()->dynamic(),
'options' => Expect::array(),
'debugger' => Expect::bool(),
'explain' => Expect::bool(true),
'reflection' => Expect::string(), // BC
'conventions' => Expect::string('discovered'), // Nette\Database\Conventions\DiscoveredConventions
'mapping' => Expect::structure([
'convention' => Expect::string(''),
'tables' => Expect::arrayOf('string', 'string'),
])->before(fn($v) => is_string($v) ? ['convention' => $v] : $v),
'autowired' => Expect::bool(),
]),
)->before(fn($val) => is_array(reset($val)) || reset($val) === null
? $val
: ['default' => $val]);
}
public function loadConfiguration(): void
{
$autowired = true;
foreach ($this->config as $name => $config) {
$config->autowired ??= $autowired;
$autowired = false;
$this->setupDatabase($config, $name);
}
}
public function beforeCompile(): void
{
$builder = $this->getContainerBuilder();
foreach ($this->config as $name => $config) {
if ($config->debugger ?? $builder->getByType(Tracy\BlueScreen::class)) {
$connection = $builder->getDefinition($this->prefix("$name.connection"));
$connection->addSetup(
[Nette\Bridges\DatabaseTracy\ConnectionPanel::class, 'initialize'],
[$connection, $this->debugMode, $name, !empty($config->explain)],
);
}
}
}
private function setupDatabase(\stdClass $config, string $name): void
{
$builder = $this->getContainerBuilder();
foreach ($config->options as $key => $value) {
if (is_string($value) && preg_match('#^PDO::\w+$#D', $value)) {
$config->options[$key] = $value = constant($value);
}
if (preg_match('#^PDO::\w+$#D', $key)) {
unset($config->options[$key]);
$config->options[constant($key)] = $value;
}
}
$connection = $builder->addDefinition($this->prefix("$name.connection"))
->setFactory(Nette\Database\Connection::class, [$config->dsn, $config->user, $config->password, $config->options])
->setAutowired($config->autowired);
$structure = $builder->addDefinition($this->prefix("$name.structure"))
->setFactory(Nette\Database\Structure::class)
->setArguments([$connection])
->setAutowired($config->autowired);
if (!empty($config->reflection)) {
$conventionsServiceName = 'reflection';
$config->conventions = $config->reflection;
if (is_string($config->conventions) && strtolower($config->conventions) === 'conventional') {
$config->conventions = 'Static';
}
} else {
$conventionsServiceName = 'conventions';
}
if (!$config->conventions) {
$conventions = null;
} elseif (is_string($config->conventions)) {
$conventions = $builder->addDefinition($this->prefix("$name.$conventionsServiceName"))
->setFactory(preg_match('#^[a-z]+$#Di', $config->conventions)
? 'Nette\Database\Conventions\\' . ucfirst($config->conventions) . 'Conventions'
: $config->conventions)
->setArguments(strtolower($config->conventions) === 'discovered' ? [$structure] : [])
->setAutowired($config->autowired);
} else {
$conventions = Nette\DI\Helpers::filterArguments([$config->conventions])[0];
}
$rowMapping = ($config->mapping->convention || $config->mapping->tables)
? new Nette\DI\Definitions\Statement([self::class, 'createRowMapping'], [
$config->mapping->convention,
(array) $config->mapping->tables,
])
: null;
$builder->addDefinition($this->prefix("$name.explorer"))
->setFactory(Nette\Database\Explorer::class, [$connection, $structure, $conventions, null, $rowMapping])
->setAutowired($config->autowired);
$builder->addAlias($this->prefix("$name.context"), $this->prefix("$name.explorer"));
if ($this->name === 'database') {
$builder->addAlias($this->prefix($name), $this->prefix("$name.connection"));
$builder->addAlias("nette.database.$name", $this->prefix($name));
$builder->addAlias("nette.database.$name.context", $this->prefix("$name.explorer"));
}
}
/**
* Creates a row mapping closure that resolves an ActiveRow subclass for each table name.
* @param array<string, string> $tables
* @return \Closure(string): string
*/
public static function createRowMapping(string $convention, array $tables): \Closure
{
return static function (string $table) use ($convention, $tables): string {
if (isset($tables[$table])) {
return $tables[$table];
}
if ($convention !== '') {
$class = str_replace('*', str_replace(' ', '', ucwords(strtr($table, '_', ' '))), $convention);
if (class_exists($class)) {
return $class;
}
}
return Nette\Database\Table\ActiveRow::class;
};
}
}