Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing the use of the globalOptions method with middleware #1

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 14 additions & 10 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', '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')
)
),
],
],
];
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