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
2 changes: 1 addition & 1 deletion src/KmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function loadFromString(string $content): self
$this->xml->registerXPathNamespace('kml', $this->namespace);

return $this;
} catch (\Exception $e) {
} catch (Exception $e) {
libxml_clear_errors();
throw KmlParserException::failedToParse($e->getMessage());
}
Expand Down
8 changes: 7 additions & 1 deletion src/KmlParserServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ public function configurePackage(Package $package): void

public function packageRegistered(): void
{
$this->app->singleton(KmlParser::class, function () {
/*
* The parser keeps the loaded document in memory, so a singleton would
* leak that state across requests under Octane and across jobs in a
* long running queue worker. A scoped binding is resolved once per
* request/job lifecycle and flushed in between.
*/
$this->app->scoped(KmlParser::class, function () {
return new KmlParser;
});
}
Expand Down
26 changes: 26 additions & 0 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use PlinCode\KmlParser\Exceptions\KmlParserException;
use PlinCode\KmlParser\KmlParser;

it('reuses the same parser instance within a single scope', function () {
$parser = app(KmlParser::class);

expect(app(KmlParser::class))->toBe($parser);
});

it('resolves a fresh parser once the scope is flushed', function () {
$parser = app(KmlParser::class);

app()->forgetScopedInstances();

expect(app(KmlParser::class))->not->toBe($parser);
});

it('does not leak a loaded document across scopes', function () {
app(KmlParser::class)->loadFromFile(__DIR__.'/files/kml-example/base.kml');

app()->forgetScopedInstances();

app(KmlParser::class)->getPlacemarks();
})->throws(KmlParserException::class, 'No KML data loaded');