diff --git a/README.md b/README.md index 0d23c8b..6093ce6 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,12 @@ [![Github Workflow Status][badge_build]][link_build] [![License][badge_license]][link_license] +## FAQ + +- Q: What problem does this package solve? +- A: In cases where it is necessary to explicitly specify the value `User-Agent` in outgoing requests. + In other cases it is not necessary. + ## Installation ```Bash @@ -61,6 +67,12 @@ php artisan vendor:publish --provider="DragonCode\LaravelHttpUserAgent\ServicePr As a result of its execution, the file `config/http.php` will be created. +You can also disable value assignment through the environment settings: + +```ini +APP_USER_AGENT_ENABLED = false +``` + ## License This package is licensed under the [MIT License](LICENSE). diff --git a/config/http.php b/config/http.php index c492872..e8918a1 100644 --- a/config/http.php +++ b/config/http.php @@ -6,15 +6,19 @@ return [ 'global' => [ - 'user_agent' => env( - 'APP_USER_AGENT', - sprintf( - '%s / %s - %s | %s', - env('APP_NAME', 'Laravel'), - Version::detect(), - env('APP_URL', 'http://localhost'), - env('MAIL_FROM_ADDRESS', 'hello@example.com') + 'user_agent' => [ + 'enabled' => (bool)env('APP_USER_AGENT_ENABLED', true), + + 'value' => env( + 'APP_USER_AGENT', + sprintf( + '%s / %s - %s | %s', + env('APP_NAME', 'Laravel'), + Version::detect(), + env('APP_URL', 'http://localhost'), + env('MAIL_FROM_ADDRESS', 'hello@example.com') + ) ) - ), + ], ] ]; diff --git a/src/Middlewares/SetHeaderMiddleware.php b/src/Middlewares/SetHeaderMiddleware.php new file mode 100644 index 0000000..5faf941 --- /dev/null +++ b/src/Middlewares/SetHeaderMiddleware.php @@ -0,0 +1,31 @@ +enabled()) { + return $request->withHeader('User-Agent', $this->value()); + } + + return $request; + } + + protected function enabled(): bool + { + return config('http.global.user_agent.enabled'); + } + + protected function value(): string + { + return config('http.global.user_agent.value'); + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 35db7e2..e759573 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -4,6 +4,7 @@ namespace DragonCode\LaravelHttpUserAgent; +use DragonCode\LaravelHttpUserAgent\Middlewares\SetHeaderMiddleware; use Illuminate\Support\Facades\Http; use Illuminate\Support\ServiceProvider as BaseServiceProvider; @@ -22,11 +23,7 @@ public function boot(): void protected function bootUserAgent(): void { - Http::globalOptions([ - 'headers' => [ - 'User-Agent' => config('http.global.user_agent'), - ] - ]); + Http::globalRequestMiddleware(new SetHeaderMiddleware()); } protected function bootConfigPublishes(): void