1: <?php
2: /**
3: * Copyright 2010-2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2010-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * A Horde_Injector based Horde_Mime_Viewer factory for IMP drivers.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2010-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Factory_MimeViewer extends Horde_Core_Factory_MimeViewer
24: {
25: /**
26: * Temporary storage for IMP_Contents object.
27: *
28: * @var IMP_Contents
29: */
30: private $_contents;
31:
32: /**
33: * Instances.
34: *
35: * @var array
36: */
37: private $_instances = array();
38:
39: /**
40: * Attempts to return a concrete Horde_Mime_Viewer object based on the
41: * MIME type.
42: *
43: * @param Horde_Mime_Part $mime An object with the data to be rendered.
44: * @param array $opts Additional options:
45: * - contents: (IMP_Contents) Object associated with $mime.
46: * - type: (string) The MIME type to use for loading.
47: *
48: * @return Horde_Mime_Viewer_Base The newly created instance.
49: * @throws Horde_Mime_Viewer_Exception
50: */
51: public function create(Horde_Mime_Part $mime, array $opts = array())
52: {
53: $opts = array_merge(array(
54: 'contents' => null,
55: 'type' => null
56: ), $opts);
57:
58: $sig = implode('|', array(
59: spl_object_hash($mime),
60: $opts['contents'] ? spl_object_hash($opts['contents']) : '',
61: strval($opts['type'])
62: ));
63:
64: if (!isset($this->_instances[$sig])) {
65: $this->_contents = $opts['contents'];
66: $this->_instances[$sig] = parent::create($mime, array_filter(array(
67: 'app' => 'imp',
68: 'type' => $opts['type']
69: )));
70: unset($this->_contents);
71: }
72:
73: return $this->_instances[$sig];
74: }
75:
76: /**
77: * Callback used to return a MIME Viewer object from within certain
78: * Viewer drivers.
79: *
80: * @param Horde_Mime_Viewer_Base $viewer The MIME Viewer driver
81: * requesting the new object.
82: * @param Horde_Mime_Part $mime An object with the data to be
83: * rendered.
84: * @param string $type The MIME type to use for
85: * rendering.
86: *
87: * @return Horde_Mime_Viewer_Base The newly created instance.
88: * @throws Horde_Mime_Viewer_Exception
89: */
90: public function createCallback(Horde_Mime_Viewer_Base $viewer,
91: Horde_Mime_Part $mime, $type)
92: {
93: return $this->create($mime, array(
94: 'contents' => $viewer->getConfigParam('imp_contents'),
95: 'type' => $type
96: ));
97: }
98:
99: /**
100: */
101: public function getViewerConfig($type, $app)
102: {
103: list($driver, $params) = parent::getViewerConfig($type, $app);
104:
105: switch ($driver) {
106: case 'Horde_Mime_Viewer_Report':
107: case 'Horde_Mime_Viewer_Security':
108: case 'Report':
109: case 'Security':
110: $params['viewer_callback'] = array($this, 'createCallback');
111: break;
112: }
113:
114: $params['imp_contents'] = $this->_contents;
115:
116: return array($driver, $params);
117: }
118:
119: }
120: