symfony/symfony — pull request #11890 by lstrojny (2014-09-09 12:49:04+00:00)
Title: [HttpFoundation] add with*Request() convenience methods

| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | [n.A.
| License | MIT
| Doc PR | -

When using request stack you end up with a lot of code that looks like this:

```php
$request = $this->requestStack->getCurrentRequest();

if ($request === null) {
return;
}

$this->doSomethingWithRequest($request);
```

This is such a common pattern, especially in listeners, that I want that to be more convenient. Replace above code with:

```php
$request = $this->requestStack->withCurrentRequest(
function (Request $request) {
$this->doSomethingWithRequest($request);
}
);
```