1: <?php
2: /**
3: * A Horde_Injector:: based Horde_Imsp_Auth:: factory.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Core
9: * @author Michael J Rubinsky <mrubinsk@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_Imsp_Auth:: factory.
16: *
17: * Copyright 2011-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 J Rubinsky <mrubinsk@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_ImspAuth
29: {
30: /**
31: * Instance cache
32: *
33: * @var array
34: */
35: static protected $_instances = array();
36:
37: /**
38: *
39: * @var Horde_Injector
40: */
41: protected $_injector;
42:
43: /**
44: * Constructor
45: *
46: * @param Horde_Injector $injector
47: */
48: public function __construct(Horde_Injector $injector)
49: {
50: $this->_injector = $injector;
51: }
52:
53: /**
54: * Attempts to return a concrete Horde_Imsp_Auth instance based on $driver.
55: * Will only create a new object if one with the same parameters already
56: * does not exist.
57: *
58: * @param string $driver Type of IMSP_Auth subclass to return.
59: * @param array $params The driver parameters.
60: *
61: * @return Horde_Imsp_Auth
62: */
63: static public function create($driver, array $params = array())
64: {
65: //@TODO: Fix this.
66: /* Check for any imtest driver instances and kill them.
67: Otherwise, the socket will hang between requests from
68: seperate drivers (an Auth request and an Options request).*/
69: if (is_array(self::$_instances)) {
70: foreach (self::$_instances as $obj) {
71: if ($obj->getDriverType() == 'imtest') {
72: $obj->logout();
73: }
74: }
75: }
76: $signature = serialize(array($driver, $params));
77: if (!isset(self::$_instances[$signature])) {
78: self::$_instances[$signature] = self::_factory($driver, $params);
79: }
80:
81: return self::$_instances[$signature];
82: }
83:
84: /**
85: * Attempts to return a concrete Horde_Imsp_Auth instance based on $driver
86: * Must be called as &Horde_Imsp_Auth::factory()
87: *
88: * @param string $driver Type of Horde_Imsp_Auth subclass to return.
89: *
90: * @return mixed The created Horde_Imsp_Auth subclass.
91: * @throws Horde_Exception
92: */
93: static protected function _factory($driver, array $params)
94: {
95: $driver = basename($driver);
96: $class = 'Horde_Imsp_Auth_' . $driver;
97: // Verify user/pass
98: if (empty($params['username'])) {
99: $params['username'] = $GLOBALS['registry']->getAuth('bare');
100: $params['password'] = $GLOBALS['registry']->getAuthCredential('password');
101: }
102: if (class_exists($class)) {
103: return new $class($params);
104: }
105:
106: throw new Horde_Exception(sprintf('Unable to load the definition of %s.', $class));
107: }
108:
109: }