From f743d8011674577a8889a8a6c4e794eca9813724 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 3 Jun 2024 15:29:36 +0300 Subject: [PATCH 1/2] Replacing the use of the `globalOptions` method with middleware --- README.md | 12 ++++++++++ config/http.php | 22 +++++++++++------- src/Middlewares/SetHeaderMiddleware.php | 31 +++++++++++++++++++++++++ src/ServiceProvider.php | 7 ++---- 4 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 src/Middlewares/SetHeaderMiddleware.php 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 From 31b0bb5fb0fadb5702aba2cc7f9a0f17fac73787 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 3 Jun 2024 15:31:12 +0300 Subject: [PATCH 2/2] Fixed code-style --- config/http.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/http.php b/config/http.php index e8918a1..6ef3e1c 100644 --- a/config/http.php +++ b/config/http.php @@ -7,7 +7,7 @@ return [ 'global' => [ 'user_agent' => [ - 'enabled' => (bool)env('APP_USER_AGENT_ENABLED', true), + 'enabled' => (bool) env('APP_USER_AGENT_ENABLED', true), 'value' => env( 'APP_USER_AGENT', @@ -18,7 +18,7 @@ env('APP_URL', 'http://localhost'), env('MAIL_FROM_ADDRESS', 'hello@example.com') ) - ) + ), ], - ] + ], ];