Skip to content

Commit

Permalink
Merge cff2542 into eaf6c77
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jan 27, 2020
2 parents eaf6c77 + cff2542 commit c4f445d
Show file tree
Hide file tree
Showing 17 changed files with 1,873 additions and 202 deletions.
181 changes: 160 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,85 @@ composer req gpslab/geoip2

## Configuration

**Attention!** MaxMind [changed](https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-geolite2-databases/)
their policy and user agreements about using their data so old URLs like
`https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz` are no working anymore.
To configure auto-update the database you need to generate your personal licence key.

**Steps for Migration**
#### Steps for generate licence key

1. [Sign up for a MaxMind account](https://www.maxmind.com/en/geolite2/signup) (no purchase required)
2. Login and generate a [licence key](https://www.maxmind.com/en/accounts/current/license-key)
3. Save your licence key
4. Open [download page](https://www.maxmind.com/en/download_files) and find your needed DB edition `ID` and copy value
from first column.
![GeoIP2 download page](https://user-images.githubusercontent.com/2862833/72380833-4ccd5a00-3727-11ea-9c6c-aecd55c086ed.png)
5. Open your config file with `gpslab_geoip` params and replace `url` param by next:

<p align="center">
<a href="https://user-images.githubusercontent.com/2862833/72380833-4ccd5a00-3727-11ea-9c6c-aecd55c086ed.png">
<img src="https://user-images.githubusercontent.com/2862833/72380833-4ccd5a00-3727-11ea-9c6c-aecd55c086ed.png" alt="GeoIP2 download page">
</a>
</p>

Example configuration:

```yml
gpslab_geoip:
# Your personal licence key
license: 'XXXXXXXXXXXXXXXX'

# Database edition ID
edition: 'GeoLite2-City'
```

#### Database source URL

By default, this URL is used to download a new databases
`https://download.maxmind.com/app/geoip_download?edition_id={edition_id}&license_key={license_key}&suffix=tar.gz`

* `ID` - character ID name from first column on download page
* `license_key` - your saved licence key

* `edition_id` - character ID name from first column on [download page](https://www.maxmind.com/en/download_files);
* `license_key` - your personal [licence key](https://www.maxmind.com/en/accounts/current/license-key).

You can change this URL, for example, if you want to use a proxy to download the database. You can customize the source
URL in the configuration.

```yml
gpslab_geoip:
# URL for download new GeoIP database.
# You should change {edition_id} and {license_key} in URL to your values.
url: 'https://download.maxmind.com/app/geoip_download?edition_id={edition_id}&license_key={license_key}&suffix=tar.gz'
url: 'https://example.com/GeoLite2-City.tar.gz'
```

# Path to download GeoIP database.
# It's a default value. You can change it.
cache: '%kernel.cache_dir%/GeoLite2-City.mmdb'
### Target download path

# Get model data in this locale
# It's a default value. You can change it.
locales: [ '%locale%' ]
By default, new databases downloaded in `%kernel.cache_dir%/{edition_id}.mmdb`, where `edition_id` is a character ID
name from first column on [download page](https://www.maxmind.com/en/download_files). That is, by default, the new
database will be downloaded into folder `var/cache/{env}/`. Keeping the database in the cache folder for each
environment may not be optimal. You can choose a common directory for all environments.

```yml
gpslab_geoip:
path: '%kernel.project_dir%/var/GeoLite2-City.mmdb'
```

#### Localization

By default, the English locale is used for GeoIP record. You can change the locale for record and declare multiple
locales for fallback.

```yml
gpslab_geoip:
locales: [ 'ru', 'en' ]
```

## Usage

You can get GeoIP2 reader service:

```php
use GeoIp2\Database\Reader;

// get a GeoIP2 City model
$record = $this->get('geoip2.reader')->city('128.101.101.101');
$reader = $this->get(Reader::class);
// or
//$reader = $this->get('geoip2.reader');

$record = $reader->city('128.101.101.101');


print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'
Expand All @@ -79,14 +118,114 @@ print($record->location->longitude . "\n"); // -93.2323

For more example see the [GeoIP2](https://github.com/maxmind/GeoIP2-php) library.

## Update GeoIP database
## Multiple databases

You can use multiple GeoIP databases in one application. Need update configuration file.

```yml
gpslab_geoip:
databases:
default:
license: 'XXXXXXXXXXXXXXXX'
edition: 'GeoLite2-City'
country:
license: 'XXXXXXXXXXXXXXXX'
edition: 'GeoLite2-Country'
asn:
license: 'XXXXXXXXXXXXXXXX'
edition: 'GeoLite2-ASN'
```

Using in application:

```php
// get a GeoIP2 City model
$default_reader = $this->get('geoip2.database.default_reader');
// or
//$default_reader = $this->get(Reader::class);
// or
//$default_reader = $this->get('geoip2.reader');

// get a GeoIP2 Country model
$country_reader = $this->get('geoip2.database.country_reader');

// get a GeoIP2 ASN model
$asn_reader = $this->get('geoip2.database.asn_reader');
```

You can rename the default database.

```yml
gpslab_geoip:
default_database: 'city'
databases:
asn:
license: 'XXXXXXXXXXXXXXXX'
edition: 'GeoLite2-ASN'
city:
license: 'XXXXXXXXXXXXXXXX'
edition: 'GeoLite2-City'
country:
license: 'XXXXXXXXXXXXXXXX'
edition: 'GeoLite2-Country'
```

Execute command for update database:
In order not to repeat the license key and locales for each database, you can specify them once.

```yml
gpslab_geoip:
license: 'XXXXXXXXXXXXXXXX' # global license
locales: [ 'ru', 'en' ] # global locales
default_database: 'city'
databases:
asn:
edition: 'GeoLite2-ASN'
locales: [ 'fr' ] # customize locales
city:
edition: 'GeoLite2-City'
country:
edition: 'GeoLite2-Country'
license: 'YYYYYYYYYYYYYYYY' # customize license
```

## Console commands

### Update GeoIP database

Execute console command for update all databases:

```
php bin/console geoip2:update
```

If you use multiple databases, then for config:

```yml
gpslab_geoip:
# ...
databases:
asn:
# ...
city:
# ...
country:
# ...
```

You can update several databases:

```
php bin/console geoip2:update city country
```

### Download GeoIP database

You can download custom database with console command:

```
php bin/console geoip2:download https://example.com/GeoLite2-City.tar.gz /path/to/GeoLite2-City.mmdb
```

## License

This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE
51 changes: 44 additions & 7 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
UPGRADE FROM 1.x to 2.0
=======================

Update `composer.json` if you use composer vent callbacks.
Renamed configuration option `cache` to `path`.

Before

```yml
gpslab_geoip:
cache: '%kernel.cache_dir%/GeoLite2-City.mmdb'
```

After

```yml
gpslab_geoip:
path: '%kernel.cache_dir%/GeoLite2-City.mmdb'
```

Update `composer.json` if you use composer event callbacks.

Before in Symfony <3.0

Expand Down Expand Up @@ -42,14 +58,19 @@ After in Symfony >4
}
```

### Dependencies
### Update database command

* The `UpdateDatabaseCommand` command not dependency a `CompressorInterface`.
The update database command (`geoip2:update`) split into two separate commands.

### Renamed
* `geoip2:download` - command for download some not configured database from URL to specific path.
* `geoip2:update` - command for update configured databases.

### Renamed

* The `gpslab.command.geoip2.update` service renamed to `GpsLab\Bundle\GeoIP2Bundle\Command\UpdateDatabaseCommand`.
* The `gpslab.command.geoip2.update` service renamed to `GpsLab\Bundle\GeoIP2Bundle\Command\DownloadDatabaseCommand`.
* The `GpsLab\Bundle\GeoIP2Bundle\Command\UpdateDatabaseCommand` class renamed to
`GpsLab\Bundle\GeoIP2Bundle\Command\DownloadDatabaseCommand`.
* The `geoip2:update` console command renamed to `geoip2:download`.

### Removed

Expand All @@ -59,7 +80,23 @@ After in Symfony >4
Updating Dependencies
---------------------

### Removed package
### Require PHP extensions

* Require the [Phar](https://www.php.net/manual/en/book.phar.php) extension.
* Require the [Zlib](https://www.php.net/manual/en/book.zlib.php) extension.

### Require packages

* Require the `symfony/filesystem` package.
* Require the `symfony/config` package.
* The `symfony/http-kernel` package moved from `require-dev` to `require`.
* The `symfony/dependency-injection` package moved from `require-dev` to `require`.
* The `symfony/expression-language` package moved from `require-dev` to `require`.
* The `symfony/console` package moved from `require-dev` to `require`.

### Removed packages

* The `gpslab/compressor` package removed from dependencies.
* The `symfony/stopwatch` package removed from dependencies.
* The `gpslab/compressor` package removed from dependencies.
* The `scrutinizer/ocular` package removed from dependencies.
* The `satooshi/php-coveralls` package removed from dependencies.
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
},
"require": {
"ext-phar": "*",
"ext-zlib": "*",
"php": ">=7.1.0",
"geoip2/geoip2": "~2.0"
},
"require-dev": {
"roave/security-advisories": "dev-master",
"geoip2/geoip2": "~2.0",
"symfony/http-kernel": "~2.3|~3.0|~4.0|~5.0",
"symfony/dependency-injection": "~2.3|~3.0|~4.0|~5.0",
"symfony/expression-language": "~2.3|~3.0|~4.0|~5.0",
"symfony/config": "~2.3|~3.0|~4.0|~5.0",
"symfony/filesystem": "~2.3|~3.0|~4.0|~5.0",
"symfony/yaml": "~2.3|~3.0|~4.0|~5.0",
"symfony/console": "~2.3|~3.0|~4.0|~5.0",
"symfony/filesystem": "~2.3|~3.0|~4.0|~5.0"
},
"require-dev": {
"roave/security-advisories": "dev-master",
"phpunit/phpunit": "~7.0|~8.0",
"phpstan/phpstan": "^0.12"
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12"
}
}
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon

parameters:
level: 7
paths:
Expand Down
Loading

0 comments on commit c4f445d

Please sign in to comment.