Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/KmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use PlinCode\KmlParser\Enums\GeometryType;
use PlinCode\KmlParser\Exceptions\KmlParserException;
use PlinCode\KmlParser\Traits\ParsesCoordinates;
use PlinCode\KmlParser\Traits\ReadsPackageConfig;
use PlinCode\KmlParser\Validators\KmlValidator;
use SimpleXMLElement;

class KmlParser
{
use ParsesCoordinates;
use ReadsPackageConfig;

protected ?SimpleXMLElement $xml = null;

Expand All @@ -21,7 +23,7 @@ class KmlParser

public function __construct()
{
$this->namespace = config('kml-parser.namespace', $this->namespace);
$this->namespace = $this->packageConfig('kml-parser.namespace', $this->namespace);
$this->validator = new KmlValidator($this->supportedNamespaces());
}

Expand All @@ -33,7 +35,7 @@ public function __construct()
*/
protected function supportedNamespaces(): array
{
$supported = config('kml-parser.supported_namespaces', KmlValidator::DEFAULT_NAMESPACES);
$supported = $this->packageConfig('kml-parser.supported_namespaces', KmlValidator::DEFAULT_NAMESPACES);

return array_values(array_unique(array_merge([$this->namespace], (array) $supported)));
}
Expand Down
24 changes: 24 additions & 0 deletions src/Traits/ReadsPackageConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace PlinCode\KmlParser\Traits;

trait ReadsPackageConfig
{
/**
* Read a package config value, falling back when there is no application.
*
* The parser and the extractor are both useful outside a booted Laravel
* application: a console script, a plain PHPUnit test, a queue bootstrap
* that has not resolved the config repository yet. The config() helper
* does not degrade there, it throws BindingResolutionException, so the
* container is only consulted once something is actually bound to it.
*/
protected function packageConfig(string $key, mixed $default): mixed
{
if (! function_exists('config') || ! function_exists('app') || ! app()->bound('config')) {
return $default;
}

return config($key, $default);
}
}
81 changes: 81 additions & 0 deletions tests/StandaloneUsageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

use Illuminate\Container\Container;
use PlinCode\KmlParser\KmlParser;

function withoutBoundConfig(callable $callback): mixed
{
$application = Container::getInstance();

Container::setInstance(new Container);

try {
return $callback();
} finally {
Container::setInstance($application);
}
}

$kml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Standalone</name>
<Placemark>
<name>Lago Blu</name>
<Point>
<coordinates>7.7300965,45.8635629,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
XML;

it('can be constructed with no config repository bound', function () {
expect(withoutBoundConfig(fn () => new KmlParser))->toBeInstanceOf(KmlParser::class);
});

it('parses with no config repository bound', function () use ($kml) {
$placemarks = withoutBoundConfig(fn () => (new KmlParser)->loadFromString($kml)->getPlacemarks());

expect($placemarks)->toHaveCount(1)
->and($placemarks[0]['name'])->toBe('Lago Blu');
});

it('falls back to the default namespaces with no config repository bound', function () {
$legacy = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<Placemark>
<Point>
<coordinates>7.7,45.8,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
XML;

$placemarks = withoutBoundConfig(fn () => (new KmlParser)->loadFromString($legacy)->getPlacemarks());

expect($placemarks)->toHaveCount(1);
});

it('still reads the config when the application provides one', function () {
config()->set('kml-parser.supported_namespaces', ['http://example.test/kml']);

$kml = str_replace('http://www.opengis.net/kml/2.2', 'http://example.test/kml', <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<Point>
<coordinates>7.7,45.8,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
XML);

expect((new KmlParser)->loadFromString($kml)->getPlacemarks())->toHaveCount(1);
});