Skip to content

Commit

Permalink
Engine specific settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Eklund committed May 8, 2017
1 parent 4622d29 commit e5ac381
Show file tree
Hide file tree
Showing 31 changed files with 192 additions and 65 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,39 @@ 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) {
// Handle exceptions as you see fit...
}
```

##### 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.

Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/Engines/OTBM.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 40 additions & 5 deletions src/Engines/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
49 changes: 45 additions & 4 deletions src/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace pandaac\Exporter;

use Exception;
use LogicException;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use UnexpectedValueException;
use pandaac\Exporter\Contracts\Engine;
Expand All @@ -16,7 +18,7 @@ class Exporter
*
* @var string
*/
const VERSION = '2.0.1';
const VERSION = '2.1.1';

/**
* Set the git repository issues link.
Expand All @@ -32,6 +34,13 @@ class Exporter
*/
protected $directory;

/**
* Holds the settings array.
*
* @var array
*/
protected $settings = [];

/**
* Holds the output implementation.
*
Expand All @@ -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.');
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/Parsers/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parsers/ChatChannels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parsers/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parsers/CreatureScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parsers/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parsers/GlobalEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parsers/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parsers/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Loading

0 comments on commit e5ac381

Please sign in to comment.