3
* A Horde_Injector:: based Turba_Driver:: factory.
7
* @author Michael Slusarz <slusarz@horde.org>
9
* @license http://www.horde.org/licenses/apl.html APL
10
* @link http://pear.horde.org/index.php?package=Turba
15
* A Horde_Injector:: based Turba_Driver:: factory.
17
* Copyright 2010-2013 Horde LLC (http://www.horde.org/)
19
* See the enclosed file COPYING for license information (APL). If you
20
* did not receive this file, see http://www.horde.org/licenses/apl.html.
22
* @author Michael Slusarz <slusarz@horde.org>
24
* @license http://www.horde.org/licenses/apl.html APL
25
* @link http://pear.horde.org/index.php?package=Turba
28
class Turba_Factory_Driver extends Horde_Core_Factory_Base
35
private $_instances = array();
38
* Return the Turba_Driver:: instance.
40
* @param mixed $name Either a string containing the internal name of this
41
* source, or a config array describing the source.
43
* @return Turba_Driver The singleton instance.
44
* @throws Turba_Exception
46
public function create($name)
48
if (is_array($name)) {
49
$key = md5(serialize($name));
55
if (empty($GLOBALS['cfgSources'][$name])) {
56
throw new Turba_Exception(sprintf(_("The address book \"%s\" does not exist."), $name));
58
$srcConfig = $GLOBALS['cfgSources'][$name];
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));
67
if (empty($srcConfig['params'])) {
68
$srcConfig['params'] = array();
72
case 'Turba_Driver_Sql':
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']
80
} catch (Horde_Db_Exception $e) {
81
throw new Turba_Exception($e);
85
case 'Turba_Driver_Kolab':
86
$srcConfig['params']['storage'] = $this->_injector->getInstance('Horde_Kolab_Storage');
89
case 'Turba_Driver_Facebook':
90
$srcConfig['params']['storage'] = $this->_injector->getInstance('Horde_Service_Facebook');
94
/* Make sure charset exists. */
95
if (!isset($srcConfig['params']['charset'])) {
96
$srcConfig['params']['charset'] = 'UTF-8';
99
$driver = new $class($srcName, $srcConfig['params']);
102
$driver->title = $srcConfig['title'];
104
/* Store and translate the map at the Source level. */
105
$driver->map = $srcConfig['map'];
106
foreach ($driver->map as $mapkey => $val) {
107
if (!is_array($val)) {
108
$driver->fields[$mapkey] = $val;
113
if (isset($srcConfig['tabs'])) {
114
$driver->tabs = $srcConfig['tabs'];
117
/* Store remaining fields. */
118
if (isset($srcConfig['strict'])) {
119
$driver->strict = $srcConfig['strict'];
121
if (isset($srcConfig['approximate'])) {
122
$driver->approximate = $srcConfig['approximate'];
124
if (isset($srcConfig['list_name_field'])) {
125
$driver->listNameField = $srcConfig['list_name_field'];
127
if (isset($srcConfig['alternative_name'])) {
128
$driver->alternativeName = $srcConfig['alternative_name'];
130
$this->_instances[$key] = $driver;
133
return $this->_instances[$key];