8000 Merge pull request #125 from anam-hossain/master · geocoder-php/GeocoderLaravel@eabbf2a · GitHub
[go: up one dir, main page]

Skip to content

Commit eabbf2a

Browse files
authored
Merge pull request #125 from anam-hossain/master
Resolve Geocoder dependency via Class name
2 parents 9b899cf + acaccc0 commit eabbf2a

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@ app('geocoder')->reverse(43.882587,-103.454067)->get();
234234
app('geocoder')->geocode('Los Angeles, CA')->dump('kml');
235235
```
236236

237+
#### Using Controller
238+
```php
239+
use Geocoder\Laravel\ProviderAndDumperAggregator as Geocoder;
240+
241+
class GeocoderController extends Controller
242+
{
243+
public function getGeocode(Geocoder $geocoder)
244+
{
245+
$geocoder->geocode('Los Angeles, CA')->get()
246+
}
247+
}
248+
```
249+
237250
## Upgrading
238251
Anytime you upgrade this package, please remember to clear your cache, to prevent incompatible cached responses when breaking changes are introduced (this should hopefully only be necessary in major versions):
239252
```sh

src/Providers/GeocoderService.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
use Geocoder\Laravel\Facades\Geocoder;
1313
use Geocoder\Laravel\ProviderAndDumperAggregator;
14-
use Illuminate\Support\Collection;
1514
use Illuminate\Support\ServiceProvider;
16-
use ReflectionClass;
1715

1816
class GeocoderService extends ServiceProvider
1917
{
@@ -27,23 +25,30 @@ public function boot()
2725
"config"
2826
);
2927
$this->mergeConfigFrom($configPath, "geocoder");
30-
$this->app->singleton("geocoder", function () {
31-
return (new ProviderAndDumperAggregator)
32-
->registerProvidersFromConfig(collect(config("geocoder.providers")));
28+
29+
$providerAndDumperAggregator = (new ProviderAndDumperAggregator)
30+
->registerProvidersFromConfig(collect(config("geocoder.providers")));
31+
32+
$this->app->singleton("geocoder", function ($app) use ($providerAndDumperAggregator) {
33+
return $providerAndDumperAggregator;
3334
});
35+
36+
// Resolve dependency via class name
37+
// i.e app(ProviderAndDumperAggregator::class) or _construct(ProviderAndDumperAggregator $geocoder)
38+
$this->app->instance(ProviderAndDumperAggregator::class, $providerAndDumperAggregator);
3439
}
3540

3641
public function register()
3742
{
3843
$this->app->alias("Geocoder", Geocoder::class);
3944
}
4045

41-
public function provides() : array
46+
public function provides(): array
4247
{
4348
return ["geocoder"];
4449
}
4550

46-
protected function configPath(string $path = "") : string
51+
protected function configPath(string $path = ""): string
4752
{
4853
if (function_exists("config_path")) {
4954
return config_path($path);

0 commit comments

Comments
 (0)
0