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 Turba_Factory_Driver 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: public function create($name)
47: {
48: if (is_array($name)) {
49: $key = md5(serialize($name));
50: $srcName = '';
51: $srcConfig = $name;
52: } else {
53: $key = $name;
54: $srcName = $name;
55: if (empty($GLOBALS['cfgSources'][$name])) {
56: throw new Turba_Exception(sprintf(_("The address book \"%s\" does not exist."), $name));
57: }
58: $srcConfig = $GLOBALS['cfgSources'][$name];
59: }
60:
61: if (!isset($this->_instances[$key])) {
62: $class = 'Turba_Driver_' . ucfirst(basename($srcConfig['type']));
63: if (!class_exists($class)) {
64: throw new Turba_Exception(sprintf(_("Unable to load the definition of %s."), $class));
65: }
66:
67: if (empty($srcConfig['params'])) {
68: $srcConfig['params'] = array();
69: }
70:
71: switch ($class) {
72: case 'Turba_Driver_Sql':
73: try {
74: $srcConfig['params']['db'] = empty($srcConfig['params']['sql'])
75: ? $this->_injector->getInstance('Horde_Db_Adapter')
76: : $this->_injector->getInstance('Horde_Core_Factory_Db')->create('turba', $srcConfig['params']['sql']);
77: $srcConfig['params']['charset'] = isset($srcConfig['params']['sql']['charset'])
78: ? $srcConfig['params']['sql']['charset']
79: : 'UTF-8';
80: } catch (Horde_Db_Exception $e) {
81: throw new Turba_Exception($e);
82: }
83: break;
84:
85: case 'Turba_Driver_Kolab':
86: $srcConfig['params']['storage'] = $this->_injector->getInstance('Horde_Kolab_Storage');
87: break;
88:
89: case 'Turba_Driver_Facebook':
90: $srcConfig['params']['storage'] = $this->_injector->getInstance('Horde_Service_Facebook');
91: break;
92: }
93:
94:
95: if (!isset($srcConfig['params']['charset'])) {
96: $srcConfig['params']['charset'] = 'UTF-8';
97: }
98:
99: $driver = new $class($srcName, $srcConfig['params']);
100:
101:
102: $driver->title = $srcConfig['title'];
103:
104:
105: $driver->map = $srcConfig['map'];
106: foreach ($driver->map as $key => $val) {
107: if (!is_array($val)) {
108: $driver->fields[$key] = $val;
109: }
110: }
111:
112:
113: if (isset($srcConfig['tabs'])) {
114: $driver->tabs = $srcConfig['tabs'];
115: }
116:
117:
118: if (isset($srcConfig['strict'])) {
119: $driver->strict = $srcConfig['strict'];
120: }
121: if (isset($srcConfig['approximate'])) {
122: $driver->approximate = $srcConfig['approximate'];
123: }
124: if (isset($srcConfig['list_name_field'])) {
125: $driver->listNameField = $srcConfig['list_name_field'];
126: }
127: if (isset($srcConfig['alternative_name'])) {
128: $driver->alternativeName = $srcConfig['alternative_name'];
129: }
130: $this->_instances[$key] = $driver;
131: }
132:
133: return $this->_instances[$key];
134: }
135:
136: }
137: