Skip to content

Commit

Permalink
Merge pull request #41 from wikimedia/svg-warnings
Browse files Browse the repository at this point in the history
Tweak XML warning handling
  • Loading branch information
samwilson committed Jan 3, 2019
2 parents a896528 + b9c7ccb commit f4554df
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/Exception/SvgLoadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types = 1);

namespace App\Exception;

use Exception;
use Throwable;

/**
* For errors during SVG file loading and parsing
* Accepts custom error messages or uses last system error from LibXML
*/
class SvgLoadException extends Exception
{
public function __construct(string $message = '', ?Throwable $previous = null)
{
$error = error_get_last();
if ($error && isset($error['message'])) {
$message = $error['message'];
}
parent::__construct($message, 0, $previous);
}
}
9 changes: 5 additions & 4 deletions src/Model/Svg/SvgFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace App\Model\Svg;

use App\Exception\SvgLoadException;
use DOMDocument;
use DOMElement;
use DOMNode;
Expand Down Expand Up @@ -57,7 +58,7 @@ class SvgFile
* Construct an SvgFile object.
*
* @param string $path
* @todo Handle DOM warnings
* @throws SvgLoadException
*/
public function __construct(string $path)
{
Expand All @@ -66,10 +67,10 @@ public function __construct(string $path)
$this->document = new DOMDocument('1.0');

// Warnings need to be suppressed in case there are DOM warnings
//wfSuppressWarnings();
$this->document->load($path);
if (!$this->document->load($path, LIBXML_NOWARNING)) {
throw new SvgLoadException();
}
$this->xpath = new DOMXpath($this->document);
//wfRestoreWarnings();

$this->xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg');

Expand Down

0 comments on commit f4554df

Please sign in to comment.