fix(parser): work without a booted application - #23
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The second argument to
config()looks like a safe fallback. It is not. The helper resolvesconfigout of the container first, and when nothing is bound there it does not fall back, it throws:So
new KmlParseris fatal anywhere outside a booted application: a console script, a plain PHPUnit test that does not boot Laravel, a queue bootstrap that has not resolved the config repository yet.There is a second version of the same problem.
composer.jsonrequiresilluminate/contracts, notilluminate/support. Theconfig()andapp()helpers live inilluminate/support, so on an install that pulls only what this package declares, the helper is not defined at all and the constructor is a fatalCall to undefined function.Fix
A
ReadsPackageConfigtrait with one method:The container is consulted only once something is actually bound to it. Otherwise the documented default is returned, which is what the second argument to
config()always looked like it was doing.Inside a real application nothing changes:
configis bound, the helper runs, published config values win exactly as before.The trait lives next to
ParsesCoordinatesand is deliberately separate fromKmlParser, becauseKmzExtractorneeds the same thing as soon astemp_directoryis implemented.Tests
tests/StandaloneUsageTest.phpswaps in a bareIlluminate\Container\Containerwith nothing bound, runs the parser, and restores the real application in afinally:earth.google.com/kml/2.1document still loadssupported_namespacesvalue set in the config is still honoured, so the fallback did not replace the real lookupThe first three fail on
mainwithBindingResolutionException. The fourth passes on both, and is there to prove the guard did not turn config reading off.Suite 65 to 69. PHPStan and Pint clean.
Note
The
function_exists()half of the guard cannot be exercised from this suite, since both helpers are always defined once testbench is loaded. It stays because theilluminate/contracts-only install is a real shape a consumer can end up in, and the check costs nothing.