Skip to content

Commit dbb72f8

Browse files
committed
Merge pull request #40 from defrag/feature/fake-listener-priotity
Fake ip priority configurable
2 parents a622b91 + 6dd2d2e commit dbb72f8

File tree

8 files changed

+71
-9
lines changed

8 files changed

+71
-9
lines changed

DependencyInjection/BazingaGeocoderExtension.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public function load(array $configs, ContainerBuilder $container)
3636
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3737
$loader->load('services.xml');
3838

39-
if (isset($config['fake_ip'])) {
40-
$container
41-
->getDefinition('bazinga_geocoder.event_listener.fake_request')
42-
->replaceArgument(0, $config['fake_ip'])
43-
;
39+
if (isset($config['fake_ip']) && !empty($config['fake_ip'])) {
40+
$definition = $container->getDefinition('bazinga_geocoder.event_listener.fake_request');
41+
$definition->replaceArgument(0, $config['fake_ip']['ip']);
42+
43+
$tag = current($definition->getTag('kernel.event_listener'));
44+
$tag['priority'] = $config['fake_ip']['priority'];
45+
$definition->setTags(array($tag));
4446
}
4547

4648
if (isset($config['adapter']['class']) && !empty($config['adapter']['class'])) {

DependencyInjection/Configuration.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ public function getConfigTreeBuilder()
3030

3131
$rootNode
3232
->children()
33-
->scalarNode('fake_ip')->defaultNull()->end()
33+
->arrayNode('fake_ip')
34+
->beforeNormalization()
35+
->ifString()
36+
->then(function($value) { return array('ip' => $value); })
37+
->end()
38+
->children()
39+
->scalarNode('ip')->defaultNull()->end()
40+
->scalarNode('priority')->defaultValue(0)->end()
41+
->end()
42+
->end()
3443
->arrayNode('adapter')
3544
->children()
3645
->scalarNode('class')->defaultNull()->end()

EventListener/FakeRequestListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ public function onKernelRequest(GetResponseEvent $event)
3838
$event->getRequest()->server->set('REMOTE_ADDR', $this->fakeIp);
3939
}
4040
}
41+
42+
public function getFakeIp()
43+
{
44+
return $this->fakeIp;
45+
}
4146
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ License
3939
This bundle is released under the MIT license. See the complete license in the
4040
bundle:
4141

42-
Resources/meta/LICENSE
42+
Resources/meta/LICENSE

Resources/doc/index.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ information in your development environment, for instance:
9090
In the example, we'll retrieve information from the user's IP address, and 5
9191
objects nears him.
9292
But it won't work on your local environment, that's why this bundle provides
93-
an easy way to fake this behavior by using a `fake_ip` parameter.
93+
an easy way to fake this behavior by using a `fake_ip` configuration.
9494

9595
``` yaml
9696
# app/config/config_dev.yml
@@ -100,6 +100,19 @@ bazinga_geocoder:
100100
101101
If set, the parameter will replace the `REMOTE_ADDR` value by the given one.
102102

103+
104+
Additionally if it interferes with your current
105+
listeners, You can set up different fake ip listener priority.
106+
107+
108+
``` yaml
109+
# app/config/config_dev.yml
110+
bazinga_geocoder:
111+
fake_ip:
112+
ip: 123.345.643.133
113+
priority: 128
114+
```
115+
103116
### Dumpers
104117

105118
If you need to dump your geocoded data to a specific format, you can use the

Tests/DependencyInjection/BazingaGeocoderExtensionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public function testLoad()
3434
'Bazinga\Bundle\GeocoderBundle\EventListener\FakeRequestListener',
3535
$container->get('bazinga_geocoder.event_listener.fake_request')
3636
);
37+
$this->assertNotNull(
38+
$container->get('bazinga_geocoder.event_listener.fake_request')->getFakeIp()
39+
);
3740

3841
$dumperManager = $container->get('bazinga_geocoder.dumper_manager');
3942
foreach (array('geojson', 'gpx', 'kmp', 'wkb', 'wkt') as $name) {
@@ -68,4 +71,30 @@ public function testLoad()
6871
$this->assertInstanceOf($class, $providers[$name], sprintf('-> Assert that %s is instance of %s', $name, $class));
6972
}
7073
}
74+
75+
public function testLoadingFakeIpOldWay()
76+
{
77+
$configs = Yaml::parse(file_get_contents(__DIR__.'/Fixtures/old_fake_ip.yml'));
78+
$container = new ContainerBuilder();
79+
$extension = new BazingaGeocoderExtension();
80+
81+
$container->setParameter('fixtures_dir', __DIR__ . '/Fixtures');
82+
83+
$container->set('doctrine.apc.cache', new ArrayCache());
84+
85+
$container->addCompilerPass(new AddDumperPass());
86+
$container->addCompilerPass(new AddProvidersPass());
87+
88+
$extension->load($configs, $container);
89+
$container->compile();
90+
91+
$this->assertInstanceOf(
92+
'Bazinga\Bundle\GeocoderBundle\EventListener\FakeRequestListener',
93+
$container->get('bazinga_geocoder.event_listener.fake_request')
94+
);
95+
96+
$this->assertNotNull(
97+
$container->get('bazinga_geocoder.event_listener.fake_request')->getFakeIp()
98+
);
99+
}
71100
}

Tests/DependencyInjection/Fixtures/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
bazinga_geocoder:
2-
fake_ip: 33.33.33.11
2+
fake_ip:
3+
ip: 33.33.33.11
4+
priority: 128
35
adapter:
46
class: Geocoder\HttpAdapter\CurlHttpAdapter
57
providers:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bazinga_geocoder:
2+
fake_ip: 33.33.33.01

0 commit comments

Comments
 (0)