Skip to content

Commit c025666

Browse files
committed
Merge pull request #183 from symfony-cmf/performance_improvement
Save services to improve performance
2 parents 4e5ed74 + 1a24acd commit c025666

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

PublishWorkflow/PublishWorkflowChecker.php

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1616
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17+
use Symfony\Component\Security\Core\Authentication\Token\TokenStorageInterface;
1718
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
19+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1820
use Symfony\Component\Security\Core\SecurityContextInterface;
1921

2022
/**
@@ -75,6 +77,16 @@ class PublishWorkflowChecker implements SecurityContextInterface
7577
*/
7678
private $token;
7779

80+
/**
81+
* @var TokenStorageInterface
82+
*/
83+
private $tokenStorage = false;
84+
85+
/**
86+
* @var AuthorizationCheckerInterface
87+
*/
88+
private $authorizationChecker = false;
89+
7890
/**
7991
* @param ContainerInterface $container To get the security context from.
8092
* @param AccessDecisionManagerInterface $accessDecisionManager Service to do the actual decision.
@@ -98,16 +110,15 @@ public function __construct(ContainerInterface $container, AccessDecisionManager
98110
*/
99111
public function getToken()
100112
{
101-
if (null === $this->token) {
102-
if ($this->container->has('security.token_storage')) {
103-
return $this->container->get('security.token_storage')->getToken();
104-
} elseif ($this->container->has('security.context')) {
105-
// to be BC with Symfony 2.3
106-
return $this->container->get('security.context')->getToken();
107-
}
113+
if (null !== $this->token) {
114+
return $this->token;
115+
}
116+
117+
if (null === $this->getTokenStorage()) {
118+
return;
108119
}
109120

110-
return $this->token;
121+
return $this->getTokenStorage()->getToken();
111122
}
112123

113124
/**
@@ -139,20 +150,11 @@ public function isGranted($attributes, $object = null)
139150
$attributes = array($attributes);
140151
}
141152

142-
$tokenStorage = $authorizationChecker = null;
143-
if ($this->container->has('security.token_storage')) {
144-
$tokenStorage = $this->container->get('security.token_storage');
145-
$authorizationChecker = $this->container->get('security.authorization_checker');
146-
} elseif ($this->container->has('security.context')) {
147-
// to be BC with Symfony <2.6
148-
$authorizationChecker = $tokenStorage = $this->container->get('security.context');
149-
}
150-
151-
if ((count($attributes) === 1)
153+
if (1 === count($attributes)
152154
&& self::VIEW_ATTRIBUTE === reset($attributes)
153-
&& null !== $tokenStorage
154-
&& null !== $tokenStorage->getToken()
155-
&& $authorizationChecker->isGranted($this->bypassingRole)
155+
&& null !== $this->getTokenStorage()
156+
&& null !== $this->getTokenStorage()->getToken()
157+
&& $this->getAuthorizationChecker()->isGranted($this->bypassingRole)
156158
) {
157159
return true;
158160
}
@@ -166,4 +168,36 @@ public function isGranted($attributes, $object = null)
166168

167169
return $this->accessDecisionManager->decide($token, $attributes, $object);
168170
}
171+
172+
private function getTokenStorage()
173+
{
174+
if (false === $this->tokenStorage) {
175+
if ($this->container->has('security.token_storage')) {
176+
$this->tokenStorage = $this->container->get('security.token_storage');
177+
} elseif ($this->container->has('security.context')) {
178+
// for Symfony <2.6 compatibility
179+
$this->tokenStorage = $this->container->get('security.context');
180+
} else {
181+
$this->tokenStorage = null;
182+
}
183+
}
184+
185+
return $this->tokenStorage;
186+
}
187+
188+
private function getAuthorizationChecker()
189+
{
190+
if (false === $this->authorizationChecker) {
191+
if ($this->container->has('security.authorization_checker')) {
192+
$this->authorizationChecker = $this->container->get('security.authorization_checker');
193+
} elseif ($this->container->has('security.context')) {
194+
// for Symfony <2.6 compatibility
195+
$this->authorizationChecker = $this->container->get('security.context');
196+
} else {
197+
$this->authorizationChecker = null;
198+
}
199+
}
200+
201+
return $this->authorizationChecker;
202+
}
169203
}

Tests/Unit/PublishWorkflow/PublishWorkflowCheckerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function setUp()
6666
// assuming Symfony <2.6
6767
$this->container->shouldReceive('has')->with('security.context')->andReturn(true);
6868
$this->container->shouldReceive('has')->with('security.token_storage')->andReturn(false);
69+
$this->container->shouldReceive('has')->with('security.authorization_checker')->andReturn(false);
6970
}
7071

7172
protected function tearDown()
@@ -159,6 +160,7 @@ public function testTokenStorageAndAuthenticationManager()
159160
$container->shouldReceive('get')->with('security.token_storage')->andReturn($ts);
160161
$container->shouldReceive('get')->with('security.authorization_checker')->andReturn($ac);
161162
$container->shouldReceive('has')->with('security.token_storage')->andReturn(true);
163+
$container->shouldReceive('has')->with('security.authorization_checker')->andReturn(true);
162164

163165
$ts->shouldReceive('getToken')->andReturn($token);
164166
$ac->shouldReceive('isGranted')->with($this->role)->andReturn(true);

0 commit comments

Comments
 (0)