diff --git a/README.md b/README.md index e415834..4451ae9 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,7 @@ use pandaac\Exporter\Parsers; use pandaac\Exporter\Exporter; try { - $exporter = new Exporter( - '/home/pandaac/theforgottenserver' - ); + $exporter = new Exporter('/home/pandaac/theforgottenserver'); $response = $exporter->parse(new Parsers\Weapons); } catch (Exception $e) { @@ -37,6 +35,31 @@ try { } ``` +##### Settings +Optionally, you may pass through engine specific settings as the second argument of the `\pandaac\Exporter\Exporter` object. + +```php +$exporter = new Exporter('/home/pandaac/theforgottenserver', $settings); +``` + +Available settings are as follows: + +```php +$settings = [ + 'xml' => [ + // The XML engine will automatically validate any file it tries to parse, + // and if the data is invalid, an exception will be thrown. You may + // disable this behaviour by setting `validate` to `false`. + 'validate' => false, + + // The XML engine will not throw exceptions on missing files when + // parsing through a recursive structure. You may enable this + // behaviour by setting `strict` to `true`. + 'strict' => true, + ], +] +``` + ## Response Each parser returns an [Illuminate Collection](https://laravel.com/docs/5.4/collections) object. Please refer to the Laravel documentation for details on how to utilise it. diff --git a/src/Contracts/Parser.php b/src/Contracts/Parser.php index 2712f8b..63b84af 100644 --- a/src/Contracts/Parser.php +++ b/src/Contracts/Parser.php @@ -17,10 +17,11 @@ public function filePath(); /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes); + public function engine(Exporter $exporter, array $attributes); /** * Parse the file. diff --git a/src/Engines/OTBM.php b/src/Engines/OTBM.php index d4529a2..0dfc1b8 100644 --- a/src/Engines/OTBM.php +++ b/src/Engines/OTBM.php @@ -3,6 +3,7 @@ namespace pandaac\Exporter\Engines; use Exception; +use pandaac\Exporter\Exporter; use pandaac\Exporter\Contracts\Engine as Contract; class OTBM implements Contract diff --git a/src/Engines/XML.php b/src/Engines/XML.php index 9bb2d09..502bfd1 100644 --- a/src/Engines/XML.php +++ b/src/Engines/XML.php @@ -8,12 +8,20 @@ use pandaac\Exporter\Output; use InvalidArgumentException; use UnexpectedValueException; +use pandaac\Exporter\Exporter; use Illuminate\Support\Collection; use pandaac\Exporter\Contracts\Source; use pandaac\Exporter\Contracts\Engine as Contract; class XML implements Contract { + /** + * Holds the Exporter implementation. + * + * @var \pandaac\Exporter\Exporter + */ + protected $exporter; + /** * Holds the source. * @@ -99,12 +107,14 @@ class XML implements Contract /** * Instantiate the engine object. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes [] * @param array $plural [] * @return void */ - public function __construct(array $attributes = [], array $plural = []) + public function __construct(Exporter $exporter, array $attributes = [], array $plural = []) { + $this->exporter = $exporter; $this->attributes = $attributes; $this->plural = $plural; @@ -124,12 +134,16 @@ public function open($source) $this->source = $source; - $document = $source instanceof Source ? $this->openSource($source) : $this->openFile($source); + if (! ($source instanceof Source ? $this->openSource($source) : $this->openFile($source))) { + return false; + } $this->reader->setParserProperty(XMLReader::VALIDATE, true); if (! $this->reader->isValid()) { - throw new UnexpectedValueException('The source data is not valid.'); + $this->triggerException( + new UnexpectedValueException('The source data is not valid.') + ); } $this->reader->setParserProperty(XMLReader::VALIDATE, false); @@ -155,7 +169,11 @@ protected function openSource(Source $source) protected function openFile($source) { if (! is_file($source)) { - throw new InvalidArgumentException('The first argument must be a valid file.'); + if ($this->exporter->setting('xml.strict', false)) { + throw new InvalidArgumentException(sprintf('%s could not be found.', $source)); + } + + return; } return $this->reader->open($source, null); @@ -179,7 +197,9 @@ public function output() if (libxml_get_last_error() !== false) { $error = libxml_get_last_error(); - throw new ErrorException($error->message, $error->code, $error->level, $error->file, $error->line); + $this->triggerException( + new ErrorException($error->message, $error->code, $error->level, $error->file, $error->line) + ); } unset($this->references); @@ -367,4 +387,19 @@ protected function getMethodName($node) return 'read'.$this->nodes[$node].'Node'; } + + /** + * Throw an exception, unless it's being suppressed. + * + * @param \Exception $e + * @return void + */ + protected function triggerException(Exception $e) + { + if (! $this->exporter->setting('xml.validate', true)) { + return; + } + + return $this->exporter->triggerException($e); + } } diff --git a/src/Exporter.php b/src/Exporter.php index 1e9ea67..ad4e66d 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -2,7 +2,9 @@ namespace pandaac\Exporter; +use Exception; use LogicException; +use Illuminate\Support\Arr; use InvalidArgumentException; use UnexpectedValueException; use pandaac\Exporter\Contracts\Engine; @@ -16,7 +18,7 @@ class Exporter * * @var string */ - const VERSION = '2.0.1'; + const VERSION = '2.1.1'; /** * Set the git repository issues link. @@ -32,6 +34,13 @@ class Exporter */ protected $directory; + /** + * Holds the settings array. + * + * @var array + */ + protected $settings = []; + /** * Holds the output implementation. * @@ -43,11 +52,13 @@ class Exporter * Instantiate a new exporter object. * * @param string $directory + * @param array $settings [] * @return void */ - public function __construct($directory) + public function __construct($directory, array $settings = []) { $this->directory = $directory; + $this->settings = $settings; if (! file_exists($directory)) { throw new InvalidArgumentException('The first argument must be a valid directory.'); @@ -86,16 +97,20 @@ public function getAbsolutePath($source, $file = null) */ public function parse(Parser $parser, array $attributes = [], $file = null) { - $engine = $parser->engine($attributes); + $engine = $parser->engine($this, $attributes); if (! ($engine instanceof Engine)) { throw new LogicException('The provided engine must implement the pandaac\Exporter\Contracts\Engine interface.'); } - $engine->open( + $document = $engine->open( $this->getAbsolutePath($parser->filePath(), $file) ); + if ($document === false) { + return; + } + $response = $parser->parse($this, $this->output = $engine->output(), $attributes); $engine->close(); @@ -116,4 +131,30 @@ public function meta() return $this->output->meta(); } + + /** + * Retrieve the value of a specific setting. + * + * @param string $setting + * @return mixed + */ + public function setting($setting, $default = null) + { + return Arr::get($this->settings, $setting, $default); + } + + /** + * Throw an exception, unless it's being suppressed. + * + * @param \Exception $e + * @return void + */ + public function triggerException(Exception $e) + { + if ($e instanceof XMLException and ! $this->setting('xml.validate', true)) { + return; + } + + throw $e; + } } diff --git a/src/Parsers/Actions.php b/src/Parsers/Actions.php index 8262b70..dcce2c6 100644 --- a/src/Parsers/Actions.php +++ b/src/Parsers/Actions.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/ChatChannels.php b/src/Parsers/ChatChannels.php index ff64fb6..535df42 100644 --- a/src/Parsers/ChatChannels.php +++ b/src/Parsers/ChatChannels.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Commands.php b/src/Parsers/Commands.php index 6041daf..075c68d 100644 --- a/src/Parsers/Commands.php +++ b/src/Parsers/Commands.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/CreatureScripts.php b/src/Parsers/CreatureScripts.php index 17119ff..f63f70c 100644 --- a/src/Parsers/CreatureScripts.php +++ b/src/Parsers/CreatureScripts.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Events.php b/src/Parsers/Events.php index 73c2f36..e67352f 100644 --- a/src/Parsers/Events.php +++ b/src/Parsers/Events.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/GlobalEvents.php b/src/Parsers/GlobalEvents.php index 91f68ed..f627654 100644 --- a/src/Parsers/GlobalEvents.php +++ b/src/Parsers/GlobalEvents.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Groups.php b/src/Parsers/Groups.php index 3ce22e4..d5fd5c9 100644 --- a/src/Parsers/Groups.php +++ b/src/Parsers/Groups.php @@ -335,12 +335,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Items.php b/src/Parsers/Items.php index 4f76461..24ceaed 100644 --- a/src/Parsers/Items.php +++ b/src/Parsers/Items.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/MapHouses.php b/src/Parsers/MapHouses.php index 064d3ba..f7ffcec 100644 --- a/src/Parsers/MapHouses.php +++ b/src/Parsers/MapHouses.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/MapSpawns.php b/src/Parsers/MapSpawns.php index f661151..6a83ca7 100644 --- a/src/Parsers/MapSpawns.php +++ b/src/Parsers/MapSpawns.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Monster.php b/src/Parsers/Monster.php index a9cfc5b..49dbeaa 100644 --- a/src/Parsers/Monster.php +++ b/src/Parsers/Monster.php @@ -36,12 +36,13 @@ public function plural() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes, $this->plural()); + return new XML($exporter, $attributes, $this->plural()); } /** diff --git a/src/Parsers/Monsters.php b/src/Parsers/Monsters.php index f4982e6..0a7d6c6 100644 --- a/src/Parsers/Monsters.php +++ b/src/Parsers/Monsters.php @@ -24,12 +24,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Mounts.php b/src/Parsers/Mounts.php index 1b2d3ec..6f0fa85 100644 --- a/src/Parsers/Mounts.php +++ b/src/Parsers/Mounts.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Movements.php b/src/Parsers/Movements.php index b7a4e5a..6cb31e3 100644 --- a/src/Parsers/Movements.php +++ b/src/Parsers/Movements.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/NPC.php b/src/Parsers/NPC.php index 682b339..1b0d43a 100644 --- a/src/Parsers/NPC.php +++ b/src/Parsers/NPC.php @@ -22,12 +22,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Outfits.php b/src/Parsers/Outfits.php index de90fbd..5db286d 100644 --- a/src/Parsers/Outfits.php +++ b/src/Parsers/Outfits.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Quests.php b/src/Parsers/Quests.php index ff15ee0..805408c 100644 --- a/src/Parsers/Quests.php +++ b/src/Parsers/Quests.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Raid.php b/src/Parsers/Raid.php index 60dc722..e084811 100644 --- a/src/Parsers/Raid.php +++ b/src/Parsers/Raid.php @@ -22,12 +22,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Raids.php b/src/Parsers/Raids.php index a38a5ed..f27c883 100644 --- a/src/Parsers/Raids.php +++ b/src/Parsers/Raids.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Spells.php b/src/Parsers/Spells.php index 245754d..71f2c0b 100644 --- a/src/Parsers/Spells.php +++ b/src/Parsers/Spells.php @@ -22,12 +22,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Stages.php b/src/Parsers/Stages.php index 9e190a1..0b5688a 100644 --- a/src/Parsers/Stages.php +++ b/src/Parsers/Stages.php @@ -22,12 +22,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/TalkActions.php b/src/Parsers/TalkActions.php index e46e3a5..a38e4cb 100644 --- a/src/Parsers/TalkActions.php +++ b/src/Parsers/TalkActions.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Towns.php b/src/Parsers/Towns.php index 0e80d93..647dd3b 100644 --- a/src/Parsers/Towns.php +++ b/src/Parsers/Towns.php @@ -22,12 +22,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new OTBM($attributes); + return new OTBM($exporter, $attributes); } /** diff --git a/src/Parsers/Vocations.php b/src/Parsers/Vocations.php index d96e143..6b4b717 100644 --- a/src/Parsers/Vocations.php +++ b/src/Parsers/Vocations.php @@ -23,12 +23,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/Weapons.php b/src/Parsers/Weapons.php index 5b7062d..5fa3451 100644 --- a/src/Parsers/Weapons.php +++ b/src/Parsers/Weapons.php @@ -22,12 +22,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /** diff --git a/src/Parsers/XMLSource.php b/src/Parsers/XMLSource.php index dcb090d..d8a5cf4 100644 --- a/src/Parsers/XMLSource.php +++ b/src/Parsers/XMLSource.php @@ -34,12 +34,13 @@ public function filePath() /** * Get the parser engine. * + * @param \pandaac\Exporter\Exporter $exporter * @param array $attributes * @return \pandaac\Exporter\Contracts\Engine */ - public function engine(array $attributes) + public function engine(Exporter $exporter, array $attributes) { - return new XML($attributes); + return new XML($exporter, $attributes); } /**