1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jade/jade package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jade\Provider; |
13
|
|
|
|
14
|
|
|
use Doctrine\DBAL\DriverManager; |
15
|
|
|
use Doctrine\DBAL\Configuration; |
16
|
|
|
use Doctrine\Common\EventManager; |
17
|
|
|
use Jade\Container; |
18
|
|
|
use Jade\ContainerInterface; |
19
|
|
|
use Jade\ServiceProviderInterface; |
20
|
|
|
use Symfony\Bridge\Doctrine\Logger\DbalLogger; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Doctrine DBAL Provider. |
24
|
|
|
* |
25
|
|
|
* @author Fabien Potencier <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class DoctrineServiceProvider implements ServiceProviderInterface |
28
|
|
|
{ |
29
|
|
|
public function register(ContainerInterface $app) |
30
|
|
|
{ |
31
|
|
|
$app['db.default_options'] = [ |
32
|
|
|
'driver' => 'pdo_mysql', |
33
|
|
|
'dbname' => null, |
34
|
|
|
'host' => 'localhost', |
35
|
|
|
'user' => 'root', |
36
|
|
|
'password' => null, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$app['dbs.options.initializer'] = $app->protect(function () use ($app) { |
40
|
|
|
static $initialized = false; |
41
|
|
|
|
42
|
|
|
if ($initialized) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$initialized = true; |
47
|
|
|
|
48
|
|
|
if (!isset($app['dbs.options'])) { |
49
|
|
|
$app['dbs.options'] = ['default' => isset($app['db.options']) ? $app['db.options'] : []]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$tmp = $app['dbs.options']; |
53
|
|
|
foreach ($tmp as $name => &$options) { |
54
|
|
|
$options = array_replace($app['db.default_options'], $options); |
55
|
|
|
|
56
|
|
|
if (!isset($app['dbs.default'])) { |
57
|
|
|
$app['dbs.default'] = $name; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
$app['dbs.options'] = $tmp; |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
$app['dbs'] = function ($app) { |
64
|
|
|
$app['dbs.options.initializer'](); |
65
|
|
|
|
66
|
|
|
$dbs = new Container(); |
67
|
|
|
foreach ($app['dbs.options'] as $name => $options) { |
68
|
|
|
if ($app['dbs.default'] === $name) { |
69
|
|
|
// we use shortcuts here in case the default has been overridden |
70
|
|
|
$config = $app['db.config']; |
71
|
|
|
$manager = $app['db.event_manager']; |
72
|
|
|
} else { |
73
|
|
|
$config = $app['dbs.config'][$name]; |
74
|
|
|
$manager = $app['dbs.event_manager'][$name]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$dbs[$name] = function ($dbs) use ($options, $config, $manager) { |
|
|
|
|
78
|
|
|
return DriverManager::getConnection($options, $config, $manager); |
79
|
|
|
}; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $dbs; |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
$app['dbs.config'] = function ($app) { |
86
|
|
|
$app['dbs.options.initializer'](); |
87
|
|
|
|
88
|
|
|
$configs = new Container(); |
89
|
|
|
$addLogger = isset($app['logger']) && null !== $app['logger'] && class_exists('Symfony\Bridge\Doctrine\Logger\DbalLogger'); |
90
|
|
|
foreach ($app['dbs.options'] as $name => $options) { |
91
|
|
|
$configs[$name] = new Configuration(); |
92
|
|
|
if ($addLogger) { |
93
|
|
|
$configs[$name]->setSQLLogger(new DbalLogger($app['logger'], isset($app['stopwatch']) ? $app['stopwatch'] : null)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $configs; |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
$app['dbs.event_manager'] = function ($app) { |
101
|
|
|
$app['dbs.options.initializer'](); |
102
|
|
|
|
103
|
|
|
$managers = new Container(); |
104
|
|
|
foreach ($app['dbs.options'] as $name => $options) { |
105
|
|
|
$managers[$name] = new EventManager(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $managers; |
109
|
|
|
}; |
110
|
|
|
|
111
|
|
|
// shortcuts for the "first" DB |
112
|
|
|
$app['db'] = function ($app) { |
113
|
|
|
$dbs = $app['dbs']; |
114
|
|
|
|
115
|
|
|
return $dbs[$app['dbs.default']]; |
116
|
|
|
}; |
117
|
|
|
|
118
|
|
|
$app['db.config'] = function ($app) { |
119
|
|
|
$dbs = $app['dbs.config']; |
120
|
|
|
|
121
|
|
|
return $dbs[$app['dbs.default']]; |
122
|
|
|
}; |
123
|
|
|
|
124
|
|
|
$app['db.event_manager'] = function ($app) { |
125
|
|
|
$dbs = $app['dbs.event_manager']; |
126
|
|
|
|
127
|
|
|
return $dbs[$app['dbs.default']]; |
128
|
|
|
}; |
129
|
|
|
} |
130
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.