Skip to content

Commit

Permalink
Replacing the use of the globalOptions method with middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 3, 2024
1 parent 615e356 commit f743d80
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down
22 changes: 13 additions & 9 deletions config/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '[email protected]')
'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', '[email protected]')
)
)
),
],
]
];
31 changes: 31 additions & 0 deletions src/Middlewares/SetHeaderMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelHttpUserAgent\Middlewares;

use Psr\Http\Message\RequestInterface;

use function config;

class SetHeaderMiddleware
{
public function __invoke(RequestInterface $request): RequestInterface
{
if ($this->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');
}
}
7 changes: 2 additions & 5 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DragonCode\LaravelHttpUserAgent;

use DragonCode\LaravelHttpUserAgent\Middlewares\SetHeaderMiddleware;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

Expand All @@ -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
Expand Down

0 comments on commit f743d80

Please sign in to comment.