Minor performance tweaks: changing PHPExcel_Shared_XMLWriter to extend the XMLWriter class rather than providing a wrapper for an instance of XMLWriter, with slight modifications to the writeRaw() method

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@63534 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker
2010-11-09 12:31:59 +00:00
parent d7583efe44
commit c0ae3911b6
5 changed files with 66 additions and 95 deletions
+57 -84
View File
@@ -19,10 +19,10 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel_Shared
* @package PHPExcel_Shared
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/
if (!defined('DATE_W3C')) {
@@ -38,21 +38,14 @@ if (!defined('DEBUGMODE_ENABLED')) {
* PHPExcel_Shared_XMLWriter
*
* @category PHPExcel
* @package PHPExcel_Shared
* @package PHPExcel_Shared
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Shared_XMLWriter {
class PHPExcel_Shared_XMLWriter extends XMLWriter {
/** Temporary storage method */
const STORAGE_MEMORY = 1;
const STORAGE_DISK = 2;
/**
* Internal XMLWriter
*
* @var XMLWriter
*/
private $_xmlWriter;
/**
* Temporary filename
*
@@ -66,83 +59,63 @@ class PHPExcel_Shared_XMLWriter {
* @param int $pTemporaryStorage Temporary storage location
* @param string $pTemporaryStorageFolder Temporary storage folder
*/
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = './') {
// Create internal XMLWriter
$this->_xmlWriter = new XMLWriter();
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = './') {
// Open temporary storage
if ($pTemporaryStorage == self::STORAGE_MEMORY) {
$this->openMemory();
} else {
// Create temporary filename
$this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
// Open temporary storage
if ($pTemporaryStorage == self::STORAGE_MEMORY) {
$this->_xmlWriter->openMemory();
} else {
// Create temporary filename
$this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
// Open storage
if ($this->openUri($this->_tempFileName) === false) {
// Fallback to memory...
$this->openMemory();
}
}
// Open storage
if ($this->_xmlWriter->openUri($this->_tempFileName) === false) {
// Fallback to memory...
$this->_xmlWriter->openMemory();
}
}
// Set default values
// Set default values
if (DEBUGMODE_ENABLED) {
$this->_xmlWriter->setIndent(true);
}
}
$this->setIndent(true);
}
}
/**
* Destructor
*/
public function __destruct() {
// Desctruct XMLWriter
unset($this->_xmlWriter);
/**
* Destructor
*/
public function __destruct() {
// Unlink temporary files
if ($this->_tempFileName != '') {
@unlink($this->_tempFileName);
}
}
// Unlink temporary files
if ($this->_tempFileName != '') {
@unlink($this->_tempFileName);
}
}
/**
* Get written data
*
* @return $data
*/
public function getData() {
if ($this->_tempFileName == '') {
return $this->outputMemory(true);
} else {
$this->flush();
return file_get_contents($this->_tempFileName);
}
}
/**
* Get written data
*
* @return $data
*/
public function getData() {
if ($this->_tempFileName == '') {
return $this->_xmlWriter->outputMemory(true);
} else {
$this->_xmlWriter->flush();
return file_get_contents($this->_tempFileName);
}
}
/**
* Fallback method for writeRaw, introduced in PHP 5.2
*
* @param string $text
* @return string
*/
public function writeRawData($text)
{
if (method_exists($this, 'writeRaw')) {
return $this->writeRaw(htmlspecialchars($text));
}
/**
* Catch function calls (and pass them to internal XMLWriter)
*
* @param unknown_type $function
* @param unknown_type $args
*/
public function __call($function, $args) {
try {
@call_user_func_array(array($this->_xmlWriter, $function), $args);
} catch (Exception $ex) {
// Do nothing!
}
}
/**
* Fallback method for writeRaw, introduced in PHP 5.2
*
* @param string $text
* @return string
*/
public function writeRaw($text)
{
if (isset($this->_xmlWriter) && is_object($this->_xmlWriter) && (method_exists($this->_xmlWriter, 'writeRaw'))) {
return $this->_xmlWriter->writeRaw(htmlspecialchars($text));
}
return $this->text($text);
}
return $this->text($text);
}
}