|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\FrameworkBundle\Templating; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* GlobalVariables is the entry point for Symfony global variables in Twig templates. |
|
|
18 |
* |
|
|
19 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
20 |
*/ |
|
|
21 |
class GlobalVariables |
|
|
22 |
{ |
|
|
23 |
protected $container; |
|
|
24 |
|
|
|
25 |
public function __construct(ContainerInterface $container) |
|
|
26 |
{ |
|
|
27 |
$this->container = $container; |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
/** |
|
|
31 |
* Returns the security context service. |
|
|
32 |
* |
|
|
33 |
* @return Symfony\Component\Security\Core\SecurityContext|void The security context |
|
|
34 |
*/ |
|
|
35 |
public function getSecurity() |
|
|
36 |
{ |
|
|
37 |
if ($this->container->has('security.context')) { |
|
|
38 |
return $this->container->get('security.context'); |
|
|
39 |
} |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
/** |
|
|
43 |
* Returns the current user. |
|
|
44 |
* |
|
|
45 |
* @return mixed|void |
|
|
46 |
* |
|
|
47 |
* @see Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUser() |
|
|
48 |
*/ |
|
|
49 |
public function getUser() |
|
|
50 |
{ |
|
|
51 |
if (!$security = $this->getSecurity()) { |
|
|
52 |
return; |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
if (!$token = $security->getToken()) { |
|
|
56 |
return; |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
$user = $token->getUser(); |
|
|
60 |
if (!is_object($user)) { |
|
|
61 |
return; |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
return $user; |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
/** |
|
|
68 |
* Returns the current request. |
|
|
69 |
* |
|
|
70 |
* @return Symfony\Component\HttpFoundation\Request|void The http request object |
|
|
71 |
*/ |
|
|
72 |
public function getRequest() |
|
|
73 |
{ |
|
|
74 |
if ($this->container->has('request') && $request = $this->container->get('request')) { |
|
|
75 |
return $request; |
|
|
76 |
} |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
/** |
|
|
80 |
* Returns the current session. |
|
|
81 |
* |
|
|
82 |
* @return Symfony\Component\HttpFoundation\Session|void The session |
|
|
83 |
*/ |
|
|
84 |
public function getSession() |
|
|
85 |
{ |
|
|
86 |
if ($request = $this->getRequest()) { |
|
|
87 |
return $request->getSession(); |
|
|
88 |
} |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
/** |
|
|
92 |
* Returns the current app environment. |
|
|
93 |
* |
|
|
94 |
* @return string The current environment string (e.g 'dev') |
|
|
95 |
*/ |
|
|
96 |
public function getEnvironment() |
|
|
97 |
{ |
|
|
98 |
return $this->container->getParameter('kernel.environment'); |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
/** |
|
|
102 |
* Returns the current app debug mode. |
|
|
103 |
* |
|
|
104 |
* @return Boolean The current debug mode |
|
|
105 |
*/ |
|
|
106 |
public function getDebug() |
|
|
107 |
{ |
|
|
108 |
return (Boolean) $this->container->getParameter('kernel.debug'); |
|
|
109 |
} |
|
|
110 |
} |