1: <?php
2: /**
3: * Determines some memory parameters 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 some memory parameters while loading/saving the Kolab objects.
16: *
17: * Copyright 2011-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_Memory
30: extends Horde_Kolab_Format_Decorator_Base
31: {
32: /**
33: * The memory tracker used for recording the memory parameters.
34: *
35: * @var Horde_Support_Memory
36: */
37: private $_memory;
38:
39: /**
40: * An optional logger.
41: *
42: * @var mixed
43: */
44: private $_logger;
45:
46: /**
47: * Constructor.
48: *
49: * @param Horde_Kolab_Format $handler The handler to be decorated.
50: * @param Horde_Support_Memory $memory The memory tracker.
51: * @param mixed $logger The logger. This must provide
52: * a debug() method.
53: */
54: public function __construct(Horde_Kolab_Format $handler,
55: Horde_Support_Memory $memory,
56: $logger = null)
57: {
58: parent::__construct($handler);
59: $this->_memory = $memory;
60: $this->_logger = $logger;
61: }
62:
63: /**
64: * Load an object based on the given XML stream.
65: *
66: * @param resource $xml The XML stream of the message.
67: * @param array $options Additional options when parsing the XML. This
68: * decorator provides no additional options.
69: *
70: * @return array The data array representing the object.
71: *
72: * @throws Horde_Kolab_Format_Exception
73: */
74: public function load($xml, $options = array())
75: {
76: $this->_memory->push();
77: $result = $this->getHandler()->load($xml);
78: $this->_logger->debug(
79: sprintf(
80: 'Kolab Format data parsing complete. Memory usage: %s',
81: $this->_formatUsage($this->_memory->pop())
82: )
83: );
84: return $result;
85: }
86:
87: /**
88: * Convert the data to a XML stream.
89: *
90: * @param array $object The data array representing the object.
91: * @param array $options Additional options when writing the XML. This
92: * decorator provides no additional options.
93: *
94: * @return resource The data as XML stream.
95: *
96: * @throws Horde_Kolab_Format_Exception
97: */
98: public function save($object, $options = array())
99: {
100: $this->_memory->push();
101: $result = $this->getHandler()->save($object);
102: $this->_logger->debug(
103: sprintf(
104: 'Kolab Format data generation complete. Memory usage: %s',
105: $this->_formatUsage($this->_memory->pop())
106: )
107: );
108: return $result;
109: }
110:
111: /**
112: * Format the memory usage information.
113: *
114: * @param array $usage The memory usage.
115: *
116: * @return string The formated memory usage.
117: */
118: private function _formatUsage($usage)
119: {
120: return sprintf(
121: '%.3f MB / %.3f MB / %.3f MB / %.3f MB [change in current usage (emalloc) / change in peak usage (emalloc) / change in current usage (real) / change in peak usage (real)]',
122: $usage[0] / 1048576,
123: $usage[1] / 1048576,
124: $usage[2] / 1048576,
125: $usage[3] / 1048576
126: );
127: }
128: }