Skip to content

Commit f42719b

Browse files
committed
added an update command to help with updates
1 parent 4f3a2cb commit f42719b

File tree

9 files changed

+264
-3
lines changed

9 files changed

+264
-3
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ GeoIP
1010
[![Latest Version](http://img.shields.io/packagist/v/pulkitjalan/geoip.svg?style=flat-square)](https://packagist.org/packages/pulkitjalan/geoip)
1111
[![Total Downloads](https://img.shields.io/packagist/dt/pulkitjalan/geoip.svg?style=flat-square)](https://packagist.org/packages/pulkitjalan/geoip)
1212

13-
This package requires PHP >=5.4
13+
This package requires PHP >= 5.4
1414

1515
## Installation
1616

@@ -81,6 +81,8 @@ $config = [
8181
];
8282
```
8383

84+
### Get Data
85+
8486
Here are the avaliable methods to pull out the required information.
8587

8688
Set IP (Optional)
@@ -149,6 +151,26 @@ Get all geo information
149151
$geoip->get();
150152
```
151153

154+
### Update Database
155+
156+
There is an update command avaliable to help with updating and installing a local geoip database. The following will download and install/update the database file to `/path/to/database.mmdb`.
157+
158+
```php
159+
<?php
160+
161+
use PulkitJalan\GeoIP\GeoIPUpdater
162+
163+
$config = [
164+
'driver' => 'maxmind',
165+
'maxmind' => [
166+
'database' => '/path/to/database.mmdb',
167+
],
168+
];
169+
170+
$geoipUpdater = new GeoIPUpdater($config);
171+
$geoipUpdater->update();
172+
```
173+
152174
## Services
153175

154176
### Maxmind

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
"autoload": {
2323
"psr-4": {
2424
"PulkitJalan\\GeoIP\\": "src"
25-
}
25+
},
26+
"files": [
27+
"helpers.php"
28+
]
2629
},
2730
"minimum-stability": "stable"
2831
}

helpers.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
if (!function_exists('gzdecode')) {
4+
/**
5+
* gzdecode function
6+
*/
7+
function gzdecode($data)
8+
{
9+
do {
10+
$tempName = uniqid('temp ');
11+
} while (file_exists($tempName));
12+
13+
if (file_put_contents($tempName, $data)) {
14+
try {
15+
ob_start();
16+
@readgzfile($tempName);
17+
$uncompressed = ob_get_clean();
18+
} catch (Exception $e) {
19+
$ex = $e;
20+
}
21+
22+
unlink($tempName);
23+
24+
if (isset($ex)) {
25+
throw $ex;
26+
}
27+
28+
return $uncompressed;
29+
}
30+
}
31+
}

src/Drivers/IPApiDriver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class IPApiDriver extends AbstractGeoIPDriver
1717
*/
1818
protected $requester;
1919

20+
/**
21+
* @param array $config
22+
*/
2023
public function __construct(array $config)
2124
{
2225
parent::__construct($config);

src/GeoIPUpdater.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP;
4+
5+
use GuzzleHttp\Client as GuzzleClient;
6+
use PulkitJalan\Requester\Requester;
7+
8+
class GeoIPUpdater
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $maxmindUrl = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz';
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $config;
19+
20+
/**
21+
* @var \PulkitJalan\Requester\Requester
22+
*/
23+
protected $requester;
24+
25+
/**
26+
* @param array $config
27+
*/
28+
public function __construct(array $config)
29+
{
30+
$this->config = $config;
31+
32+
$this->requester = with(new Requester(new GuzzleClient()))->retry(2)->every(50);
33+
}
34+
35+
/**
36+
* Main update function
37+
*
38+
* @return bool|string
39+
*/
40+
public function update()
41+
{
42+
if (array_get($this->config, 'maxmind.database', false)) {
43+
return $this->updateMaxmindDatabase();
44+
}
45+
46+
return false;
47+
}
48+
49+
/**
50+
* Update function for maxmind database
51+
*
52+
* @return string
53+
*/
54+
protected function updateMaxmindDatabase()
55+
{
56+
$database = array_get($this->config, 'maxmind.database', '/tmp/GeoLite2-City.mmdb');
57+
58+
$file = $this->requester->url($this->maxmindUrl)->get()->getBody();
59+
60+
file_put_contents($database, gzdecode($file));
61+
62+
return $database;
63+
}
64+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Laravel\Console;
4+
5+
use Illuminate\Console\Command;
6+
use PulkitJalan\GeoIP\GeoIPUpdater;
7+
8+
class UpdateCommand extends Command
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'geoip:update';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Update geoip database files to the latest version';
23+
24+
/**
25+
* @var \PulkitJalan\GeoIP\GeoIPUpdater
26+
*/
27+
protected $geoIPUpdater;
28+
29+
/**
30+
* Create a new console command instance.
31+
*
32+
* @param Config $config
33+
*/
34+
public function __construct(array $config)
35+
{
36+
parent::__construct();
37+
38+
$this->geoIPUpdater = new GeoIPUpdater($config);
39+
}
40+
41+
/**
42+
* Execute the console command.
43+
*
44+
* @return void
45+
*/
46+
public function fire()
47+
{
48+
$result = $this->geoIPUpdater->update();
49+
50+
if (!$result) {
51+
$this->error('Update failed!');
52+
53+
return;
54+
}
55+
56+
$this->info('New update file ('.$result.') installed.');
57+
}
58+
}

src/Laravel/GeoIPServiceProvider.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace PulkitJalan\GeoIP\Laravel;
44

5+
use PulkitJalan\GeoIP\Laravel\Console\UpdateCommand;
56
use Illuminate\Support\ServiceProvider;
7+
use PulkitJalan\GeoIP\GeoIP;
68

79
class GeoIPServiceProvider extends ServiceProvider
810
{
@@ -32,18 +34,44 @@ public function register()
3234
{
3335
$this->app->config->package('pulkitjalan/geoip', realpath(__DIR__.'/config'), 'geoip');
3436

37+
$this->registerGeoIP();
38+
39+
$this->registerUpdateCommand();
40+
}
41+
42+
/**
43+
* Register the main geoip wrapper
44+
*
45+
* @return void
46+
*/
47+
protected function registerGeoIP()
48+
{
3549
$this->app['geoip'] = $this->app->share(function ($app) {
3650
return new GeoIP($app->config->get('geoip::config'));
3751
});
3852
}
3953

54+
/**
55+
* Register the geoip update console command.
56+
*
57+
* @return void
58+
*/
59+
protected function registerUpdateCommand()
60+
{
61+
$this->app['command.geoip.update'] = $this->app->share(function ($app) {
62+
return new UpdateCommand($app->config->get('geoip::config'));
63+
});
64+
65+
$this->commands(array('command.geoip.update'));
66+
}
67+
4068
/**
4169
* Get the services provided by the provider.
4270
*
4371
* @return string[]
4472
*/
4573
public function provides()
4674
{
47-
return ['geoip', 'PulkitJalan\GeoIP\GeoIP'];
75+
return ['geoip', 'command.geoip.update', 'PulkitJalan\GeoIP\GeoIP'];
4876
}
4977
}

tests/GeoIPTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ public function testMaxmindDatabaseException()
5252
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
5353
}
5454

55+
public function testMaxmindInvalidDatabaseException()
56+
{
57+
$config = [
58+
'driver' => 'maxmind',
59+
'maxmind' => [
60+
'database' => __FILE__,
61+
],
62+
];
63+
64+
$this->setExpectedException('MaxMind\Db\Reader\InvalidDatabaseException');
65+
66+
$geoip = new \PulkitJalan\GeoIP\GeoIP($config);
67+
}
68+
5569
public function testMaxmindWebApiException()
5670
{
5771
$config = [

tests/GeoIPUpdaterTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PulkitJalan\GeoIP\Tests;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use Mockery;
7+
8+
class GeoIPUpdaterTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function tearDown()
11+
{
12+
Mockery::close();
13+
}
14+
15+
public function testNoUpdate()
16+
{
17+
$geoipUpdater = new \PulkitJalan\GeoIP\GeoIPUpdater([]);
18+
19+
$this->assertFalse($geoipUpdater->update());
20+
}
21+
22+
public function testMaxmindUpdater()
23+
{
24+
$database = __DIR__.'/data/GeoLite2-City.mmdb';
25+
$config = [
26+
'driver' => 'maxmind',
27+
'maxmind' => [
28+
'database' => $database,
29+
],
30+
];
31+
32+
$geoipUpdater = new \PulkitJalan\GeoIP\GeoIPUpdater($config);
33+
34+
$this->assertEquals($geoipUpdater->update(), $database);
35+
36+
unlink($database);
37+
}
38+
}

0 commit comments

Comments
 (0)