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

View File

@ -41,18 +41,11 @@ if (!defined('DEBUGMODE_ENABLED')) {
* @package PHPExcel_Shared * @package PHPExcel_Shared
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel) * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
*/ */
class PHPExcel_Shared_XMLWriter { class PHPExcel_Shared_XMLWriter extends XMLWriter {
/** Temporary storage method */ /** Temporary storage method */
const STORAGE_MEMORY = 1; const STORAGE_MEMORY = 1;
const STORAGE_DISK = 2; const STORAGE_DISK = 2;
/**
* Internal XMLWriter
*
* @var XMLWriter
*/
private $_xmlWriter;
/** /**
* Temporary filename * Temporary filename
* *
@ -67,26 +60,23 @@ class PHPExcel_Shared_XMLWriter {
* @param string $pTemporaryStorageFolder Temporary storage folder * @param string $pTemporaryStorageFolder Temporary storage folder
*/ */
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = './') { public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = './') {
// Create internal XMLWriter
$this->_xmlWriter = new XMLWriter();
// Open temporary storage // Open temporary storage
if ($pTemporaryStorage == self::STORAGE_MEMORY) { if ($pTemporaryStorage == self::STORAGE_MEMORY) {
$this->_xmlWriter->openMemory(); $this->openMemory();
} else { } else {
// Create temporary filename // Create temporary filename
$this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml'); $this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
// Open storage // Open storage
if ($this->_xmlWriter->openUri($this->_tempFileName) === false) { if ($this->openUri($this->_tempFileName) === false) {
// Fallback to memory... // Fallback to memory...
$this->_xmlWriter->openMemory(); $this->openMemory();
} }
} }
// Set default values // Set default values
if (DEBUGMODE_ENABLED) { if (DEBUGMODE_ENABLED) {
$this->_xmlWriter->setIndent(true); $this->setIndent(true);
} }
} }
@ -94,9 +84,6 @@ class PHPExcel_Shared_XMLWriter {
* Destructor * Destructor
*/ */
public function __destruct() { public function __destruct() {
// Desctruct XMLWriter
unset($this->_xmlWriter);
// Unlink temporary files // Unlink temporary files
if ($this->_tempFileName != '') { if ($this->_tempFileName != '') {
@unlink($this->_tempFileName); @unlink($this->_tempFileName);
@ -110,37 +97,23 @@ class PHPExcel_Shared_XMLWriter {
*/ */
public function getData() { public function getData() {
if ($this->_tempFileName == '') { if ($this->_tempFileName == '') {
return $this->_xmlWriter->outputMemory(true); return $this->outputMemory(true);
} else { } else {
$this->_xmlWriter->flush(); $this->flush();
return file_get_contents($this->_tempFileName); return file_get_contents($this->_tempFileName);
} }
} }
/**
* 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 * Fallback method for writeRaw, introduced in PHP 5.2
* *
* @param string $text * @param string $text
* @return string * @return string
*/ */
public function writeRaw($text) public function writeRawData($text)
{ {
if (isset($this->_xmlWriter) && is_object($this->_xmlWriter) && (method_exists($this->_xmlWriter, 'writeRaw'))) { if (method_exists($this, 'writeRaw')) {
return $this->_xmlWriter->writeRaw(htmlspecialchars($text)); return $this->writeRaw(htmlspecialchars($text));
} }
return $this->text($text); return $this->text($text);

View File

@ -169,13 +169,13 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write
// dcterms:created // dcterms:created
$objWriter->startElement('dcterms:created'); $objWriter->startElement('dcterms:created');
$objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
$objWriter->writeRaw(date(DATE_W3C, $pPHPExcel->getProperties()->getCreated())); $objWriter->writeRawData(date(DATE_W3C, $pPHPExcel->getProperties()->getCreated()));
$objWriter->endElement(); $objWriter->endElement();
// dcterms:modified // dcterms:modified
$objWriter->startElement('dcterms:modified'); $objWriter->startElement('dcterms:modified');
$objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
$objWriter->writeRaw(date(DATE_W3C, $pPHPExcel->getProperties()->getModified())); $objWriter->writeRawData(date(DATE_W3C, $pPHPExcel->getProperties()->getModified()));
$objWriter->endElement(); $objWriter->endElement();
// dc:title // dc:title
@ -251,7 +251,7 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write
break; break;
case 'd' : case 'd' :
$objWriter->startElement('vt:filetime'); $objWriter->startElement('vt:filetime');
$objWriter->writeRaw(date(DATE_W3C, $propertyValue)); $objWriter->writeRawData(date(DATE_W3C, $propertyValue));
$objWriter->endElement(); $objWriter->endElement();
break; break;
default : default :

View File

@ -123,7 +123,7 @@ class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_Wr
if ($textToWrite !== trim($textToWrite)) { if ($textToWrite !== trim($textToWrite)) {
$objWriter->writeAttribute('xml:space', 'preserve'); $objWriter->writeAttribute('xml:space', 'preserve');
} }
$objWriter->writeRaw($textToWrite); $objWriter->writeRawData($textToWrite);
$objWriter->endElement(); $objWriter->endElement();
} else if ($textElement instanceof PHPExcel_RichText) { } else if ($textElement instanceof PHPExcel_RichText) {
$this->writeRichText($objWriter, $textElement); $this->writeRichText($objWriter, $textElement);
@ -213,7 +213,7 @@ class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_Wr
// t // t
$objWriter->startElement('t'); $objWriter->startElement('t');
$objWriter->writeAttribute('xml:space', 'preserve'); $objWriter->writeAttribute('xml:space', 'preserve');
$objWriter->writeRaw(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $element->getText() )); $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $element->getText() ));
$objWriter->endElement(); $objWriter->endElement();
$objWriter->endElement(); $objWriter->endElement();

View File

@ -328,7 +328,7 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_Write
} }
$range = PHPExcel_Cell::buildRange($range); $range = PHPExcel_Cell::buildRange($range);
$objWriter->writeRaw($range); $objWriter->writeRawData($range);
$objWriter->endElement(); $objWriter->endElement();
} }
@ -357,7 +357,7 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_Write
$range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]); $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]);
$range = implode(':', $range); $range = implode(':', $range);
$objWriter->writeRaw('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range); $objWriter->writeRawData('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range);
$objWriter->endElement(); $objWriter->endElement();
} }
@ -400,7 +400,7 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_Write
$settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1]; $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1];
} }
$objWriter->writeRaw($settingString); $objWriter->writeRawData($settingString);
$objWriter->endElement(); $objWriter->endElement();
} }
@ -435,7 +435,7 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_Write
$chunks[] = '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . implode(':', $printAreaRect); $chunks[] = '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . implode(':', $printAreaRect);
} }
$objWriter->writeRaw(implode(',', $chunks)); $objWriter->writeRawData(implode(',', $chunks));
$objWriter->endElement(); $objWriter->endElement();
} }

View File

@ -965,10 +965,8 @@ class PHPExcel_Writer_Excel2007_Worksheet extends PHPExcel_Writer_Excel2007_Writ
case 'f': // Formula case 'f': // Formula
$calculatedValue = null; $calculatedValue = null;
if ($this->getParentWriter()->getPreCalculateFormulas()) { if ($this->getParentWriter()->getPreCalculateFormulas()) {
$pCell->attach($pSheet);
$calculatedValue = $pCell->getCalculatedValue(); $calculatedValue = $pCell->getCalculatedValue();
} else { } else {
$pCell->attach($pSheet);
$calculatedValue = $pCell->getValue(); $calculatedValue = $pCell->getValue();
} }
if (is_string($calculatedValue)) { if (is_string($calculatedValue)) {