Skip to content

Commit

Permalink
Merge pull request #256 from norkunas/autotag
Browse files Browse the repository at this point in the history
Auto tag dumpers with `bazinga_geocoder.dumper`
  • Loading branch information
norkunas committed Oct 21, 2019
2 parents 918599e + c40f24c commit 2850133
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
4 changes: 4 additions & 0 deletions DependencyInjection/BazingaGeocoderExtension.php
Expand Up @@ -18,6 +18,7 @@
use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin;
use Bazinga\GeocoderBundle\ProviderFactory\PluginProviderFactory;
use Bazinga\GeocoderBundle\ProviderFactory\ProviderFactoryInterface;
use Geocoder\Dumper\Dumper;
use Geocoder\Plugin\Plugin\CachePlugin;
use Geocoder\Plugin\Plugin\LimitPlugin;
use Geocoder\Plugin\Plugin\LocalePlugin;
Expand Down Expand Up @@ -59,6 +60,9 @@ public function load(array $configs, ContainerBuilder $container)
}

$this->loadProviders($container, $config);

$container->registerForAutoconfiguration(Dumper::class)
->addTag('bazinga_geocoder.dumper');
}

private function loadProviders(ContainerBuilder $container, array $config)
Expand Down
34 changes: 11 additions & 23 deletions Resources/config/services.yml
@@ -1,27 +1,15 @@
services:
Geocoder\Dumper\GeoArray:
public: true
tags: ['bazinga_geocoder.dumper']

Geocoder\Dumper\GeoJson:
public: true
tags: ['bazinga_geocoder.dumper']

Geocoder\Dumper\Gpx:
public: true
tags: ['bazinga_geocoder.dumper']

Geocoder\Dumper\Kml:
public: true
tags: ['bazinga_geocoder.dumper']

Geocoder\Dumper\Wkb:
public: true
tags: ['bazinga_geocoder.dumper']

Geocoder\Dumper\Wkt:
public: true
tags: ['bazinga_geocoder.dumper']
_instanceof:
Geocoder\Dumper\Dumper:
tags: ['bazinga_geocoder.dumper']
public: true

Geocoder\Dumper\GeoArray: ~
Geocoder\Dumper\GeoJson: ~
Geocoder\Dumper\Gpx: ~
Geocoder\Dumper\Kml: ~
Geocoder\Dumper\Wkb: ~
Geocoder\Dumper\Wkt: ~

Bazinga\GeocoderBundle\ProviderFactory\:
resource: '../../ProviderFactory'
Expand Down
52 changes: 32 additions & 20 deletions Resources/doc/index.md
Expand Up @@ -230,41 +230,53 @@ Read more about cache [here](cache.md).
If you need to dump your geocoded data to a specific format, you can use the
__Dumper__ component. The following dumper's are supported:

* Geojson
* GeoArray
* GeoJson
* GPX
* KMP
* KML
* WKB
* WKT

Here is an example:
Here is an example if you are using autowiring:

```php
<?php

public function geocodeAction(Request $request)
namespace App\Controller;

use Geocoder\Dumper\GeoJson;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class AcmeController
{
$result = $this->container
->get('bazinga_geocoder.provider.acme')
->geocodeQuery(GeocodeQuery::create($request->server->get('REMOTE_ADDR')));
private $acmeGeocoder;
private $geoJsonDumper;

$body = $this->container
->get('Geocoder\Dumper\GeoJson')
->dump($result);
public function __construct(Provider $acmeGeocoder, GeoJson $dumper)
{
$this->acmeGeocoder = $acmeGeocoder;
$this->geoJsonDumper = $dumper;
}

public function geocodeAction(Request $request)
{
$result = $this->acmeGeocoder->geocodeQuery(GeocodeQuery::create($request->server->get('REMOTE_ADDR')));

$response = new Response();
$response->setContent($body);
$body = $this->geoJsonDumper->dump($result);

return $response;
return new JsonResponse($body);
}
}
```

To register a new dumper, you must tag it with `bazinga_geocoder.dumper`.

```xml
<service id="some.dumper" class="%some.dumper.class">
<tag name="bazinga_geocoder.dumper" alias="custom" />
</service>
```
Each dumper service if it implements `Geocoder\Dumper\Dumper` interface will be
tagged with `bazinga_geocoder.dumper` tag. Each dumper can be used with autowiring
providing the dumper class name as the argument.
Also If you want to inject all the tagged dumpers to your service you can provide
your service argument as: `!tagged bazinga_geocoder.dumper`.

### Custom HTTP Client

Expand Down

0 comments on commit 2850133

Please sign in to comment.