-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
DatabaseExtension.php
129 lines (105 loc) 路 3.68 KB
/
DatabaseExtension.php
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
<?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\Bridges\DatabaseDI;
use Nette;
/**
* Nette Framework Database services.
*
* @author David Grudl
* @author Jan Skrasek
*/
class DatabaseExtension extends Nette\DI\CompilerExtension
{
public $databaseDefaults = array(
'dsn' => NULL,
'user' => NULL,
'password' => NULL,
'options' => NULL,
'debugger' => TRUE,
'explain' => TRUE,
'reflection' => NULL, // BC
'conventions' => 'discovered', // Nette\Database\Conventions\DiscoveredConventions
'autowired' => NULL,
);
/** @var bool */
private $debugMode;
public function __construct($debugMode = FALSE)
{
$this->debugMode = $debugMode;
}
public function loadConfiguration()
{
$configs = $this->getConfig();
if (isset($configs['dsn'])) {
$configs = array('default' => $configs);
}
$defaults = $this->databaseDefaults;
$defaults['autowired'] = TRUE;
foreach ((array) $configs as $name => $config) {
if (!is_array($config)) {
continue;
}
$config = $this->validateConfig($defaults, $config, $this->prefix($name));
$defaults['autowired'] = FALSE;
$this->setupDatabase($config, $name);
}
}
private function setupDatabase($config, $name)
{
$container = $this->getContainerBuilder();
foreach ((array) $config['options'] as $key => $value) {
if (preg_match('#^PDO::\w+\z#', $key)) {
unset($config['options'][$key]);
$config['options'][constant($key)] = $value;
}
}
$connection = $container->addDefinition($this->prefix("$name.connection"))
->setClass('Nette\Database\Connection', array($config['dsn'], $config['user'], $config['password'], $config['options']))
->setAutowired($config['autowired']);
$structure = $container->addDefinition($this->prefix("$name.structure"))
->setClass('Nette\Database\Structure')
->setArguments(array($connection))
->setAutowired($config['autowired']);
if (!empty($config['reflection'])) {
$conventionsServiceName = 'reflection';
$config['conventions'] = $config['reflection'];
if (strtolower($config['conventions']) === 'conventional') {
$config['conventions'] = 'Static';
}
} else {
$conventionsServiceName = 'conventions';
}
if (!$config['conventions']) {
$conventions = NULL;
} elseif (is_string($config['conventions'])) {
$conventions = $container->addDefinition($this->prefix("$name.$conventionsServiceName"))
->setClass(preg_match('#^[a-z]+\z#i', $config['conventions'])
? 'Nette\Database\Conventions\\' . ucfirst($config['conventions']) . 'Conventions'
: $config['conventions'])
->setArguments(strtolower($config['conventions']) === 'discovered' ? array($structure) : array())
->setAutowired($config['autowired']);
} else {
$tmp = Nette\DI\Compiler::filterArguments(array($config['conventions']));
$conventions = reset($tmp);
}
$container->addDefinition($this->prefix("$name.context"))
->setClass('Nette\Database\Context', array($connection, $structure, $conventions))
->setAutowired($config['autowired']);
if ($config['debugger']) {
$connection->addSetup('@Tracy\BlueScreen::addPanel', array(
'Nette\Bridges\DatabaseTracy\ConnectionPanel::renderException'
));
if ($this->debugMode) {
$connection->addSetup('Nette\Database\Helpers::createDebugPanel', array($connection, !empty($config['explain']), $name));
}
}
if ($this->name === 'database') {
$container->addAlias($this->prefix($name), $this->prefix("$name.connection"));
$container->addAlias("nette.database.$name", $this->prefix($name));
$container->addAlias("nette.database.$name.context", $this->prefix("$name.context"));
}
}
}