1: <?php
2: /**
3: * A Horde_Injector:: based Horde_Identity:: factory.
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 Horde_Identity:: factory.
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_Identity extends Horde_Core_Factory_Base
29: {
30: /**
31: * Instances.
32: *
33: * @var array
34: */
35: private $_instances = array();
36:
37: /**
38: * Return the Horde_Identity:: instance.
39: *
40: * @param string $user The user to use, if not the current user.
41: * @param string $driver The identity driver. Either empty (use default
42: * driver) or an application name.
43: *
44: * @return Horde_Identity The singleton identity instance.
45: * @throws Horde_Exception
46: */
47: public function create($user = null, $driver = null)
48: {
49: global $prefs, $registry;
50:
51: $class = 'Horde_Core_Prefs_Identity';
52: switch ($driver) {
53: case 'horde':
54: // Bug #9936: There is a conflict between the horde/Prefs
55: // Identity base driver and the application-specific Identity
56: // driver for Horde.
57: $temp_class = 'Horde_Prefs_HordeIdentity';
58: if (class_exists($temp_class)) {
59: $class = $temp_class;
60: }
61: break;
62:
63: default:
64: if (!empty($driver)) {
65: $class = Horde_String::ucfirst($driver) . '_Prefs_Identity';
66: }
67: break;
68: }
69: $key = $class . '|' . $user;
70:
71: if (!isset($this->_instances[$key])) {
72: if (!class_exists($class)) {
73: throw new Horde_Exception('Class definition of ' . $class . ' not found.');
74: }
75:
76: $params = array(
77: 'user' => is_null($user) ? $registry->getAuth() : $user,
78: );
79:
80: if (isset($prefs) && ($params['user'] == $registry->getAuth())) {
81: $params['prefs'] = $prefs;
82: } else {
83: $params['prefs'] = $this->_injector->getInstance('Horde_Core_Factory_Prefs')->create($registry->getApp(), array(
84: 'cache' => false,
85: 'user' => $user
86: ));
87: $params['prefs']->retrieve();
88: }
89:
90: $this->_instances[$key] = new $class($params);
91: $this->_instances[$key]->init();
92: }
93:
94: return $this->_instances[$key];
95: }
96:
97: }
98: