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