Commit be5ec1a
committed
minor symfony#60803 [SecurityBundle] Don't break
This PR was merged into the 7.4 branch.
Discussion
----------
[SecurityBundle] Don't break `security.http_utils` service decoration
| Q | A
| ------------- | ---
| Branch? | 7.4
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Issues |
| License | MIT
---
Actually, this service is really hard to override. My use case: I want to preserve a parameter in the URL. I ended with the following code, and I'm not happy with it
```php
<?php
namespace App\Security\HttpUtils;
use App\Site\SiteContext;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Security\Http\HttpUtils as SymfonyHttpUtils;
#[AsDecorator('security.http_utils')]
final class HttpUtils extends SymfonyHttpUtils
{
public function __construct(
private readonly SiteContext $siteContext,
?UrlGeneratorInterface $urlGenerator = null,
#[Autowire(service: 'router')]
UrlMatcherInterface|RequestMatcherInterface|null $urlMatcher = null,
// See symfony#60803
?string $domainRegexp = null,
?string $secureDomainRegexp = null,
) {
parent::__construct($urlGenerator, $urlMatcher, $domainRegexp, $secureDomainRegexp);
}
public function generateUri(Request $request, string $path): string
{
$uri = parent::generateUri($request, $path);
$site = $this->siteContext->currentSite;
if (!$site) {
return $uri;
}
$position = strpos($uri, '?');
if ($position === false) {
return $uri . '?site=' . $site->id;
}
parse_str(parse_url($uri, PHP_URL_QUERY) ?? '', $queryParams);
if (array_key_exists('site', $queryParams)) {
return $uri;
}
return substr($uri, 0, $position + 1) . 'site=' . $site->id . '&' . substr($uri, $position + 1);
}
}
```
This is not a real decorator. And I cannot do that, since the original service call itself
---
>[!CAUTION]
>I believe my patch is good, but with it I cannot achieve anymore what I need to do 😬
Instead, I could change the class with a compiler pass
Commits
-------
a099ba2 [SecurityBundle] Don't break `security.http_utils` service decorationsecurity.http_utils service decoration (lyrixx)File tree
2 files changed
+2
-2
lines changed- src/Symfony/Bundle/SecurityBundle
- DependencyInjection/Compiler
2 files changed
+2
-2
lines changedLines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
0 commit comments