1: <?php
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: class Horde_Core_Factory_Db extends Horde_Core_Factory_Base
29: {
30: 31: 32: 33: 34:
35: private $_instances = array();
36:
37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: public function create($app = 'horde', $backend = null)
50: {
51: $sig = hash('md5', serialize(array($app, $backend)));
52:
53: if (isset($this->_instances[$sig])) {
54: return $this->_instances[$sig];
55: }
56:
57: $pushed = ($app == 'horde')
58: ? false
59: : $GLOBALS['registry']->pushApp($app);
60:
61: $config = is_array($backend)
62: ? $backend
63: : $this->getConfig($backend);
64:
65:
66: if (isset($config['driverconfig']) &&
67: ($config['driverconfig'] == 'horde')) {
68: $this->_instances[$sig] = $this->create();
69: return $this->_instances[$sig];
70: }
71:
72:
73:
74:
75:
76: if (!is_array($backend) && $backend == 'auth') {
77: unset($config['driverconfig'],
78: $config['query_auth'],
79: $config['query_add'],
80: $config['query_getpw'],
81: $config['query_update'],
82: $config['query_resetpassword'],
83: $config['query_remove'],
84: $config['query_list'],
85: $config['query_exists'],
86: $config['encryption'],
87: $config['show_encryption'],
88: $config['username_field'],
89: $config['password_field'],
90: $config['table'],
91: $config['login_block'],
92: $config['login_block_count'],
93: $config['login_block_time']);
94: }
95: unset($config['umask']);
96:
97: $e = null;
98: try {
99: $this->_instances[$sig] = $this->createDb($config);
100: } catch (Horde_Exception $e) {}
101:
102: if ($pushed) {
103: $GLOBALS['registry']->popApp();
104: }
105:
106: if ($e) {
107: throw $e;
108: }
109:
110: return $this->_instances[$sig];
111: }
112:
113: 114:
115: public function getConfig($backend)
116: {
117: return Horde::getDriverConfig($backend, 'sql');
118: }
119:
120: 121:
122: public function createDb($config)
123: {
124:
125: if (!empty($config['splitread'])) {
126: $read_config = $config['read'];
127: unset($config['read'], $config['splitread']);
128: return new Horde_Db_Adapter_SplitRead($this->createDb(array_merge($config, $read_config)), $this->createDb($config));
129: }
130:
131: if (!isset($config['adapter'])) {
132: if (empty($config['phptype'])) {
133: throw new Horde_Exception('The database configuration is missing.');
134: }
135: if ($config['phptype'] == 'oci8') {
136: $config['phptype'] = 'oci';
137: }
138: if ($config['phptype'] == 'mysqli') {
139: $config['adapter'] = 'mysqli';
140: } elseif ($config['phptype'] == 'mysql') {
141: if (extension_loaded('pdo_mysql')) {
142: $config['adapter'] = 'pdo_mysql';
143: } else {
144: $config['adapter'] = 'mysql';
145: }
146: } else {
147: $config['adapter'] = 'pdo_' . $config['phptype'];
148: }
149: }
150:
151: if (!empty($config['hostspec'])) {
152: $config['host'] = $config['hostspec'];
153: }
154:
155: $adapter = str_replace(' ', '_', ucwords(str_replace('_', ' ', basename($config['adapter']))));
156: $class = 'Horde_Db_Adapter_' . $adapter;
157:
158: if (class_exists($class)) {
159: unset($config['hostspec'], $config['splitread']);
160: $ob = new $class($config);
161:
162: if (!isset($config['cache'])) {
163: $injector = $this->_injector->createChildInjector();
164: $injector->setInstance('Horde_Db_Adapter', $ob);
165: $cacheFactory = $this->_injector->getInstance('Horde_Core_Factory_Cache');
166: $cache = $cacheFactory->create($injector);
167: $ob->setCache($cache);
168: }
169:
170: if (!isset($config['logger'])) {
171: $ob->setLogger($this->_injector->getInstance('Horde_Log_Logger'));
172: }
173:
174: return $ob;
175: }
176:
177: throw new Horde_Exception('Adapter class "' . $class . '" not found');
178: }
179: }
180: