1: <?php
2: /**
3: * Determines how much time is spent while loading/saving the Kolab objects.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Format
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://www.horde.org/libraries/Horde_Kolab_Format
12: */
13:
14: /**
15: * Determines how much time is spent while loading/saving the Kolab objects.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you did not
20: * receive this file, see
21: * http://www.horde.org/licenses/lgpl21.
22: *
23: * @category Kolab
24: * @package Kolab_Format
25: * @author Gunnar Wrobel <wrobel@pardus.de>
26: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
27: * @link http://www.horde.org/libraries/Horde_Kolab_Format
28: */
29: class Horde_Kolab_Format_Decorator_Timed
30: extends Horde_Kolab_Format_Decorator_Base
31: {
32: /**
33: * The timer used for recording the amount of time spent.
34: *
35: * @var Horde_Support_Timer
36: */
37: private $_timer;
38:
39: /**
40: * An optional logger.
41: *
42: * @var mixed
43: */
44: private $_logger;
45:
46: /**
47: * Time spent handling objects.
48: *
49: * @var float
50: */
51: static private $_spent = 0.0;
52:
53: /**
54: * Constructor.
55: *
56: * @param Horde_Kolab_Format $handler The handler to be decorated.
57: * @param Horde_Support_Timer $timer The timer.
58: * @param mixed $logger The optional logger. If set this
59: * needs to provide a debug() method.
60: */
61: public function __construct(Horde_Kolab_Format $handler,
62: Horde_Support_Timer $timer,
63: $logger = null)
64: {
65: parent::__construct($handler);
66: $this->_timer = $timer;
67: $this->_logger = $logger;
68: }
69:
70: /**
71: * Load an object based on the given XML stream.
72: *
73: * @param resource $xml The XML stream of the message.
74: * @param array $options Additional options when parsing the XML. This
75: * decorator provides no additional options.
76: *
77: * @return array The data array representing the object.
78: *
79: * @throws Horde_Kolab_Format_Exception
80: */
81: public function load($xml, $options = array())
82: {
83: $this->_timer->push();
84: $result = $this->getHandler()->load($xml);
85: $spent = $this->_timer->pop();
86: if (is_object($this->_logger)) {
87: $this->_logger->debug(sprintf('Kolab Format data parsing complete. Time spent: %s ms', floor($spent * 1000)));
88: }
89: self::$_spent += $spent;
90: return $result;
91: }
92:
93: /**
94: * Convert the data to a XML stream.
95: *
96: * @param array $object The data array representing the object.
97: * @param array $options Additional options when writing the XML. This
98: * decorator provides no additional options.
99: *
100: * @return resource The data as XML stream.
101: *
102: * @throws Horde_Kolab_Format_Exception
103: */
104: public function save($object, $options = array())
105: {
106: $this->_timer->push();
107: $result = $this->getHandler()->save($object);
108: $spent = $this->_timer->pop();
109: if (is_object($this->_logger)) {
110: $this->_logger->debug(sprintf('Kolab Format data generation complete. Time spent: %s ms', floor($spent * 1000)));
111: }
112: self::$_spent += $spent;
113: return $result;
114: }
115:
116: /**
117: * Report the time spent for loading/saving objects.
118: *
119: * @return float The amount of time.
120: */
121: public function timeSpent()
122: {
123: return self::$_spent;
124: }
125: }