Permalink
Browse files

XMLWriter : Refactoring for improving performances

  • Loading branch information...
Progi1984 committed Feb 15, 2016
1 parent 8f46114 commit cfeb9d71c09e38441d5ec06f21855733d0c71077
Showing with 17 additions and 47 deletions.
  1. +17 −47 src/Common/XMLWriter.php
@@ -33,19 +33,12 @@
* @method bool writeElement(string $name, string $content = null)
* @method bool writeRaw(string $content)
*/
class XMLWriter
class XMLWriter extends \XMLWriter
{
/** Temporary storage method */
const STORAGE_MEMORY = 1;
const STORAGE_DISK = 2;
/**
* Internal XMLWriter
*
* @var \XMLWriter
*/
private $xmlWriter;
/**
* Temporary filename
*
@@ -61,26 +54,23 @@ class XMLWriter
*/
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = './', $compatibility = false)
{
// Create internal XMLWriter
$this->xmlWriter = new \XMLWriter();
// Open temporary storage
if ($pTemporaryStorage == self::STORAGE_MEMORY) {
$this->xmlWriter->openMemory();
$this->openMemory();
} else {
// Create temporary filename
$this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
// Open storage
$this->xmlWriter->openUri($this->tempFileName);
$this->openUri($this->tempFileName);
}
if ($compatibility) {
$this->xmlWriter->setIndent(false);
$this->xmlWriter->setIndentString('');
$this->setIndent(false);
$this->setIndentString('');
} else {
$this->xmlWriter->setIndent(true);
$this->xmlWriter->setIndentString(' ');
$this->setIndent(true);
$this->setIndentString(' ');
}
}
@@ -89,9 +79,6 @@ public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTempora
*/
public function __destruct()
{
// Desctruct XMLWriter
unset($this->xmlWriter);
// Unlink temporary files
if ($this->tempFileName != '') {
if (@unlink($this->tempFileName) === false) {
@@ -100,23 +87,6 @@ public function __destruct()
}
}
/**
* Catch function calls (and pass them to internal XMLWriter)
*
* @param mixed $function
* @param mixed $args
*/
public function __call($function, $args)
{
try {
if (@call_user_func_array(array($this->xmlWriter, $function), $args) === false) {
throw new \Exception('The method '.$function.' doesn\'t exist.');
}
} catch (\Exception $ex) {
// Do nothing!
}
}
/**
* Get written data
*
@@ -125,9 +95,9 @@ public function __call($function, $args)
public function getData()
{
if ($this->tempFileName == '') {
return $this->xmlWriter->outputMemory(true);
return $this->outputMemory(true);
} else {
$this->xmlWriter->flush();
$this->flush();
return file_get_contents($this->tempFileName);
}
}
@@ -147,14 +117,14 @@ public function getData()
*/
public function writeElementBlock($element, $attributes, $value = null)
{
$this->xmlWriter->startElement($element);
$this->startElement($element);
if (!is_array($attributes)) {
$attributes = array($attributes => $value);
}
foreach ($attributes as $attribute => $value) {
$this->xmlWriter->writeAttribute($attribute, $value);
$this->writeAttribute($attribute, $value);
}
$this->xmlWriter->endElement();
$this->endElement();
}
/**
@@ -170,11 +140,11 @@ public function writeElementIf($condition, $element, $attribute = null, $value =
{
if ($condition == true) {
if (is_null($attribute)) {
$this->xmlWriter->writeElement($element, $value);
$this->writeElement($element, $value);
} else {
$this->xmlWriter->startElement($element);
$this->xmlWriter->writeAttribute($attribute, $value);
$this->xmlWriter->endElement();
$this->startElement($element);
$this->writeAttribute($attribute, $value);
$this->endElement();
}
}
}
@@ -190,7 +160,7 @@ public function writeElementIf($condition, $element, $attribute = null, $value =
public function writeAttributeIf($condition, $attribute, $value)
{
if ($condition == true) {
$this->xmlWriter->writeAttribute($attribute, $value);
$this->writeAttribute($attribute, $value);
}
}
}

0 comments on commit cfeb9d7

Please sign in to comment.