bug #14816 [TwigBridge] Make AppVariable check if security.context exists (ogizanagi)

This PR was merged into the 2.7 branch.

Discussion
----------

[TwigBridge] Make AppVariable check if security.context exists

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

If security isn't configured in the application, neither the `security.context` service, nor the `security.token_storage` service exists.

Therefore, if a third-party bundle relies on the `app.user` or `app.security` (deprecated) check in Twig templates, an exception was thrown about asking for an non-existing service:

```yml
security: false
```

```twig
{% if app.user %}
    <div>
        ...
    </div>
{% endif %}
```
```
You have requested a non-existent service "security.context"
```

Instead, this patch checks if the `security.context` actually exists before trying to use it, and returns null otherwise.

Note that the **GlobalVariables** class, used for twig app globals in previous versions, and still used for PHP templating, behaves the same way.
So this is basically a BC break.

Commits
-------

ea71174 [TwigBridge] Make AppVariable check if security.context exists
This commit is contained in:
Fabien Potencier 2015-06-11 21:11:23 +02:00
commit da02fceb26
1 changed files with 5 additions and 1 deletions

View File

@ -74,7 +74,9 @@ class AppVariable
throw new \RuntimeException('The "app.security" variable is not available.');
}
return $this->container->get('security.context');
if ($this->container->has('security.context')) {
return $this->container->get('security.context');
}
}
/**
@ -89,6 +91,8 @@ class AppVariable
if (null === $this->tokenStorage) {
if (null === $this->container) {
throw new \RuntimeException('The "app.user" variable is not available.');
} elseif (!$this->container->has('security.context')) {
return;
}
$this->tokenStorage = $this->container->get('security.context');