From 15554e51e33f684c3e28c2065f940f6af0bc4ca2 Mon Sep 17 00:00:00 2001 From: Daniele Barbaro Date: Tue, 8 Sep 2026 15:42:21 +0200 Subject: [PATCH] fix(parser): work without a booted application The constructor called the config() helper directly. That helper does not degrade when no application is running, it resolves 'config' out of the container and throws BindingResolutionException, so `new KmlParser` was fatal in a console script, a plain PHPUnit test, or anything else outside a booted Laravel app. The package requires illuminate/contracts rather than illuminate/support, so on a bare install the helper may not even be defined. Config is now read through packageConfig(), which consults the container only once something is bound to it and otherwise returns the documented default. Behaviour inside an application is unchanged. --- src/KmlParser.php | 6 ++- src/Traits/ReadsPackageConfig.php | 24 +++++++++ tests/StandaloneUsageTest.php | 81 +++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/Traits/ReadsPackageConfig.php create mode 100644 tests/StandaloneUsageTest.php diff --git a/src/KmlParser.php b/src/KmlParser.php index aced1e4..1061449 100755 --- a/src/KmlParser.php +++ b/src/KmlParser.php @@ -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; @@ -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()); } @@ -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))); } diff --git a/src/Traits/ReadsPackageConfig.php b/src/Traits/ReadsPackageConfig.php new file mode 100644 index 0000000..2110513 --- /dev/null +++ b/src/Traits/ReadsPackageConfig.php @@ -0,0 +1,24 @@ +bound('config')) { + return $default; + } + + return config($key, $default); + } +} diff --git a/tests/StandaloneUsageTest.php b/tests/StandaloneUsageTest.php new file mode 100644 index 0000000..9fb60d4 --- /dev/null +++ b/tests/StandaloneUsageTest.php @@ -0,0 +1,81 @@ + + + + Standalone + + Lago Blu + + 7.7300965,45.8635629,0 + + + + +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' + + + + + + 7.7,45.8,0 + + + + +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' + + + + + + 7.7,45.8,0 + + + + +XML); + + expect((new KmlParser)->loadFromString($kml)->getPlacemarks())->toHaveCount(1); +});