Skip to content

Commit 3453b8f

Browse files
committed
Use a compiler pass to inject the Container only when necessary
1 parent 009db4f commit 3453b8f

File tree

6 files changed

+117
-27
lines changed

6 files changed

+117
-27
lines changed

Controller/AuthorizeController.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function setContainer(ContainerInterface $container = null)
5656
*/
5757
public function authorizeAction(Request $request)
5858
{
59-
$user = $this->container->get('security.context')->getToken()->getUser();
59+
$user = $this->getTokenStorage()->getToken()->getUser();
6060

6161
if (!$user instanceof UserInterface) {
6262
throw new AccessDeniedException('This user does not have access to this section.');
@@ -106,7 +106,7 @@ public function authorizeAction(Request $request)
106106
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request)
107107
{
108108
if (true === $this->container->get('session')->get('_fos_oauth_server.ensure_logout')) {
109-
$this->container->get('security.context')->setToken(null);
109+
$this->getTokenStorage()->setToken(null);
110110
$this->container->get('session')->invalidate();
111111
}
112112

@@ -146,11 +146,7 @@ protected function getRedirectionUrl(UserInterface $user)
146146
protected function getClient()
147147
{
148148
if (null === $this->client) {
149-
try {
150-
$request = $this->container->get('request_stack')->getCurrentRequest();
151-
} catch (ServiceNotFoundException $e) {
152-
$request = $this->container->get('request');
153-
}
149+
$request = $this->getCurrentRequest();
154150

155151
$client = null;
156152
if (null !== $request) {
@@ -173,4 +169,26 @@ protected function getClient()
173169

174170
return $this->client;
175171
}
172+
173+
private function getCurrentRequest() {
174+
if($this->container->has('request_stack')) {
175+
$request = $this->container->get('request_stack')->getCurrentRequest();
176+
if(null === $request) {
177+
throw new \RuntimeException('No current request.');
178+
}
179+
180+
return $request;
181+
} else {
182+
return $this->container->get('request');
183+
}
184+
}
185+
186+
private function getTokenStorage()
187+
{
188+
if($this->container->has('security.token_storage')) {
189+
return $this->container->get('security.token_storage');
190+
}
191+
192+
return $this->container->get('security.context');
193+
}
176194
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSOAuthServerBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\OAuthServerBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* @author Ener-Getick <[email protected]>
20+
*
21+
* @internal
22+
*/
23+
final class RequestStackCompilerPass implements CompilerPassInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function process(ContainerBuilder $container)
29+
{
30+
if($container->has('request_stack')) {
31+
return;
32+
}
33+
34+
$definition = $container->getDefinition('fos_oauth_server.authorize.form.handler.default');
35+
$definition->addMethodCall('setContainer', array(new Reference('service_container')));
36+
}
37+
}

FOSOAuthServerBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\OAuthServerBundle;
1313

1414
use FOS\OAuthServerBundle\DependencyInjection\Compiler\TokenStorageCompilerPass;
15+
use FOS\OAuthServerBundle\DependencyInjection\Compiler\RequestStackCompilerPass;
1516
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
1617
use FOS\OAuthServerBundle\DependencyInjection\Security\Factory\OAuthFactory;
1718
use FOS\OAuthServerBundle\DependencyInjection\Compiler\GrantExtensionsCompilerPass;
@@ -37,5 +38,6 @@ public function build(ContainerBuilder $container)
3738

3839
$container->addCompilerPass(new GrantExtensionsCompilerPass());
3940
$container->addCompilerPass(new TokenStorageCompilerPass());
41+
$container->addCompilerPass(new RequestStackCompilerPass());
4042
}
4143
}

Form/Handler/AuthorizeFormHandler.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111

1212
namespace FOS\OAuthServerBundle\Form\Handler;
1313

14-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1514
use Symfony\Component\DependencyInjection\ContainerInterface;
1615
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1716
use Symfony\Component\Form\FormInterface;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\RequestStack;
1819
use FOS\OAuthServerBundle\Form\Model\Authorize;
1920

2021
/**
2122
* @author Chris Jones <[email protected]>
2223
*/
23-
class AuthorizeFormHandler implements ContainerAwareInterface
24+
class AuthorizeFormHandler
2425
{
2526
/**
2627
* @var FormInterface
@@ -32,6 +33,25 @@ class AuthorizeFormHandler implements ContainerAwareInterface
3233
*/
3334
protected $container;
3435

36+
/**
37+
* @var RequestStack|Request|null
38+
*/
39+
private $requestStack;
40+
41+
/**
42+
* @param FormInterface $form
43+
* @param Request|RequestStack $requestStack
44+
*/
45+
public function __construct(FormInterface $form, $requestStack = null)
46+
{
47+
if(null !== $requestStack && !$requestStack instanceof RequestStack && !$requestStack instanceof Request) {
48+
throw new \InvalidArgumentException(sprintf('Argument 2 of %s must be an instanceof RequestStack or Request', __CLASS__));
49+
}
50+
51+
$this->form = $form;
52+
$this->requestStack = $requestStack;
53+
}
54+
3555
/**
3656
* Sets the container.
3757
*
@@ -42,11 +62,6 @@ public function setContainer(ContainerInterface $container = null)
4262
$this->container = $container;
4363
}
4464

45-
public function __construct(FormInterface $form)
46-
{
47-
$this->form = $form;
48-
}
49-
5065
public function isAccepted()
5166
{
5267
return $this->form->getData()->accepted;
@@ -59,12 +74,7 @@ public function isRejected()
5974

6075
public function process()
6176
{
62-
try {
63-
$request = $this->container->get('request_stack')->getCurrentRequest();
64-
} catch (ServiceNotFoundException $e) {
65-
$request = $this->container->get('request');
66-
}
67-
77+
$request = $this->getCurrentRequest();
6878
if (null !== $request) {
6979
$this->form->setData(new Authorize(
7080
$request->request->has('accepted'),
@@ -89,6 +99,15 @@ public function getScope()
8999
return $this->form->getData()->scope;
90100
}
91101

102+
public function __get($name)
103+
{
104+
if ($name === 'request') {
105+
@trigger_error(sprintf('%s::$request is deprecated since 1.4 and will be removed in 2.0.', __CLASS__), E_USER_DEPRECATED);
106+
107+
return $this->getCurrentRequest();
108+
}
109+
}
110+
92111
/**
93112
* Put form data in $_GET so that OAuth2 library will call Request::createFromGlobals()
94113
*
@@ -105,4 +124,17 @@ protected function onSuccess()
105124
'scope' => $this->form->getData()->scope,
106125
);
107126
}
127+
128+
private function getCurrentRequest()
129+
{
130+
if (null !== $this->requestStack) {
131+
if ($this->requestStack instanceof Request) {
132+
return $this->requestStack;
133+
} else {
134+
return $this->requestStack->getCurrentRequest();
135+
}
136+
}
137+
138+
return $this->container->get('request');
139+
}
108140
}

Resources/config/authorize.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
<service id="fos_oauth_server.authorize.form.handler.default" class="FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler">
2222
<argument type="service" id="fos_oauth_server.authorize.form" />
23+
<argument type="service" id="request_stack" on-invalid="ignore" />
2324
</service>
2425
</services>
2526

Security/Firewall/OAuthListener.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,24 @@
3030
class OAuthListener implements ListenerInterface
3131
{
3232
/**
33-
* @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|\Symfony\Component\Security\Core\SecurityContextInterface
33+
* @var TokenStorageInterface|SecurityContextInterface
3434
*/
3535
protected $securityContext;
3636

3737
/**
38-
* @var \Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface
38+
* @var AuthenticationManagerInterface
3939
*/
4040
protected $authenticationManager;
4141

4242
/**
43-
* @var \OAuth2\OAuth2
43+
* @var OAuth2
4444
*/
4545
protected $serverService;
4646

4747
/**
48-
* @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface|\Symfony\Component\Security\Core\SecurityContextInterface $tokenStorage The token storage.
49-
* @param \Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface $authenticationManager The authentication manager.
50-
* @param \OAuth2\OAuth2 $serverService
48+
* @param TokenStorageInterface|SecurityContextInterface $tokenStorage The token storage.
49+
* @param AuthenticationManagerInterface $authenticationManager The authentication manager.
50+
* @param OAuth2 $serverService
5151
*/
5252
public function __construct($securityContext, AuthenticationManagerInterface $authenticationManager, OAuth2 $serverService)
5353
{
@@ -60,7 +60,7 @@ public function __construct($securityContext, AuthenticationManagerInterface $au
6060
}
6161

6262
/**
63-
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event The event.
63+
* @param GetResponseEvent $event The event.
6464
*/
6565
public function handle(GetResponseEvent $event)
6666
{

0 commit comments

Comments
 (0)