Skip to content

Commit da7fd54

Browse files
authored
Merge pull request #1 from iisisrael/symfony-5
Symfony 5 compatibility updates - cleanups and improvements by @iisisrael
2 parents d7ba5e4 + 24726a3 commit da7fd54

File tree

13 files changed

+48
-241
lines changed

13 files changed

+48
-241
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Propel/om/
55
Propel/map/
66
composer.lock
77
.php_cs.cache
8+
.phpunit.result.cache

Controller/AuthorizeController.php

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
3131
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
3232
use Symfony\Component\Security\Core\User\UserInterface;
33-
use Symfony\Component\Templating\EngineInterface;
33+
use Twig\Environment;
3434

3535
/**
3636
* Controller handling basic authorization.
@@ -65,9 +65,9 @@ class AuthorizeController
6565
private $oAuth2Server;
6666

6767
/**
68-
* @var EngineInterface
68+
* @var Environment
6969
*/
70-
private $templating;
70+
private $twig;
7171

7272
/**
7373
* @var RequestStack
@@ -89,11 +89,6 @@ class AuthorizeController
8989
*/
9090
private $clientManager;
9191

92-
/**
93-
* @var string
94-
*/
95-
private $templateEngineType;
96-
9792
/**
9893
* @var EventDispatcherInterface
9994
*/
@@ -109,37 +104,34 @@ class AuthorizeController
109104
* @param Form $authorizeForm
110105
* @param AuthorizeFormHandler $authorizeFormHandler
111106
* @param OAuth2 $oAuth2Server
112-
* @param EngineInterface $templating
107+
* @param Environment $twig
113108
* @param TokenStorageInterface $tokenStorage
114109
* @param UrlGeneratorInterface $router
115110
* @param ClientManagerInterface $clientManager
116111
* @param EventDispatcherInterface $eventDispatcher
117112
* @param SessionInterface $session
118-
* @param string $templateEngineType
119113
*/
120114
public function __construct(
121115
RequestStack $requestStack,
122116
Form $authorizeForm,
123117
AuthorizeFormHandler $authorizeFormHandler,
124118
OAuth2 $oAuth2Server,
125-
EngineInterface $templating,
119+
Environment $twig,
126120
TokenStorageInterface $tokenStorage,
127121
UrlGeneratorInterface $router,
128122
ClientManagerInterface $clientManager,
129123
EventDispatcherInterface $eventDispatcher,
130-
SessionInterface $session = null,
131-
$templateEngineType = 'twig'
124+
SessionInterface $session = null
132125
) {
133126
$this->requestStack = $requestStack;
134127
$this->session = $session;
135128
$this->authorizeForm = $authorizeForm;
136129
$this->authorizeFormHandler = $authorizeFormHandler;
137130
$this->oAuth2Server = $oAuth2Server;
138-
$this->templating = $templating;
131+
$this->twig = $twig;
139132
$this->tokenStorage = $tokenStorage;
140133
$this->router = $router;
141134
$this->clientManager = $clientManager;
142-
$this->templateEngineType = $templateEngineType;
143135
$this->eventDispatcher = $eventDispatcher;
144136
}
145137

@@ -183,7 +175,10 @@ public function authorizeAction(Request $request)
183175
'client' => $this->getClient(),
184176
];
185177

186-
return $this->renderAuthorize($data, $this->templating, $this->templateEngineType);
178+
return $this->twig->render(
179+
'@FOSOAuthServer/Authorize/authorize.html.twig',
180+
$data
181+
);
187182
}
188183

189184
/**
@@ -258,17 +253,6 @@ protected function getClient()
258253
return $this->client;
259254
}
260255

261-
/**
262-
* @throws \RuntimeException
263-
*/
264-
protected function renderAuthorize(array $data, EngineInterface $engine, string $engineType): string
265-
{
266-
return $engine->render(
267-
'@FOSOAuthServer/Authorize/authorize.html.'.$engineType,
268-
$data
269-
);
270-
}
271-
272256
/**
273257
* @return null|Request
274258
*/

DependencyInjection/Compiler/TokenStorageCompilerPass.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

DependencyInjection/Configuration.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public function getConfigTreeBuilder()
3333

3434
/** @var ArrayNodeDefinition $rootNode */
3535
$rootNode = $treeBuilder->getRootNode();
36-
//$treeBuilder->root('fos_oauth_server');
3736

3837
$supportedDrivers = ['orm', 'mongodb', 'propel', 'custom'];
3938

@@ -82,7 +81,6 @@ public function getConfigTreeBuilder()
8281

8382
$this->addAuthorizeSection($rootNode);
8483
$this->addServiceSection($rootNode);
85-
$this->addTemplateSection($rootNode);
8684

8785
return $treeBuilder;
8886
}
@@ -138,18 +136,4 @@ private function addServiceSection(ArrayNodeDefinition $node)
138136
->end()
139137
;
140138
}
141-
142-
private function addTemplateSection(ArrayNodeDefinition $node)
143-
{
144-
$node
145-
->children()
146-
->arrayNode('template')
147-
->addDefaultsIfNotSet()
148-
->children()
149-
->scalarNode('engine')->defaultValue('twig')->end()
150-
->end()
151-
->end()
152-
->end()
153-
;
154-
}
155139
}

FOSOAuthServerBundle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use FOS\OAuthServerBundle\DependencyInjection\Compiler\GrantExtensionsCompilerPass;
1717
use FOS\OAuthServerBundle\DependencyInjection\Compiler\RequestStackCompilerPass;
18-
use FOS\OAuthServerBundle\DependencyInjection\Compiler\TokenStorageCompilerPass;
1918
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
2019
use FOS\OAuthServerBundle\DependencyInjection\Security\Factory\OAuthFactory;
2120
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
@@ -38,7 +37,6 @@ public function build(ContainerBuilder $container)
3837
$extension->addSecurityListenerFactory(new OAuthFactory());
3938

