Skip to content

Resolve Geocoder dependency via Class name #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2018
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,19 @@ app('geocoder')->reverse(43.882587,-103.454067)->get();
app('geocoder')->geocode('Los Angeles, CA')->dump('kml');
```

#### Using Controller
```php
use Geocoder\Laravel\ProviderAndDumperAggregator as Geocoder;

class GeocoderController extends Controller
{
public function getGeocode(Geocoder $geocoder)
{
$geocoder->geocode('Los Angeles, CA')->get()
}
}
```

## Upgrading
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):
```sh
Expand Down
19 changes: 12 additions & 7 deletions src/Providers/GeocoderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

use Geocoder\Laravel\Facades\Geocoder;
use Geocoder\Laravel\ProviderAndDumperAggregator;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use ReflectionClass;

class GeocoderService extends ServiceProvider
{
Expand All @@ -27,23 +25,30 @@ public function boot()
"config"
);
$this->mergeConfigFrom($configPath, "geocoder");
$this->app->singleton("geocoder", function () {
return (new ProviderAndDumperAggregator)
->registerProvidersFromConfig(collect(config("geocoder.providers")));

$providerAndDumperAggregator = (new ProviderAndDumperAggregator)
->registerProvidersFromConfig(collect(config("geocoder.providers")));

$this->app->singleton("geocoder", function ($app) use ($providerAndDumperAggregator) {
return $providerAndDumperAggregator;
});

// Resolve dependency via class name
// i.e app(ProviderAndDumperAggregator::class) or _construct(ProviderAndDumperAggregator $geocoder)
$this->app->instance(ProviderAndDumperAggregator::class, $providerAndDumperAggregator);
}

public function register()
{
$this->app->alias("Geocoder", Geocoder::class);
}

public function provides() : array
public function provides(): array
{
return ["geocoder"];
}

protected function configPath(string $path = "") : string
protected function configPath(string $path = ""): string
{
if (function_exists("config_path")) {
return config_path($path);
Expand Down