From ea71174fb7c05ca9808418961f14ff77950d1c8d Mon Sep 17 00:00:00 2001 From: "maxime.steinhausser" Date: Mon, 1 Jun 2015 16:10:45 +0200 Subject: [PATCH] [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. --- src/Symfony/Bridge/Twig/AppVariable.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bridge/Twig/AppVariable.php b/src/Symfony/Bridge/Twig/AppVariable.php index d5c92c42cb..0d4d4f8223 100644 --- a/src/Symfony/Bridge/Twig/AppVariable.php +++ b/src/Symfony/Bridge/Twig/AppVariable.php @@ -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');