4039
$container->addCompilerPass(new GrantExtensionsCompilerPass());
41-
$container->addCompilerPass(new TokenStorageCompilerPass());
4240
$container->addCompilerPass(new RequestStackCompilerPass());
4341
}
4442
}

Resources/config/authorize.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@
2828
<argument type="service" id="fos_oauth_server.authorize.form" />
2929
<argument type="service" id="fos_oauth_server.authorize.form.handler" />
3030
<argument type="service" id="fos_oauth_server.server" />
31-
<!-- <argument type="service" id="templating" /> -->
31+
<argument type="service" id="twig" />
3232
<argument type="service" id="security.token_storage" />
3333
<argument type="service" id="router" />
3434
<argument type="service" id="fos_oauth_server.client_manager" />
3535
<argument type="service" id="event_dispatcher" />
3636
<argument type="service" id="session" on-invalid="null" />
37-
<argument>%fos_oauth_server.template.engine%</argument>
3837
</service>
3938
</services>
4039

Security/Firewall/OAuthListener.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,25 @@ public function __invoke(RequestEvent $event)
6565
$this->handle($event);
6666
}
6767

68-
public function handle(RequestEvent $event)
68+
public function handle(RequestEvent $event): void
6969
{
70-
//TODO - This method needs to be re-examined for inconsistent return types
71-
7270
if (null === $oauthToken = $this->serverService->getBearerToken($event->getRequest(), true)) {
73-
//return;
7471
throw new LogicException('Token for event was null');
7572
}
7673

7774
$token = new OAuthToken();
7875
$token->setToken($oauthToken);
7976

8077
try {
81-
$returnValue = $this->authenticationManager->authenticate($token);
78+
$authenticateResult = $this->authenticationManager->authenticate($token);
8279

83-
if ($returnValue instanceof TokenInterface) {
84-
return $this->tokenStorage->setToken($returnValue);
85-
}
80+
if ($authenticateResult instanceof TokenInterface) {
81+
82+
$this->tokenStorage->setToken($authenticateResult);
83+
84+
} elseif ($authenticateResult instanceof Response) {
8685

87-
if ($returnValue instanceof Response) {
88-
return $event->setResponse($returnValue);
86+
$event->setResponse($authenticateResult);
8987
}
9088
} catch (AuthenticationException $e) {
9189
if (null !== $p = $e->getPrevious()) {

Tests/Controller/AuthorizeControllerTest.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use PHPUnit\Framework\MockObject\MockObject;
2323
use PHPUnit\Framework\TestCase;
2424
use ReflectionProperty;
25-
use Symfony\Component\Templating\EngineInterface;
2625
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2726
use Symfony\Component\Form\Form;
2827
use Symfony\Component\Form\FormView;
@@ -36,6 +35,7 @@
3635
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
3736
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
3837
use Symfony\Component\Security\Core\User\UserInterface;
38+
use Twig\Environment;
3939

4040
class AuthorizeControllerTest extends TestCase
4141
{
@@ -65,9 +65,9 @@ class AuthorizeControllerTest extends TestCase
6565
protected $oAuth2Server;
6666

6767
/**
68-
* @var MockObject|EngineInterface
68+
* @var MockObject|Environment
6969
*/
70-
protected $templateEngine;
70+
protected $twig;
7171

7272
/**
7373
* @var MockObject|TokenStorageInterface
@@ -89,11 +89,6 @@ class AuthorizeControllerTest extends TestCase
8989
*/
9090
protected $eventDispatcher;
9191

92-
/**
93-
* @var string
94-
*/
95-
protected $templateEngineType;
96-
9792
/**
9893
* @var AuthorizeController
9994
*/
@@ -152,7 +147,7 @@ public function setUp() : void
152147
->disableOriginalConstructor()
153148
->getMock()
154149
;
155-
$this->templateEngine = $this->getMockBuilder(EngineInterface::class)
150+
$this->twig = $this->getMockBuilder(Environment::class)
156151
->disableOriginalConstructor()
157152
->getMock()
158153
;
@@ -176,20 +171,18 @@ public function setUp() : void
176171
->disableOriginalConstructor()
177172
->getMock()
178173
;
179-
$this->templateEngineType = 'twig';
180174

181175
$this->instance = new AuthorizeController(
182176
$this->requestStack,
183177
$this->form,
184178
$this->authorizeFormHandler,
185179
$this->oAuth2Server,
186-
$this->templateEngine,
180+
$this->twig,
187181
$this->tokenStorage,
188182
$this->router,
189183
$this->clientManager,
190184
$this->eventDispatcher,
191-
$this->session,
192-
$this->templateEngineType
185+
$this->session
193186
);
194187

195188
/** @var MockObject&Request $request */
@@ -312,7 +305,7 @@ public function testAuthorizeActionWillRenderTemplate(): void
312305

313306
$response = '';
314307

315-
$this->templateEngine
308+
$this->twig
316309
->expects($this->at(0))
317310
->method('render')
318311
->with(
@@ -471,7 +464,7 @@ public function testAuthorizeActionWillEnsureLogout(): void
471464

472465
$response = '';
473466

474-
$this->templateEngine
467+
$this->twig
475468
->expects($this->at(0))
476469
->method('render')
477470
->with(

0 commit comments

Comments
 (0)