Skip to content

Commit 7211017

Browse files
committed
improved update command and removed helpers
1 parent 340c4bd commit 7211017

File tree

5 files changed

+48
-43
lines changed

5 files changed

+48
-43
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ There is a Laravel service provider and facade available.
3939
Add the following to the `providers` array in your `config/app.php`
4040

4141
```php
42-
'PulkitJalan\GeoIP\GeoIPServiceProvider'
42+
PulkitJalan\GeoIP\GeoIPServiceProvider::class
4343
```
4444

4545
Next add the following to the `aliases` array in your `config/app.php`
4646

4747
```php
48-
'GeoIP' => 'PulkitJalan\GeoIP\Facades\GeoIP'
48+
'GeoIP' => PulkitJalan\GeoIP\Facades\GeoIP:class
4949
```
5050

51-
Next run `php artisan vendor:publish --provider="pulkitjalan\geoip\GeoIPServiceProvider"` to publish the config file.
51+
Next run `php artisan vendor:publish --provider="PulkitJalan\GeoIP\GeoIPServiceProvider" --tag="config"` to publish the config file.
5252

5353
#### Looking for a Laravel 4 compatible version?
5454

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
"autoload": {
2424
"psr-4": {
2525
"PulkitJalan\\GeoIP\\": "src"
26-
},
27-
"files": [
28-
"helpers.php"
29-
]
26+
}
3027
},
3128
"extra": {
3229
"branch-alias": {

helpers.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/GeoIPUpdater.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Client as GuzzleClient;
66
use PulkitJalan\Requester\Requester;
7+
use Exception;
78

89
class GeoIPUpdater
910
{
@@ -52,10 +53,49 @@ protected function updateMaxmindDatabase()
5253

5354
$database = array_get($this->config, 'maxmind.database', false);
5455

56+
if (!file_exists($dir = pathinfo($database, PATHINFO_DIRNAME))) {
57+
mkdir($dir, 0777, true);
58+
}
59+
5560
$file = $this->requester->url($maxmindDatabaseUrl)->get()->getBody();
5661

57-
@file_put_contents($database, gzdecode($file));
62+
try {
63+
file_put_contents($database, $this->gzdecode($file));
64+
} catch (Exception $e) {
65+
return false;
66+
}
5867

5968
return $database;
6069
}
70+
71+
/**
72+
* gzdecode function.
73+
*
74+
* @param mixed $data
75+
* @return mixed
76+
*/
77+
protected function gzdecode($data)
78+
{
79+
do {
80+
$tempName = uniqid('temp ');
81+
} while (file_exists($tempName));
82+
83+
if (file_put_contents($tempName, $data)) {
84+
try {
85+
ob_start();
86+
@readgzfile($tempName);
87+
$uncompressed = ob_get_clean();
88+
} catch (Exception $e) {
89+
$ex = $e;
90+
}
91+
92+
unlink($tempName);
93+
94+
if (isset($ex)) {
95+
throw $ex;
96+
}
97+
98+
return $uncompressed;
99+
}
100+
}
61101
}

src/config/config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
| Only applicable with the Pro service
4141
|
4242
*/
43-
'secure' => env('GEOIP_IPAPI_SECURE'),
43+
'secure' => env('GEOIP_IPAPI_SECURE', true),
4444
],
4545

4646
/*
@@ -57,7 +57,7 @@
5757
| Use http or https
5858
|
5959
*/
60-
'secure' => env('GEOIP_TELIZE_SECURE'),
60+
'secure' => env('GEOIP_TELIZE_SECURE', true),
6161
],
6262

6363
/*
@@ -74,7 +74,7 @@
7474
| Example: app_path().'/database/maxmind/GeoLite2-City.mmdb'
7575
|
7676
*/
77-
'database' => env('GEOIP_MAXMIND_DATABASE'),
77+
'database' => base_path().'/'.env('GEOIP_MAXMIND_DATABASE', 'database/geoip/GeoLite2-City.mmdb'),
7878

7979
/*
8080
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)