Skip to content

Commit

Permalink
Revert "Remove templating engine (FriendsOfSymfony#653)"
Browse files Browse the repository at this point in the history
This reverts commit d8f41fe.

This is needed since Mautic 4 still uses the Template Engine.
  • Loading branch information
dennisameling committed Mar 28, 2021
1 parent 446f0c5 commit d3cbd3a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 42 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ This changelog references the relevant changes done in 6.0 versions.
* **[BC break]** Removed class `FOS\OAuthServerBundle\Event\OAuthEvent`, use dedicated event classes instead: [[#655](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/655)]
- `OAuthEvent::PRE_AUTHORIZATION_PROCESS` => `FOS\OAuthServerBundle\Event\PreAuthorizationEvent`
- `OAuthEvent::POST_AUTHORIZATION_PROCESS` => `FOS\OAuthServerBundle\Event\PostAuthorizationEvent`
* **[BC break]** Removed support for templating engine [[#653](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/653)]
* **[BC break]** Removed support for templating engine [[#653](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/pull/653)]
* **NOTE: We added this back just for Mautic 4**

### 2.0.0-ALPHA0 (2018-05-01)

Expand Down
44 changes: 29 additions & 15 deletions Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use OAuth2\OAuth2;
use OAuth2\OAuth2ServerException;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -31,7 +32,6 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Twig\Environment as TwigEnvironment;

/**
* Controller handling basic authorization.
Expand Down Expand Up @@ -65,6 +65,11 @@ class AuthorizeController
*/
private $oAuth2Server;

/**
* @var EngineInterface
*/
private $templating;

/**
* @var RequestStack
*/
Expand All @@ -75,11 +80,6 @@ class AuthorizeController
*/
private $tokenStorage;

/**
* @var TwigEnvironment
*/
private $twig;

/**
* @var UrlGeneratorInterface
*/
Expand All @@ -90,6 +90,11 @@ class AuthorizeController
*/
private $clientManager;

/**
* @var string
*/
private $templateEngineType;

/**
* @var EventDispatcherInterface
*/
Expand All @@ -102,29 +107,32 @@ class AuthorizeController
* @todo This controller could be refactored to not rely on so many dependencies
*
* @param SessionInterface $session
* @param string $templateEngineType
*/
public function __construct(
RequestStack $requestStack,
Form $authorizeForm,
AuthorizeFormHandler $authorizeFormHandler,
OAuth2 $oAuth2Server,
EngineInterface $templating,
TokenStorageInterface $tokenStorage,
UrlGeneratorInterface $router,
ClientManagerInterface $clientManager,
EventDispatcherInterface $eventDispatcher,
TwigEnvironment $twig,
SessionInterface $session = null
SessionInterface $session = null,
$templateEngineType = 'twig'
) {
$this->requestStack = $requestStack;
$this->session = $session;
$this->authorizeForm = $authorizeForm;
$this->authorizeFormHandler = $authorizeFormHandler;
$this->oAuth2Server = $oAuth2Server;
$this->templating = $templating;
$this->tokenStorage = $tokenStorage;
$this->router = $router;
$this->clientManager = $clientManager;
$this->templateEngineType = $templateEngineType;
$this->eventDispatcher = $eventDispatcher;
$this->twig = $twig;
}

/**
Expand Down Expand Up @@ -159,10 +167,12 @@ public function authorizeAction(Request $request)
return $this->processSuccess($user, $formHandler, $request);
}

return $this->renderAuthorize([
$data = [
'form' => $form->createView(),
'client' => $this->getClient(),
]);
];

return $this->renderAuthorize($data, $this->templating, $this->templateEngineType);
}

/**
Expand Down Expand Up @@ -202,7 +212,7 @@ protected function getRedirectionUrl(UserInterface $user)
}

/**
* @return ClientInterface
* @return ClientInterface
*/
protected function getClient()
{
Expand All @@ -228,10 +238,14 @@ protected function getClient()
return $this->client;
}

protected function renderAuthorize(array $context): Response
/**
* @throws \RuntimeException
*/
protected function renderAuthorize(array $data, EngineInterface $engine, string $engineType): Response
{
return new Response(
$this->twig->render('@FOSOAuthServer/Authorize/authorize.html.twig', $context)
return $engine->renderResponse(
'@FOSOAuthServer/Authorize/authorize.html.'.$engineType,
$data
);
}

Expand Down
15 changes: 15 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function getConfigTreeBuilder()

$this->addAuthorizeSection($rootNode);
$this->addServiceSection($rootNode);
$this->addTemplateSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -134,4 +135,18 @@ private function addServiceSection(ArrayNodeDefinition $node)
->end()
;
}

private function addTemplateSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('template')
->addDefaultsIfNotSet()
->children()
->scalarNode('engine')->defaultValue('twig')->end()
->end()
->end()
->end()
;
}
}
1 change: 1 addition & 0 deletions DependencyInjection/FOSOAuthServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function load(array $configs, ContainerBuilder $container)
'refresh_token_class' => 'fos_oauth_server.model.refresh_token.class',
'auth_code_class' => 'fos_oauth_server.model.auth_code.class',
],
'template' => 'fos_oauth_server.template.%s',
]);

// Handle the MongoDB document manager name in a specific way as it does not have a registry to make it easy
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/authorize.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
<argument type="service" id="fos_oauth_server.authorize.form" />
<argument type="service" id="fos_oauth_server.authorize.form.handler" />
<argument type="service" id="fos_oauth_server.server" />
<argument type="service" id="templating" />
<argument type="service" id="security.token_storage" />
<argument type="service" id="router" />
<argument type="service" id="fos_oauth_server.client_manager" />
<argument type="service" id="event_dispatcher" />
<argument type="service" id="twig" />
<argument type="service" id="session" on-invalid="null" />
<argument>%fos_oauth_server.template.engine%</argument>
</service>
</services>

Expand Down
49 changes: 30 additions & 19 deletions Tests/Controller/AuthorizeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use OAuth2\OAuth2;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormView;
Expand All @@ -33,7 +34,6 @@
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Twig\Environment as TwigEnvironment;

class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -63,14 +63,14 @@ class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
protected $oAuth2Server;

/**
* @var \PHPUnit\Framework\MockObject\MockObject|TokenStorageInterface
* @var \PHPUnit\Framework\MockObject\MockObject|EngineInterface
*/
protected $tokenStorage;
protected $templateEngine;

/**
* @var \PHPUnit\Framework\MockObject\MockObject|TwigEnvironment
* @var \PHPUnit\Framework\MockObject\MockObject|TokenStorageInterface
*/
protected $twig;
protected $tokenStorage;

/**
* @var \PHPUnit\Framework\MockObject\MockObject|UrlGeneratorInterface
Expand All @@ -87,6 +87,11 @@ class AuthorizeControllerTest extends \PHPUnit\Framework\TestCase
*/
protected $eventDispatcher;

/**
* @var string
*/
protected $templateEngineType;

/**
* @var AuthorizeController
*/
Expand Down Expand Up @@ -150,11 +155,11 @@ public function setUp(): void
->disableOriginalConstructor()
->getMock()
;
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)
$this->templateEngine = $this->getMockBuilder(EngineInterface::class)
->disableOriginalConstructor()
->getMock()
;
$this->twig = $this->getMockBuilder(TwigEnvironment::class)
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)
->disableOriginalConstructor()
->getMock()
;
Expand All @@ -174,18 +179,20 @@ public function setUp(): void
->disableOriginalConstructor()
->getMock()
;
$this->templateEngineType = 'twig';

