Augmentation vers version 3.3.0
This commit is contained in:
98
vendor/symfony/twig-bridge/AppVariable.php
vendored
98
vendor/symfony/twig-bridge/AppVariable.php
vendored
@@ -11,12 +11,11 @@
|
||||
|
||||
namespace Symfony\Bridge\Twig;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\SecurityContext;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
|
||||
/**
|
||||
* Exposes some Symfony parameters and services as an "app" global variable.
|
||||
@@ -25,20 +24,11 @@ use Symfony\Component\Security\Core\SecurityContext;
|
||||
*/
|
||||
class AppVariable
|
||||
{
|
||||
private $container;
|
||||
private $tokenStorage;
|
||||
private $requestStack;
|
||||
private $environment;
|
||||
private $debug;
|
||||
|
||||
/**
|
||||
* @deprecated since version 2.7, to be removed in 3.0.
|
||||
*/
|
||||
public function setContainer(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function setTokenStorage(TokenStorageInterface $tokenStorage)
|
||||
{
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
@@ -60,52 +50,41 @@ class AppVariable
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the security context service.
|
||||
* Returns the current token.
|
||||
*
|
||||
* @deprecated since version 2.6, to be removed in 3.0.
|
||||
* @return TokenInterface|null
|
||||
*
|
||||
* @return SecurityContext|null The security context
|
||||
* @throws \RuntimeException When the TokenStorage is not available
|
||||
*/
|
||||
public function getSecurity()
|
||||
public function getToken()
|
||||
{
|
||||
@trigger_error('The "app.security" variable is deprecated since Symfony 2.6 and will be removed in 3.0.', E_USER_DEPRECATED);
|
||||
|
||||
if (null === $this->container) {
|
||||
throw new \RuntimeException('The "app.security" variable is not available.');
|
||||
if (null === $tokenStorage = $this->tokenStorage) {
|
||||
throw new \RuntimeException('The "app.token" variable is not available.');
|
||||
}
|
||||
|
||||
if ($this->container->has('security.context')) {
|
||||
return $this->container->get('security.context');
|
||||
}
|
||||
return $tokenStorage->getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current user.
|
||||
*
|
||||
* @return mixed
|
||||
* @return object|null
|
||||
*
|
||||
* @see TokenInterface::getUser()
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
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');
|
||||
if (null === $tokenStorage = $this->tokenStorage) {
|
||||
throw new \RuntimeException('The "app.user" variable is not available.');
|
||||
}
|
||||
|
||||
if (!$token = $this->tokenStorage->getToken()) {
|
||||
return;
|
||||
if (!$token = $tokenStorage->getToken()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$user = $token->getUser();
|
||||
if (\is_object($user)) {
|
||||
return $user;
|
||||
}
|
||||
|
||||
return \is_object($user) ? $user : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,11 +95,7 @@ class AppVariable
|
||||
public function getRequest()
|
||||
{
|
||||
if (null === $this->requestStack) {
|
||||
if (null === $this->container) {
|
||||
throw new \RuntimeException('The "app.request" variable is not available.');
|
||||
}
|
||||
|
||||
$this->requestStack = $this->container->get('request_stack');
|
||||
throw new \RuntimeException('The "app.request" variable is not available.');
|
||||
}
|
||||
|
||||
return $this->requestStack->getCurrentRequest();
|
||||
@@ -133,13 +108,11 @@ class AppVariable
|
||||
*/
|
||||
public function getSession()
|
||||
{
|
||||
if (null === $this->requestStack && null === $this->container) {
|
||||
if (null === $this->requestStack) {
|
||||
throw new \RuntimeException('The "app.session" variable is not available.');
|
||||
}
|
||||
|
||||
if ($request = $this->getRequest()) {
|
||||
return $request->getSession();
|
||||
}
|
||||
return ($request = $this->getRequest()) ? $request->getSession() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,4 +142,39 @@ class AppVariable
|
||||
|
||||
return $this->debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns some or all the existing flash messages:
|
||||
* * getFlashes() returns all the flash messages
|
||||
* * getFlashes('notice') returns a simple array with flash messages of that type
|
||||
* * getFlashes(['notice', 'error']) returns a nested array of type => messages.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFlashes($types = null)
|
||||
{
|
||||
try {
|
||||
$session = $this->getSession();
|
||||
if (null === $session) {
|
||||
return [];
|
||||
}
|
||||
} catch (\RuntimeException $e) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (null === $types || '' === $types || [] === $types) {
|
||||
return $session->getFlashBag()->all();
|
||||
}
|
||||
|
||||
if (\is_string($types)) {
|
||||
return $session->getFlashBag()->get($types);
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($types as $type) {
|
||||
$result[$type] = $session->getFlashBag()->get($type);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user