Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
XMLWriter : Refactoring for improving performances
  • Loading branch information
Progi1984 committed Feb 15, 2016
1 parent 8f46114 commit cfeb9d7
Showing 1 changed file with 17 additions and 47 deletions.
64 changes: 17 additions & 47 deletions src/Common/XMLWriter.php
Expand Up @@ -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
*
Expand All @@ -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(' ');
}
}

Expand All @@ -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) {
Expand All @@ -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
*
Expand All @@ -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);
}
}
Expand All @@ -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();
}

/**
Expand All @@ -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();
}
}
}
Expand All @@ -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.