Skip to content

Commit 2850133

Browse files
authored
Merge pull request #256 from norkunas/autotag
Auto tag dumpers with `bazinga_geocoder.dumper`
2 parents 918599e + c40f24c commit 2850133

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

DependencyInjection/BazingaGeocoderExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin;
1919
use Bazinga\GeocoderBundle\ProviderFactory\PluginProviderFactory;
2020
use Bazinga\GeocoderBundle\ProviderFactory\ProviderFactoryInterface;
21+
use Geocoder\Dumper\Dumper;
2122
use Geocoder\Plugin\Plugin\CachePlugin;
2223
use Geocoder\Plugin\Plugin\LimitPlugin;
2324
use Geocoder\Plugin\Plugin\LocalePlugin;
@@ -59,6 +60,9 @@ public function load(array $configs, ContainerBuilder $container)
5960
}
6061

6162
$this->loadProviders($container, $config);
63+
64+
$container->registerForAutoconfiguration(Dumper::class)
65+
->addTag('bazinga_geocoder.dumper');
6266
}
6367

6468
private function loadProviders(ContainerBuilder $container, array $config)

Resources/config/services.yml

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
services:
2-
Geocoder\Dumper\GeoArray:
3-
public: true
4-
tags: ['bazinga_geocoder.dumper']
5-
6-
Geocoder\Dumper\GeoJson:
7-
public: true
8-
tags: ['bazinga_geocoder.dumper']
9-
10-
Geocoder\Dumper\Gpx:
11-
public: true
12-
tags: ['bazinga_geocoder.dumper']
13-
14-
Geocoder\Dumper\Kml:
15-
public: true
16-
tags: ['bazinga_geocoder.dumper']
17-
18-
Geocoder\Dumper\Wkb:
19-
public: true
20-
tags: ['bazinga_geocoder.dumper']
21-
22-
Geocoder\Dumper\Wkt:
23-
public: true
24-
tags: ['bazinga_geocoder.dumper']
2+
_instanceof:
3+
Geocoder\Dumper\Dumper:
4+
tags: ['bazinga_geocoder.dumper']
5+
public: true
6+
7+
Geocoder\Dumper\GeoArray: ~
8+
Geocoder\Dumper\GeoJson: ~
9+
Geocoder\Dumper\Gpx: ~
10+
Geocoder\Dumper\Kml: ~
11+
Geocoder\Dumper\Wkb: ~
12+
Geocoder\Dumper\Wkt: ~
2513

2614
Bazinga\GeocoderBundle\ProviderFactory\:
2715
resource: '../../ProviderFactory'

Resources/doc/index.md

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,41 +230,53 @@ Read more about cache [here](cache.md).
230230
If you need to dump your geocoded data to a specific format, you can use the
231231
__Dumper__ component. The following dumper's are supported:
232232

233-
* Geojson
233+
* GeoArray
234+
* GeoJson
234235
* GPX
235-
* KMP
236+
* KML
236237
* WKB
237238
* WKT
238239

239-
Here is an example:
240+
Here is an example if you are using autowiring:
240241

241242
```php
242243
<?php
243244
244-
public function geocodeAction(Request $request)
245+
namespace App\Controller;
246+
247+
use Geocoder\Dumper\GeoJson;
248+
use Geocoder\Provider\Provider;
249+
use Geocoder\Query\GeocodeQuery;
250+
use Symfony\Component\HttpFoundation\JsonResponse;
251+
use Symfony\Component\HttpFoundation\Request;
252+
253+
class AcmeController
245254
{
246-
$result = $this->container
247-
->get('bazinga_geocoder.provider.acme')
248-
->geocodeQuery(GeocodeQuery::create($request->server->get('REMOTE_ADDR')));
255+
private $acmeGeocoder;
256+
private $geoJsonDumper;
249257
250-
$body = $this->container
251-
->get('Geocoder\Dumper\GeoJson')
252-
->dump($result);
258+
public function __construct(Provider $acmeGeocoder, GeoJson $dumper)
259+
{
260+
$this->acmeGeocoder = $acmeGeocoder;
261+
$this->geoJsonDumper = $dumper;
262+
}
263+
264+
public function geocodeAction(Request $request)
265+
{
266+
$result = $this->acmeGeocoder->geocodeQuery(GeocodeQuery::create($request->server->get('REMOTE_ADDR')));
253267
254-
$response = new Response();
255-
$response->setContent($body);
268+
$body = $this->geoJsonDumper->dump($result);
256269
257-
return $response;
270+
return new JsonResponse($body);
271+
}
258272
}
259273
```
260274

261-
To register a new dumper, you must tag it with `bazinga_geocoder.dumper`.
262-
263-
```xml
264-
<service id="some.dumper" class="%some.dumper.class">
265-
<tag name="bazinga_geocoder.dumper" alias="custom" />
266-
</service>
267-
```
275+
Each dumper service if it implements `Geocoder\Dumper\Dumper` interface will be
276+
tagged with `bazinga_geocoder.dumper` tag. Each dumper can be used with autowiring
277+
providing the dumper class name as the argument.
278+
Also If you want to inject all the tagged dumpers to your service you can provide
279+
your service argument as: `!tagged bazinga_geocoder.dumper`.
268280

269281
### Custom HTTP Client
270282

0 commit comments

Comments
 (0)