Skip to content

Commit 0aac67f

Browse files
committed
Make monolog/monolog dependency really optional
1 parent 55b770a commit 0aac67f

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"php": "^8.1",
1616
"illuminate/cache": "^10.0 || ^11.0",
1717
"illuminate/console": "^10.0 || ^11.0",
18-
"illuminate/support": "^10.0 || ^11.0",
19-
"monolog/monolog": "^3.0"
18+
"illuminate/support": "^10.0 || ^11.0"
2019
},
2120
"require-dev": {
2221
"friendsofphp/php-cs-fixer": "^3.50",

config/geoip.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
| Here you may configure the log settings for when a location is not found
1313
| for the IP provided.
1414
|
15+
| Requires Monolog to be installed: `composer install monolog/monolog`
16+
|
1517
*/
1618

1719
'log_failures' => true,

src/GeoIP.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace InteractionDesignFoundation\GeoIP;
66

7-
use Monolog\Logger;
87
use Illuminate\Support\Arr;
98
use Illuminate\Cache\CacheManager;
10-
use Monolog\Handler\StreamHandler;
119

1210
/**
1311
* @psalm-import-type LocationArray from \InteractionDesignFoundation\GeoIP\Location
@@ -148,8 +146,16 @@ private function find($ip = null): Location
148146
return $location;
149147
} catch (\Exception $e) {
150148
if ($this->config('log_failures', true) === true) {
151-
$log = new Logger('geoip');
152-
$log->pushHandler(new StreamHandler(storage_path('logs/geoip.log'), Logger::ERROR));
149+
if (! class_exists(\Monolog\Logger::class)) {
150+
throw new \RuntimeException(
151+
'monolog/monolog composer package is not installed, but required with the enabled geoip.log_failures config option.',
152+
0,
153+
$e
154+
);
155+
}
156+
157+
$log = new \Monolog\Logger('geoip');
158+
$log->pushHandler(new \Monolog\Handler\StreamHandler(storage_path('logs/geoip.log'), \Monolog\Logger::ERROR));
153159
$log->error($e);
154160
}
155161
}

0 commit comments

Comments
 (0)