Skip to content

Commit

Permalink
Merge pull request #9 from ipinfo/uman/fixes
Browse files Browse the repository at this point in the history
Various fixes & updates for v2
  • Loading branch information
UmanShahzad authored Nov 27, 2020
2 parents 7846c6e + 42e7dfc commit 65c7832
Show file tree
Hide file tree
Showing 99 changed files with 13,377 additions and 233 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build
composer.lock
vendor
.phpunit.result.cache
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# CHANGELOG

All notable changes to `ipinfolaravel` will be documented in this file.

## Version 2.0

- Supports Laravel 5.x to 8.x.
- The `ipinfo` object on the request object is not accessible via input, e.g.
`$request->input('ipinfo')->ip`; you must use `$request->ipinfo->ip`, etc.
- The IP for which data is retrieved is now correctly the **client IP**, and
not the server IP.

## Version 1.0

### Added

- Everything
63 changes: 31 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,51 @@ composer require ipinfo/ipinfolaravel

Open your application's `\app\Http\Kernel.php` file and add the following to the `Kernel::middleware` property:

```
```php
protected $middleware = [
...
\ipinfo\ipinfolaravel\ipinfolaravel::class,
];
...
\ipinfo\ipinfolaravel\ipinfolaravel::class,
];
```

#### Quick Start

```
```php
Route::get('/', function (Request $request) {
$location_text = "The IP address {$request->ipinfo->ip} is located in the city of {$request->ipinfo->city}."
$location_text = "The IP address {$request->ipinfo->ip}.";
return view('index', ['location' => $location_text]);
});
```

will return the following string to the `index` view:

```
"The IP address 216.239.36.21 is located in the city of Emeryville."
"The IP address 127.0.0.1."
```

### Authentication

The IPinfo library can be authenticated with your IPinfo API token. It also works without an authentication token, but in a more limited capacity. To set your access token, add the following to your app's `\config\services.php` file and replace `{{access_token}}` with your own token:

```
```php
'ipinfo' => [
'access_token' => {{access_token}},
],
'access_token' => {{access_token}},
],
```

To do this in a more secure manner and avoid putting secret keys in your codebase, create an `IPINFO_SECRET` (or similar) environment variable and access this value from within `\config\services.php`, like so:

```
```php
'ipinfo' => [
'access_token' => env('IPINFO_SECRET'),
],
'access_token' => env('IPINFO_SECRET'),
],
```

### Details Data

`$request->ipinfo` is a `Details` object that contains all fields listed [IPinfo developer docs](https://ipinfo.io/developers/responses#full-response) with a few minor additions. Properties can be accessed directly.

```
```php
>>> $request->ipinfo->hostname
cpe-104-175-221-247.socal.res.rr.com
```
Expand All @@ -76,7 +75,7 @@ cpe-104-175-221-247.socal.res.rr.com

`$request->ipinfo->country_name` will return the country name, as supplied by the `countries.json` file. See below for instructions on changing that file for use with non-English languages. `$request->ipinfo->country` will still return the country code.

```
```php
>>> $request->ipinfo->country
US
>>> $request->ipinfo->country_name
Expand All @@ -87,7 +86,7 @@ United States

`$request->ipinfo->all` will return all details data as an array.

```
```php
>>> $request->ipinfo->all
{
'asn': { 'asn': 'AS20001',
Expand Down Expand Up @@ -124,7 +123,7 @@ Default cache TTL and maximum size can be changed by setting values in the `$set
* Default maximum cache size: 4096 (multiples of 2 are recommended to increase efficiency)
* Default TTL: 24 hours (in minutes)

```
```php
'ipinfo' => [
'cache_maxsize' => {{cache_maxsize}},
'cache_ttl' => {{cache_ttl}},
Expand All @@ -135,7 +134,7 @@ Default cache TTL and maximum size can be changed by setting values in the `$set

It is possible to use a custom cache by creating a child class of the [CacheInterface](https://github.com/ipinfo/php/blob/master/src/cache/Interface.php) class and setting the the `cache` config value in `\config\services.php`. FYI this is known as [the Strategy Pattern](https://sourcemaking.com/design_patterns/strategy).

```
```php
'ipinfo' => [
...
'cache' => new MyCustomCacheObject(),
Expand All @@ -146,7 +145,7 @@ It is possible to use a custom cache by creating a child class of the [CacheInte

When looking up an IP address, the response object includes a `$request->ipinfo->country_name` property which includes the country name based on American English. It is possible to return the country name in other languages by telling the library to read from a custom file. To define a custom file, add the following to your app's `\config\services.php` file and replace `{{countries}}` with your own file path.

```
```php
'ipinfo' => [
...
'countries_file' => {{countries}},
Expand All @@ -157,23 +156,23 @@ The file must be a `.json` file with the following structure:

```
{
{{country_code}}: {{country_name}},
"BD": "Bangladesh",
"BE": "Belgium",
"BF": "Burkina Faso",
"BG": "Bulgaria"
...
{{country_code}}: {{country_name}},
"BD": "Bangladesh",
"BE": "Belgium",
"BF": "Burkina Faso",
"BG": "Bulgaria"
...
}
```

### Filtering

By default, `ipinfolaravel` filters out requests that have `bot` or `spider` in the user-agent. Instead of looking up IP address data for these requests, the `$request->ipinfo` attribute is set to `null`. This is to prevent you from unnecessarily using up requests on non-user traffic. This behavior can be switched off by adding the following to your app's `\config\services.php` file.

```
```php
'ipinfo' => [
...
'filter' => false,
...
'filter' => false,
],
```

Expand All @@ -184,10 +183,10 @@ To set your own filtering rules, *thereby replacing the default filter*, you can

To use your own filter function:

```
```php
'ipinfo' => [
...
'filter' => $customFilterFunction,
...
'filter' => $customFilterFunction,
],
```

Expand Down
8 changes: 0 additions & 8 deletions changelog.md

This file was deleted.

22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
"name": "James Timmins",
"email": "[email protected]",
"homepage": "https://www.ipinfo.io"
},
{
"name": "Uman Shahzad",
"email": "[email protected]",
"homepage": "https://github.com/UmanShahzad"
}
],
"homepage": "https://github.com/ipinfo/ipinfolaravel",
"keywords": ["Laravel", "ipinfolaravel"],
"require": {
"illuminate/support": "~5",
"ipinfo/ipinfo": "^1.0@dev"
"illuminate/support": ">=5, <9",
"ipinfo/ipinfo": "^2.0@dev"
},
"require-dev": {
"phpunit/phpunit": "~7.0",
"mockery/mockery": "^1.1",
"orchestra/testbench": "~3.0",
"sempro/phpunit-pretty-print": "^1.0"
"phpunit/phpunit": ">=8 <10",
"mockery/mockery": "^1.4.2",
"orchestra/testbench": "^6.4.0",
"sempro/phpunit-pretty-print": "^1.3.0",
"squizlabs/php_codesniffer": "^3.5.8"
},
"autoload": {
"psr-4": {
Expand All @@ -31,6 +37,10 @@
"ipinfo\\ipinfolaravel\\Tests\\": "tests"
}
},
"scripts": {
"check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src config",
"fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src config"
},
"extra": {
"laravel": {
"providers": [
Expand Down
2 changes: 1 addition & 1 deletion config/ipinfolaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

return [
//
];
];
27 changes: 0 additions & 27 deletions contributing.md

This file was deleted.

5 changes: 0 additions & 5 deletions license.md

This file was deleted.

34 changes: 14 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="ipinfo-laravel Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</phpunit>
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="8"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
Loading

0 comments on commit 65c7832

Please sign in to comment.