1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Provider\Database; |
13
|
|
|
|
14
|
|
|
use Phalcon\Db\Adapter\Pdo\AbstractPdo; |
15
|
|
|
use Phalcon\Db\Adapter\Pdo\Mysql; |
16
|
|
|
use Phalcon\Di\DiInterface; |
17
|
|
|
use Phalcon\Events\ManagerInterface; |
18
|
|
|
use Zemit\Config\ConfigInterface; |
19
|
|
|
use Zemit\Db\Events\Logger; |
20
|
|
|
use Zemit\Db\Events\Profiler; |
21
|
|
|
use Zemit\Provider\AbstractServiceProvider; |
22
|
|
|
|
23
|
|
|
class ServiceProvider extends AbstractServiceProvider |
24
|
|
|
{ |
25
|
|
|
protected ?string $driverName = null; |
26
|
|
|
protected string $serviceName = 'db'; |
27
|
|
|
|
28
|
113 |
|
public function register(DiInterface $di): void |
29
|
|
|
{ |
30
|
113 |
|
$driverName = $this->driverName; |
31
|
113 |
|
$di->setShared($this->getName(), function () use ($di, $driverName) { |
32
|
6 |
|
$config = $di->get('config'); |
33
|
6 |
|
assert($config instanceof ConfigInterface); |
34
|
|
|
|
35
|
|
|
// database config |
36
|
6 |
|
$databaseConfig = $config->pathToArray('database') ?? []; |
37
|
6 |
|
$driverName ??= $databaseConfig['default'] ?? 'mysql'; |
38
|
|
|
|
39
|
|
|
// specified driver name |
40
|
6 |
|
if (isset($databaseConfig['drivers'][$driverName])) { |
41
|
6 |
|
if (!is_array($databaseConfig['drivers'][$driverName])) { |
42
|
|
|
throw new \InvalidArgumentException('Database driver option `' . $driverName . '` must be an array'); |
43
|
|
|
} |
44
|
|
|
|
45
|
6 |
|
$driverOptions = array_filter($databaseConfig['drivers'][$driverName], fn($value) => $value !== null); |
46
|
|
|
|
47
|
6 |
|
if (isset($driverOptions['extends'])) { |
48
|
|
|
if (is_string($driverOptions['extends'])) { |
49
|
|
|
$driverOptions['extends'] = explode(',', $driverOptions['extends']); |
50
|
|
|
} |
51
|
|
|
if (is_array($driverOptions['extends'])) { |
52
|
|
|
foreach ($driverOptions['extends'] as $extend) { |
53
|
|
|
$driverOptions = array_merge($databaseConfig['drivers'][trim($extend)] ?? [], $driverOptions); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// default driver name |
60
|
|
|
else { |
61
|
|
|
$defaultDriverName = $databaseConfig['default'] ?? 'mysql'; |
62
|
|
|
$driverOptions = $databaseConfig['drivers'][$defaultDriverName] ?? []; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// unset unsupported parameters |
66
|
6 |
|
unset($driverOptions['extends']); |
67
|
6 |
|
unset($driverOptions['enable']); |
68
|
|
|
|
69
|
|
|
// dialect |
70
|
6 |
|
if (!empty($driverOptions['dialectClass'])) { |
71
|
6 |
|
$dialectClass = $driverOptions['dialectClass']; |
72
|
6 |
|
assert(class_exists($dialectClass)); |
73
|
6 |
|
$driverOptions['dialectClass'] = new $dialectClass(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// adapter |
77
|
6 |
|
$adapter = $driverOptions['adapter'] ?? Mysql::class; |
78
|
6 |
|
assert(class_exists($adapter)); |
79
|
6 |
|
unset($driverOptions['adapter']); |
80
|
|
|
|
81
|
|
|
// connection |
82
|
6 |
|
$connection = new $adapter($driverOptions); |
83
|
6 |
|
assert($connection instanceof AbstractPdo); |
84
|
|
|
|
85
|
|
|
// attach events |
86
|
6 |
|
$eventsManager = $di->get('eventsManager'); |
87
|
6 |
|
assert($eventsManager instanceof ManagerInterface); |
88
|
6 |
|
$eventsManager->attach('db', new Logger()); |
89
|
6 |
|
$eventsManager->attach('db', new Profiler()); |
90
|
6 |
|
$connection->setEventsManager($eventsManager); |
91
|
|
|
|
92
|
6 |
|
return $connection; |
93
|
113 |
|
}); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|