1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Horde_Kolab_Storage_Factory
29: {
30: 31: 32: 33: 34:
35: private $_params;
36:
37: 38: 39: 40: 41:
42: private $_folder_types;
43:
44: 45: 46: 47: 48:
49: private $_formats;
50:
51: 52: 53: 54: 55:
56: private $_format_factory;
57:
58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71:
72: public function __construct($params = array())
73: {
74: $this->_params = $params;
75: if (isset($params['format']['factory'])) {
76: $this->_format_factory = $params['format']['factory'];
77: } else {
78: $this->_format_factory = new Horde_Kolab_Format_Factory();
79: }
80: }
81:
82: 83: 84: 85: 86:
87: public function create()
88: {
89: if (isset($this->_params['queryset'])) {
90: $queryset = $this->_params['queryset'];
91: } else {
92: $queryset = array();
93: }
94: if (isset($this->_params['storage'])) {
95: $sparams = $this->_params['storage'];
96: } else {
97: $sparams = array();
98: }
99: if (!empty($this->_params['logger'])) {
100: $sparams['logger'] = $this->_params['logger'];
101: }
102: if (!empty($this->_params['cache'])) {
103: $cache = $this->createCache();
104: $storage = new Horde_Kolab_Storage_Cached(
105: $this->createDriver(),
106: new Horde_Kolab_Storage_QuerySet_Cached($this, $queryset, $cache),
107: $this,
108: $cache,
109: $sparams
110: );
111: } else {
112: $storage = new Horde_Kolab_Storage_Uncached(
113: $this->createDriver(),
114: new Horde_Kolab_Storage_QuerySet_Uncached($this, $queryset),
115: $this,
116: $sparams
117: );
118: }
119: $storage = new Horde_Kolab_Storage_Decorator_Synchronization(
120: $storage, new Horde_Kolab_Storage_Synchronization()
121: );
122: return $storage;
123: }
124:
125: 126: 127: 128: 129: 130: 131: 132:
133: public function createDriver($params = array())
134: {
135: $params = array_merge($this->_params, $params);
136: if (!isset($params['driver'])) {
137: throw new Horde_Kolab_Storage_Exception(
138: Horde_Kolab_Storage_Translation::t(
139: 'Missing "driver" parameter!'
140: )
141: );
142: }
143: if (isset($params['params'])) {
144: $config = (array)$params['params'];
145: } else {
146: $config = array();
147: }
148: if (empty($config['host'])) {
149: $config['host'] = 'localhost';
150: }
151: if (empty($config['port'])) {
152: $config['port'] = 143;
153: }
154: if (!empty($params['timelog'])) {
155: $timer = new Horde_Support_Timer();
156: $timer->push();
157: }
158: switch ($params['driver']) {
159: case 'mock':
160: if (!isset($config['data'])) {
161: $config['data'] = array('user/test' => array());
162: }
163: $driver = new Horde_Kolab_Storage_Driver_Mock($this, $config);
164: break;
165: case 'horde':
166: case 'horde-php':
167: $driver = new Horde_Kolab_Storage_Driver_Imap($this, $config);
168: break;
169: case 'php':
170: $driver = new Horde_Kolab_Storage_Driver_Cclient($this, $config);
171: break;
172: case 'pear':
173: $driver = new Horde_Kolab_Storage_Driver_Pear($this, $config);
174: break;
175: case 'roundcube':
176: $driver = new Horde_Kolab_Storage_Driver_Rcube($this, $config);
177: break;
178: default:
179: throw new Horde_Kolab_Storage_Exception(
180: sprintf(
181: Horde_Kolab_Storage_Translation::t(
182: 'Invalid "driver" parameter "%s". Please use one of "mock", "php", "pear", "horde", "horde-php", and "roundcube"!'
183: ),
184: $params['driver']
185: )
186: );
187: }
188:
189: $parser = new Horde_Kolab_Storage_Data_Parser_Structure($driver);
190: $format = new Horde_Kolab_Storage_Data_Format_Mime($this, $parser);
191: $parser->setFormat($format);
192: $driver->setParser($parser);
193:
194: if (!empty($params['logger'])) {
195: $driver = new Horde_Kolab_Storage_Driver_Decorator_Log(
196: $driver, $params['logger']
197: );
198: $parser->setLogger($params['logger']);
199: }
200: if (!empty($params['ignore_parse_errors'])) {
201: $parser->setLogger(false);
202: }
203: if (!empty($params['timelog'])) {
204: $driver = new Horde_Kolab_Storage_Driver_Decorator_Timer(
205: $driver, $timer, $params['timelog']
206: );
207: }
208: return $driver;
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218:
219: public function createFolder(Horde_Kolab_Storage_List $list,
220: $folder)
221: {
222: return new Horde_Kolab_Storage_Folder_Base(
223: $list, $folder
224: );
225: }
226:
227: 228: 229: 230: 231: 232: 233: 234: 235:
236: public function createNamespace($type, $user, array $params = array())
237: {
238: $class = 'Horde_Kolab_Storage_Folder_Namespace_' . ucfirst($type);
239: if (!class_exists($class)) {
240: throw new Horde_Kolab_Storage_Exception(
241: sprintf(
242: Horde_Kolab_Storage_Translation::t(
243: 'Invalid "namespace" type "%s"!'
244: ),
245: $type
246: )
247: );
248: }
249: return new $class($user, $params);
250: }
251:
252: 253: 254: 255: 256: 257: 258:
259: public function createFoldertype($annotation)
260: {
261: if (!isset($this->_folder_types[$annotation])) {
262: $this->_folder_types[$annotation] = new Horde_Kolab_Storage_Folder_Type($annotation);
263: }
264: return $this->_folder_types[$annotation];
265: }
266:
267: 268: 269: 270: 271:
272: public function createCache()
273: {
274: if (isset($this->_params['cache'])) {
275: $params = $this->_params['cache'];
276: } else {
277: $params = array();
278: }
279: if ($params instanceOf Horde_Cache) {
280: return new Horde_Kolab_Storage_Cache($params);
281: } else {
282: $cache = new Horde_Cache(
283: new Horde_Cache_Storage_File($params),
284: array('lifetime' => 0)
285: );
286: }
287: return new Horde_Kolab_Storage_Cache(
288: $cache
289: );
290: }
291:
292: 293: 294: 295: 296: 297: 298:
299: public function createHistory($user)
300: {
301: if (isset($this->_params['history']) &&
302: $this->_params['history'] instanceOf Horde_History) {
303: return $this->_params['history'];
304: }
305: return new Horde_History_Mock($user);
306: }
307:
308: 309: 310: 311: 312: 313: 314: 315: 316:
317: public function createFormat($format, $type, $version)
318: {
319: $key = md5(serialize(array($format, $type, $version)));
320: if (!isset($this->_formats[$key])) {
321: if (isset($this->_params['format'])) {
322: $params = $this->_params['format'];
323: } else {
324: $params = array();
325: }
326: $params['version'] = $version;
327: $this->_formats[$key] = $this->_format_factory->create(
328: $format, $type, $params
329: );
330: }
331: return $this->_formats[$key];
332: }
333: }