1: <?php
2: /**
3: * A Horde_Injector:: based Horde_Imsp:: 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:: 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_Imsp extends Horde_Core_Factory_Base
29: {
30: /**
31: * The instance cache
32: *
33: * @var array
34: */
35: 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: * Create an instance of a Horde_Imsp class.
55: *
56: * @param type $driver The driver type.
57: * @param array $params Driver params.
58: *
59: * @return Horde_Imsp The Horde_Imsp object
60: */
61: public function create($driver = null, array $params = array())
62: {
63: $signature = serialize(array($driver, $params));
64: if (!isset($this->_instances[$signature])) {
65: $this->_instances[$signature] = self::_factory($driver, $params);
66: }
67:
68: return $this->_instances[$signature];
69: }
70:
71: /**
72: * Factory method
73: *
74: * @param string $driver The driver type, leave empty for client connection
75: * @param array $params The driver parameters, leave empty to use default.
76: *
77: * @return mixed The Horde_Imsp object or Horde_Imsp_Client object.
78: * @throws Horde_Exception
79: */
80: protected function _factory($driver = null, array $params = array())
81: {
82: $driver = basename($driver);
83:
84: // Use global config if none passed in.
85: if (empty($params)) {
86: $params = $GLOBALS['conf']['imsp'];
87: } elseif (empty($params['auth_method'])) {
88: $params['auth_method'] = $GLOBALS['conf']['imsp']['auth_method'];
89: }
90:
91: $params['authObj'] = $this->_injector->getInstance('Horde_Core_Factory_ImspAuth')->create($params['auth_method'], $params);
92: // @TODO: Separate class for the imtest client?
93: unset($params['auth_method']);
94: try {
95: $socket = new Horde_Imsp_Client_Socket($params);
96: } catch (Horde_Imsp_Exception $e) {
97: throw new Horde_Exception($e);
98: }
99: // Return the client itself if no requested driver.
100: if (empty($driver)) {
101: return $socket;
102: }
103: if (!$socket->authenticate($params)) {
104: throw new Horde_Exception_PermissionDenied();
105: }
106:
107: $class = 'Horde_Imsp_' . $driver;
108: if (class_exists($class)) {
109: $obj = new $class($socket, $params);
110: return $obj;
111: }
112:
113: throw new Horde_Exception('Class ' . $class . ' Not Found');
114: }
115:
116: }