1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\DatabaseDI;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class DatabaseExtension extends Nette\DI\CompilerExtension
17: {
18: public $databaseDefaults = [
19: 'dsn' => null,
20: 'user' => null,
21: 'password' => null,
22: 'options' => null,
23: 'debugger' => true,
24: 'explain' => true,
25: 'reflection' => null,
26: 'conventions' => 'discovered',
27: 'autowired' => null,
28: ];
29:
30:
31: private $debugMode;
32:
33:
34: public function __construct($debugMode = false)
35: {
36: $this->debugMode = $debugMode;
37: }
38:
39:
40: public function loadConfiguration()
41: {
42: $configs = $this->getConfig();
43: $configs = is_array(reset($configs))
44: ? $configs
45: : ['default' => $configs];
46:
47: $defaults = $this->databaseDefaults;
48: $defaults['autowired'] = true;
49: foreach ((array) $configs as $name => $config) {
50: if (!is_array($config)) {
51: continue;
52: }
53: $config = $this->validateConfig($defaults, $config, $this->prefix($name));
54: $defaults['autowired'] = false;
55: $this->setupDatabase($config, $name);
56: }
57: }
58:
59:
60: private function setupDatabase($config, $name)
61: {
62: $builder = $this->getContainerBuilder();
63:
64: foreach ((array) $config['options'] as $key => $value) {
65: if (is_string($value) && preg_match('#^PDO::\w+\z#', $value)) {
66: $config['options'][$key] = $value = constant($value);
67: }
68: if (preg_match('#^PDO::\w+\z#', $key)) {
69: unset($config['options'][$key]);
70: $config['options'][constant($key)] = $value;
71: }
72: }
73:
74: $connection = $builder->addDefinition($this->prefix("$name.connection"))
75: ->setFactory(Nette\Database\Connection::class, [$config['dsn'], $config['user'], $config['password'], $config['options']])
76: ->setAutowired($config['autowired']);
77:
78: $structure = $builder->addDefinition($this->prefix("$name.structure"))
79: ->setFactory(Nette\Database\Structure::class)
80: ->setArguments([$connection])
81: ->setAutowired($config['autowired']);
82:
83: if (!empty($config['reflection'])) {
84: $conventionsServiceName = 'reflection';
85: $config['conventions'] = $config['reflection'];
86: if (is_string($config['conventions']) && strtolower($config['conventions']) === 'conventional') {
87: $config['conventions'] = 'Static';
88: }
89: } else {
90: $conventionsServiceName = 'conventions';
91: }
92:
93: if (!$config['conventions']) {
94: $conventions = null;
95:
96: } elseif (is_string($config['conventions'])) {
97: $conventions = $builder->addDefinition($this->prefix("$name.$conventionsServiceName"))
98: ->setFactory(preg_match('#^[a-z]+\z#i', $config['conventions'])
99: ? 'Nette\Database\Conventions\\' . ucfirst($config['conventions']) . 'Conventions'
100: : $config['conventions'])
101: ->setArguments(strtolower($config['conventions']) === 'discovered' ? [$structure] : [])
102: ->setAutowired($config['autowired']);
103:
104: } else {
105: $class = method_exists(Nette\DI\Helpers::class, 'filterArguments') ? Nette\DI\Helpers::class : Nette\DI\Compiler::class;
106: $conventions = $class::filterArguments([$config['conventions']])[0];
107: }
108:
109: $builder->addDefinition($this->prefix("$name.context"))
110: ->setFactory(Nette\Database\Context::class, [$connection, $structure, $conventions])
111: ->setAutowired($config['autowired']);
112:
113: if ($config['debugger']) {
114: $connection->addSetup('@Tracy\BlueScreen::addPanel', [
115: 'Nette\Bridges\DatabaseTracy\ConnectionPanel::renderException',
116: ]);
117: if ($this->debugMode) {
118: $connection->addSetup('Nette\Database\Helpers::createDebugPanel', [$connection, !empty($config['explain']), $name]);
119: }
120: }
121:
122: if ($this->name === 'database') {
123: $builder->addAlias($this->prefix($name), $this->prefix("$name.connection"));
124: $builder->addAlias("nette.database.$name", $this->prefix($name));
125: $builder->addAlias("nette.database.$name.context", $this->prefix("$name.context"));
126: }
127: }
128: }
129: