1: <?php
2: /**
3: * PHPWord
4: *
5: * Copyright (c) 2013 PHPWord
6: *
7: * This library is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU Lesser General Public
9: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20: *
21: * @category PHPWord
22: * @package PHPWord
23: * @copyright Copyright (c) 2013 PHPWord
24: * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25: * @version 0.7.0
26: */
27:
28: if (!defined('DATE_W3C')) {
29: define('DATE_W3C', 'Y-m-d\TH:i:sP');
30: }
31:
32: /**
33: * Class PHPWord_Shared_XMLWriter
34: *
35: * @method bool startElement(string $name)
36: * @method bool writeAttribute(string $name, string $value)
37: * @method bool endElement()
38: */
39: class PHPWord_Shared_XMLWriter
40: {
41: /** Temporary storage method */
42: const STORAGE_MEMORY = 1;
43: const STORAGE_DISK = 2;
44:
45: /**
46: * Internal XMLWriter
47: *
48: * @var XMLWriter
49: */
50: private $_xmlWriter;
51:
52: /**
53: * Temporary filename
54: *
55: * @var string
56: */
57: private $_tempFileName = '';
58:
59: /**
60: * Create a new PHPPowerPoint_Shared_XMLWriter instance
61: *
62: * @param int $pTemporaryStorage Temporary storage location
63: * @param string $pTemporaryStorageFolder Temporary storage folder
64: */
65: public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = './')
66: {
67: // Create internal XMLWriter
68: $this->_xmlWriter = new XMLWriter();
69:
70: // Open temporary storage
71: if ($pTemporaryStorage == self::STORAGE_MEMORY) {
72: $this->_xmlWriter->openMemory();
73: } else {
74: // Create temporary filename
75: $this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
76:
77: // Open storage
78: if ($this->_xmlWriter->openUri($this->_tempFileName) === false) {
79: // Fallback to memory...
80: $this->_xmlWriter->openMemory();
81: }
82: }
83:
84: // Set default values
85: // proposed to be false in production version
86: $this->_xmlWriter->setIndent(true);
87: //$this->_xmlWriter->setIndent(false);
88:
89: // Set indent
90: // proposed to be '' in production version
91: $this->_xmlWriter->setIndentString(' ');
92: //$this->_xmlWriter->setIndentString('');
93: }
94:
95: /**
96: * Destructor
97: */
98: public function __destruct()
99: {
100: // Desctruct XMLWriter
101: unset($this->_xmlWriter);
102:
103: // Unlink temporary files
104: if ($this->_tempFileName != '') {
105: @unlink($this->_tempFileName);
106: }
107: }
108:
109: /**
110: * Get written data
111: *
112: * @return $data
113: */
114: public function getData()
115: {
116: if ($this->_tempFileName == '') {
117: return $this->_xmlWriter->outputMemory(true);
118: } else {
119: $this->_xmlWriter->flush();
120: return file_get_contents($this->_tempFileName);
121: }
122: }
123:
124: /**
125: * Catch function calls (and pass them to internal XMLWriter)
126: *
127: * @param unknown_type $function
128: * @param unknown_type $args
129: */
130: public function __call($function, $args)
131: {
132: try {
133: @call_user_func_array(array($this->_xmlWriter, $function), $args);
134: } catch (Exception $ex) {
135: // Do nothing!
136: }
137: }
138:
139: /**
140: * Fallback method for writeRaw, introduced in PHP 5.2
141: *
142: * @param string $text
143: * @return string
144: */
145: public function writeRaw($text)
146: {
147: if (isset($this->_xmlWriter) && is_object($this->_xmlWriter) && (method_exists($this->_xmlWriter, 'writeRaw'))) {
148: return $this->_xmlWriter->writeRaw($text);
149: }
150:
151: return $this->text($text);
152: }
153: }