~ubuntu-branches/debian/sid/php-horde-turba/sid

« back to all changes in this revision

Viewing changes to turba-4.1.0/lib/Factory/Driver.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2013-08-11 13:16:25 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130811131625-z91stjvq51jr9onv
Tags: 4.1.1-1
New upstream version 4.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * A Horde_Injector:: based Turba_Driver:: factory.
4
 
 *
5
 
 * PHP version 5
6
 
 *
7
 
 * @author   Michael Slusarz <slusarz@horde.org>
8
 
 * @category Horde
9
 
 * @license  http://www.horde.org/licenses/apl.html APL
10
 
 * @link     http://pear.horde.org/index.php?package=Turba
11
 
 * @package  Turba
12
 
 */
13
 
 
14
 
/**
15
 
 * A Horde_Injector:: based Turba_Driver:: factory.
16
 
 *
17
 
 * Copyright 2010-2013 Horde LLC (http://www.horde.org/)
18
 
 *
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.
21
 
 *
22
 
 * @author   Michael Slusarz <slusarz@horde.org>
23
 
 * @category Horde
24
 
 * @license  http://www.horde.org/licenses/apl.html APL
25
 
 * @link     http://pear.horde.org/index.php?package=Turba
26
 
 * @package  Turba
27
 
 */
28
 
class Turba_Factory_Driver extends Horde_Core_Factory_Base
29
 
{
30
 
    /**
31
 
     * Instances.
32
 
     *
33
 
     * @var array
34
 
     */
35
 
    private $_instances = array();
36
 
 
37
 
    /**
38
 
     * Return the Turba_Driver:: instance.
39
 
     *
40
 
     * @param mixed $name  Either a string containing the internal name of this
41
 
     *                     source, or a config array describing the source.
42
 
     *
43
 
     * @return Turba_Driver  The singleton instance.
44
 
     * @throws Turba_Exception
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
 
            /* Make sure charset exists. */
95
 
            if (!isset($srcConfig['params']['charset'])) {
96
 
                $srcConfig['params']['charset'] = 'UTF-8';
97
 
            }
98
 
 
99
 
            $driver = new $class($srcName, $srcConfig['params']);
100
 
 
101
 
            // Title
102
 
            $driver->title = $srcConfig['title'];
103
 
 
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;
109
 
                }
110
 
            }
111
 
 
112
 
            /* Store tabs. */
113
 
            if (isset($srcConfig['tabs'])) {
114
 
                $driver->tabs = $srcConfig['tabs'];
115
 
            }
116
 
 
117
 
            /* Store remaining fields. */
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
 
}