$this->instance = new AuthorizeController(
$this->requestStack,
$this->form,
$this->authorizeFormHandler,
$this->oAuth2Server,
$this->templateEngine,
$this->tokenStorage,
$this->router,
$this->clientManager,
$this->eventDispatcher,
$this->twig,
$this->session
$this->session,
$this->templateEngineType
);

/** @var \PHPUnit\Framework\MockObject\MockObject&Request $request */
Expand Down Expand Up @@ -309,20 +316,22 @@ public function testAuthorizeActionWillRenderTemplate(): void
->willReturn($this->formView)
;

$this->twig
->expects($this->once())
->method('render')
$response = new Response();

$this->templateEngine
->expects($this->at(0))
->method('renderResponse')
->with(
'@FOSOAuthServer/Authorize/authorize.html.twig',
[
'form' => $this->formView,
'client' => $this->client,
]
)
->willReturn($responseBody = 'response')
->willReturn($response)
;

$this->assertSame($responseBody, $this->instance->authorizeAction($this->request)->getContent());
$this->assertSame($response, $this->instance->authorizeAction($this->request));
}

public function testAuthorizeActionWillFinishClientAuthorization(): void
Expand Down Expand Up @@ -464,20 +473,22 @@ public function testAuthorizeActionWillEnsureLogout(): void
->willReturn($this->formView)
;

$this->twig
->expects($this->once())
->method('render')
$response = new Response();

$this->templateEngine
->expects($this->at(0))
->method('renderResponse')
->with(
'@FOSOAuthServer/Authorize/authorize.html.twig',
[
'form' => $this->formView,
'client' => $this->client,
]
)
->willReturn($responseBody = 'response')
->willReturn($response)
;

$this->assertSame($responseBody, $this->instance->authorizeAction($this->request)->getContent());
$this->assertSame($response, $this->instance->authorizeAction($this->request));
}

public function testAuthorizeActionWillProcessAuthorizationForm(): void
Expand Down
6 changes: 2 additions & 4 deletions Tests/Functional/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
framework:
templating:
engines: ["twig"]
form: ~
secret: test
router:
resource: '%kernel.project_dir%/Tests/Functional/config/routing.yml'

twig:
exception_controller: null
strict_variables: '%kernel.debug%'

fos_oauth_server:

security:
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"friendsofsymfony/oauth2-php": "~1.1",
"symfony/dependency-injection": "^4.4 || ^5.1",
"symfony/framework-bundle": "^4.4 || ^5.1",
"symfony/security-bundle": "^4.4 || ^5.1",
"symfony/twig-bundle": "^4.4 || ^5.1"
"symfony/security-bundle": "^4.4 || ^5.1"
},
"conflict": {
"twig/twig": "<1.40 || >=2.0,<2.9"
Expand All @@ -43,6 +42,8 @@
"symfony/console": "^4.4 || ^5.1",
"symfony/form": "^4.4 || ^5.1",
"symfony/phpunit-bridge": "^4.4 || ^5.1",
"symfony/templating": "^4.4 || ^5.1",
"symfony/twig-bundle": "^4.4 || ^5.1",
"symfony/yaml": "^4.4 || ^5.1",
"willdurand/propel-typehintable-behavior": "~1.0"
},
Expand Down

0 comments on commit d3cbd3a

Please sign in to comment.