1: <?php
2: /**
3: * A Horde_Injector:: based factory for creating PEAR DB objects.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Core
9: * @author Michael Slusarz <slusarz@horde.org>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Core
12: */
13:
14: /**
15: * A Horde_Injector:: based factory for creating PEAR DB objects..
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Horde
23: * @package Core
24: * @author Michael Slusarz <slusarz@horde.org>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Core
27: */
28: class Horde_Core_Factory_DbPear extends Horde_Core_Factory_Base
29: {
30: /**
31: * Instances.
32: *
33: * @var array
34: */
35: private $_instances = array();
36:
37: /**
38: * Return the DB instance.
39: *
40: * @param string $type Either 'read' or 'rw'.
41: * @param string $app The application.
42: * @param mixed $dtype The type. If this is an array, this is used as
43: * the configuration array.
44: *
45: * @return DB The singleton DB instance.
46: * @throws Horde_Exception
47: */
48: public function create($type = 'rw', $app = 'horde', $dtype = null)
49: {
50: global $registry;
51:
52: $sig = hash('md5', serialize($type . '|' . $app . '|' . $dtype));
53:
54: if (isset($this->_instances[$sig])) {
55: return $this->_instances[$sig];
56: }
57:
58: $pushed = ($app == 'horde')
59: ? false
60: : $registry->pushApp($app);
61:
62: $config = is_array($dtype)
63: ? $dtype
64: : $this->getConfig($dtype);
65:
66: if ($type == 'read' && !empty($config['splitread'])) {
67: $config = array_merge($config, $config['read']);
68: }
69:
70: Horde::assertDriverConfig($config, 'sql', array('charset', 'phptype'));
71:
72: /* Connect to the SQL server using the supplied parameters. */
73: $db = DB::connect($config, array(
74: 'persistent' => !empty($config['persistent']),
75: 'ssl' => !empty($config['ssl'])
76: ));
77:
78: if ($db instanceof PEAR_Error) {
79: if ($pushed) {
80: $registry->popApp();
81: }
82: throw new Horde_Exception($db);
83: }
84:
85: // Set DB portability options.
86: switch ($db->phptype) {
87: case 'mssql':
88: $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
89: break;
90:
91: default:
92: $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
93: break;
94: }
95:
96: if ($pushed) {
97: $registry->popApp();
98: }
99:
100: $this->_instances[$sig] = $db;
101:
102: return $db;
103: }
104:
105: /**
106: */
107: public function getConfig($type)
108: {
109: return Horde::getDriverConfig($type, 'sql');
110: }
111:
112: }
113: