Skip to content

Commit c2f6631

Browse files
committed
feat: 🔧 Resolve container-bound providers instead of via reflection.
1 parent 52ccb5d commit c2f6631

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎config/geocoder.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@
5353
| can explicitly call subsequently listed providers by
5454
| alias: `app('geocoder')->using('google_maps')`.
5555
|
56+
| Providers that require a static factory method (e.g. `Nominatim::
57+
| withOpenStreetMapServer()`, `GoogleMaps::business()`) can be bound
58+
| in your AppServiceProvider — they'll be resolved via the container.
59+
| Configure the provider here with an empty args array:
60+
|
61+
| // AppServiceProvider::register()
62+
| $this->app->bind(Nominatim::class, fn ($app) => Nominatim::
63+
| withOpenStreetMapServer($app->make(LaravelHttpClient::class),
64+
| 'MyApp/1.0'));
65+
|
66+
| // config/geocoder.php
67+
| Nominatim::class => [],
68+
|
5669
| Please consult the official Geocoder documentation for more info.
5770
| https://github.com/geocoder-php/Geocoder#providers
5871
|

‎src/ProviderAndDumperAggregator.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Geocoder\Dumper\Wkb;
1616
use Geocoder\Dumper\Wkt;
1717
use Geocoder\Laravel\Exceptions\InvalidDumperException;
18+
use Geocoder\Provider\Chain\Chain;
1819
use Geocoder\ProviderAggregator;
1920
use Geocoder\Query\GeocodeQuery;
2021
use Geocoder\Query\ReverseQuery;
@@ -295,6 +296,10 @@ protected function getArguments(array $arguments, string $provider) : array
295296
protected function getProvidersFromConfiguration(Collection $providers) : array
296297
{
297298
$providers = $providers->map(function ($arguments, $provider) {
299+
if (app()->bound($provider)) {
300+
return app($provider);
301+
}
302+
298303
$arguments = $this->getArguments($arguments, $provider);
299304
$reflection = new ReflectionClass($provider);
300305

‎tests/Feature/Providers/GeocoderServiceTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,26 @@
272272
expect($result->getStreetNumber())->toBe('1600');
273273
});
274274

275+
it('resolves providers from the service container when bound', function () {
276+
app()->bind(Nominatim::class, fn () => Nominatim::withOpenStreetMapServer(
277+
app(LaravelHttpClient::class),
278+
'ContainerBoundAgent/1.0',
279+
));
280+
config()->set('geocoder.providers', [
281+
Nominatim::class => [],
282+
]);
283+
284+
$result = app('geocoder')
285+
->using('nominatim')
286+
->geocode('1600 Pennsylvania Ave NW, Washington, DC 20500, USA')
287+
->get()
288+
->first();
289+
290+
expect($result)->not->toBeNull();
291+
expect($result->getStreetNumber())->toBe('1600');
292+
Http::assertSent(fn ($request) => $request->header('User-Agent')[0] === 'ContainerBoundAgent/1.0');
293+
});
294+
275295
it('does not collide reverse cache keys across coordinate signs', function () {
276296
$providerName = app('geocoder')->getProvider()->getName();
277297
$negativeKey = sha1("{$providerName}-" . strtolower(urlencode('-45.473282--73.834721')));

0 commit comments

Comments
 (0)