[TwigBridge] Make AppVariable check if security.context exists

If security isn't configured in the application, the `security.context` service, nor the `security.token_storage` service exists.
Therefore, if a third-party bundle relies on the app.user check in Twig templates, an exception was thrown about asking for an non-existing service.
Instead, this change check if the `security.context` actually exists before trying to use it, and return null otherwise.
This commit is contained in:
maxime.steinhausser 2015-06-01 16:10:45 +02:00
parent 7493c2bef5
commit ea71174fb7
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');