From c0ae3911b68be09ea06b46afab98f0aa08db3130 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Tue, 9 Nov 2010 12:31:59 +0000 Subject: [PATCH] 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 --- Classes/PHPExcel/Shared/XMLWriter.php | 141 +++++++----------- .../PHPExcel/Writer/Excel2007/DocProps.php | 6 +- .../PHPExcel/Writer/Excel2007/StringTable.php | 4 +- .../PHPExcel/Writer/Excel2007/Workbook.php | 8 +- .../PHPExcel/Writer/Excel2007/Worksheet.php | 2 - 5 files changed, 66 insertions(+), 95 deletions(-) diff --git a/Classes/PHPExcel/Shared/XMLWriter.php b/Classes/PHPExcel/Shared/XMLWriter.php index e9db08d6..f091122e 100644 --- a/Classes/PHPExcel/Shared/XMLWriter.php +++ b/Classes/PHPExcel/Shared/XMLWriter.php @@ -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); + } } diff --git a/Classes/PHPExcel/Writer/Excel2007/DocProps.php b/Classes/PHPExcel/Writer/Excel2007/DocProps.php index 249d5575..971e2f29 100644 --- a/Classes/PHPExcel/Writer/Excel2007/DocProps.php +++ b/Classes/PHPExcel/Writer/Excel2007/DocProps.php @@ -169,13 +169,13 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write // dcterms:created $objWriter->startElement('dcterms:created'); $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); - $objWriter->writeRaw(date(DATE_W3C, $pPHPExcel->getProperties()->getCreated())); + $objWriter->writeRawData(date(DATE_W3C, $pPHPExcel->getProperties()->getCreated())); $objWriter->endElement(); // dcterms:modified $objWriter->startElement('dcterms:modified'); $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); - $objWriter->writeRaw(date(DATE_W3C, $pPHPExcel->getProperties()->getModified())); + $objWriter->writeRawData(date(DATE_W3C, $pPHPExcel->getProperties()->getModified())); $objWriter->endElement(); // dc:title @@ -251,7 +251,7 @@ class PHPExcel_Writer_Excel2007_DocProps extends PHPExcel_Writer_Excel2007_Write break; case 'd' : $objWriter->startElement('vt:filetime'); - $objWriter->writeRaw(date(DATE_W3C, $propertyValue)); + $objWriter->writeRawData(date(DATE_W3C, $propertyValue)); $objWriter->endElement(); break; default : diff --git a/Classes/PHPExcel/Writer/Excel2007/StringTable.php b/Classes/PHPExcel/Writer/Excel2007/StringTable.php index 112a6131..05e9da53 100644 --- a/Classes/PHPExcel/Writer/Excel2007/StringTable.php +++ b/Classes/PHPExcel/Writer/Excel2007/StringTable.php @@ -123,7 +123,7 @@ class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_Wr if ($textToWrite !== trim($textToWrite)) { $objWriter->writeAttribute('xml:space', 'preserve'); } - $objWriter->writeRaw($textToWrite); + $objWriter->writeRawData($textToWrite); $objWriter->endElement(); } else if ($textElement instanceof PHPExcel_RichText) { $this->writeRichText($objWriter, $textElement); @@ -213,7 +213,7 @@ class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_Wr // t $objWriter->startElement('t'); $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(); diff --git a/Classes/PHPExcel/Writer/Excel2007/Workbook.php b/Classes/PHPExcel/Writer/Excel2007/Workbook.php index 0eed3568..9df6c67c 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Workbook.php +++ b/Classes/PHPExcel/Writer/Excel2007/Workbook.php @@ -328,7 +328,7 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_Write } $range = PHPExcel_Cell::buildRange($range); - $objWriter->writeRaw($range); + $objWriter->writeRawData($range); $objWriter->endElement(); } @@ -357,7 +357,7 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_Write $range[1] = PHPExcel_Cell::absoluteCoordinate($range[1]); $range = implode(':', $range); - $objWriter->writeRaw('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range); + $objWriter->writeRawData('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range); $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]; } - $objWriter->writeRaw($settingString); + $objWriter->writeRawData($settingString); $objWriter->endElement(); } @@ -435,7 +435,7 @@ class PHPExcel_Writer_Excel2007_Workbook extends PHPExcel_Writer_Excel2007_Write $chunks[] = '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . implode(':', $printAreaRect); } - $objWriter->writeRaw(implode(',', $chunks)); + $objWriter->writeRawData(implode(',', $chunks)); $objWriter->endElement(); } diff --git a/Classes/PHPExcel/Writer/Excel2007/Worksheet.php b/Classes/PHPExcel/Writer/Excel2007/Worksheet.php index e2c8773c..6c74a3dd 100644 --- a/Classes/PHPExcel/Writer/Excel2007/Worksheet.php +++ b/Classes/PHPExcel/Writer/Excel2007/Worksheet.php @@ -965,10 +965,8 @@ class PHPExcel_Writer_Excel2007_Worksheet extends PHPExcel_Writer_Excel2007_Writ case 'f': // Formula $calculatedValue = null; if ($this->getParentWriter()->getPreCalculateFormulas()) { - $pCell->attach($pSheet); $calculatedValue = $pCell->getCalculatedValue(); } else { - $pCell->attach($pSheet); $calculatedValue = $pCell->getValue(); } if (is_string($calculatedValue)) {