Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Add lazy Renderer service to speedup page load time
Browse files Browse the repository at this point in the history
  • Loading branch information
umpirsky committed Aug 17, 2016
1 parent dd8ea15 commit 41ad4f8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 78 deletions.
9 changes: 7 additions & 2 deletions Resources/config/security.xml
Expand Up @@ -12,6 +12,7 @@
<parameter key="scheb_two_factor.security_voter.class">Scheb\TwoFactorBundle\Security\TwoFactor\Voter</parameter>
<parameter key="scheb_two_factor.authentication_context.class">Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContext</parameter>
<parameter key="scheb_two_factor.authentication_context_factory.class">Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextFactory</parameter>
<parameter key="scheb_two_factor.security.renderer.class">Scheb\TwoFactorBundle\Security\TwoFactor\Renderer</parameter>
</parameters>
<services>
<service id="scheb_two_factor.session_flag_manager" class="%scheb_two_factor.session_flag_manager.class%">
Expand All @@ -32,13 +33,13 @@
</service>
<service id="scheb_two_factor.trusted_token_generator" class="%scheb_two_factor.trusted_token_generator.class%">
</service>
<service id="scheb_two_factor.trusted_filter" class="%scheb_two_factor.trusted_filter.class%" lazy="true">
<service id="scheb_two_factor.trusted_filter" class="%scheb_two_factor.trusted_filter.class%">
<argument type="service" id="scheb_two_factor.provider_registry" />
<argument type="service" id="scheb_two_factor.trusted_cookie_manager" />
<argument>%scheb_two_factor.trusted_computer.enabled%</argument>
<argument>%scheb_two_factor.parameter_names.trusted%</argument>
</service>
<service id="scheb_two_factor.provider_registry" class="%scheb_two_factor.provider_registry.class%" lazy="true">
<service id="scheb_two_factor.provider_registry" class="%scheb_two_factor.provider_registry.class%">
<argument type="service" id="scheb_two_factor.session_flag_manager" />
<argument /> <!-- Two-factor providers -->
</service>
Expand All @@ -53,5 +54,9 @@
<service id="scheb_two_factor.authentication_context_factory" class="%scheb_two_factor.authentication_context_factory.class%">
<argument>%scheb_two_factor.authentication_context.class%</argument>
</service>
<service id="scheb_two_factor.security.renderer" class="%scheb_two_factor.security.renderer.class%" lazy="true">
<argument type="service" id="templating" />
<argument>%scheb_two_factor.google.template%</argument>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion Resources/config/security_email.xml
Expand Up @@ -28,7 +28,7 @@
<tag name="scheb_two_factor.provider" alias="email" />
<argument type="service" id="scheb_two_factor.security.email.code_generator" />
<argument type="service" id="scheb_two_factor.security.email.code_validator" />
<argument type="service" id="templating" />
<argument type="service" id="scheb_two_factor.security.renderer" />
<argument>%scheb_two_factor.email.template%</argument>
<argument>%scheb_two_factor.parameter_names.auth_code%</argument>
</service>
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/security_google.xml
Expand Up @@ -25,7 +25,7 @@
<service id="scheb_two_factor.security.google.provider" class="%scheb_two_factor.security.google.provider.class%">
<tag name="scheb_two_factor.provider" alias="google" />
<argument type="service" id="scheb_two_factor.security.google.code_validator" />
<argument type="service" id="templating" />
<argument type="service" id="scheb_two_factor.security.renderer" />
<argument>%scheb_two_factor.google.template%</argument>
<argument>%scheb_two_factor.parameter_names.auth_code%</argument>
</service>
Expand Down
29 changes: 6 additions & 23 deletions Security/TwoFactor/Provider/Email/TwoFactorProvider.php
Expand Up @@ -7,7 +7,7 @@
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Validation\CodeValidatorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Renderer;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -24,35 +24,20 @@ class TwoFactorProvider implements TwoFactorProviderInterface
private $authenticator;

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

/**
* @var string
*/
private $formTemplate;
private $renderer;

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

/**
* Construct provider for email authentication.
*
* @param CodeGeneratorInterface $codeGenerator
* @param CodeValidatorInterface $authenticator
* @param EngineInterface $templating
* @param string $formTemplate
* @param string $authCodeParameter
*/
public function __construct(CodeGeneratorInterface $codeGenerator, CodeValidatorInterface $authenticator, EngineInterface $templating, $formTemplate, $authCodeParameter)
public function __construct(CodeGeneratorInterface $codeGenerator, CodeValidatorInterface $authenticator, Renderer $renderer, $formTemplate, $authCodeParameter)
{
$this->codeGenerator = $codeGenerator;
$this->authenticator = $authenticator;
$this->templating = $templating;
$this->formTemplate = $formTemplate;
$this->renderer = $renderer;
$this->authCodeParameter = $authCodeParameter;
}

Expand Down Expand Up @@ -103,8 +88,6 @@ public function requestAuthenticationCode(AuthenticationContextInterface $contex
}

// Force authentication code dialog
return $this->templating->renderResponse($this->formTemplate, array(
'useTrustedOption' => $context->useTrustedOption(),
));
return $this->renderer->render($context);
}
}
28 changes: 6 additions & 22 deletions Security/TwoFactor/Provider/Google/TwoFactorProvider.php
Expand Up @@ -6,7 +6,7 @@
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\Validation\CodeValidatorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Renderer;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -18,33 +18,19 @@ class TwoFactorProvider implements TwoFactorProviderInterface
private $authenticator;

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

/**
* @var string
*/
private $formTemplate;
private $renderer;

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

/**
* Construct provider for Google authentication.
*
* @param CodeValidatorInterface $authenticator
* @param EngineInterface $templating
* @param string $formTemplate
* @param string $authCodeParameter
*/
public function __construct(CodeValidatorInterface $authenticator, EngineInterface $templating, $formTemplate, $authCodeParameter)
public function __construct(CodeValidatorInterface $authenticator, Renderer $renderer, $authCodeParameter)
{
$this->authenticator = $authenticator;
$this->templating = $templating;
$this->formTemplate = $formTemplate;
$this->renderer = $renderer;
$this->authCodeParameter = $authCodeParameter;
}

Expand Down Expand Up @@ -89,8 +75,6 @@ public function requestAuthenticationCode(AuthenticationContextInterface $contex
}

// Force authentication code dialog
return $this->templating->renderResponse($this->formTemplate, array(
'useTrustedOption' => $context->useTrustedOption(),
));
return $this->renderer->render($context);
}
}
24 changes: 24 additions & 0 deletions Security/TwoFactor/Renderer.php
@@ -0,0 +1,24 @@
<?php

namespace Scheb\TwoFactorBundle\Security\TwoFactor;

use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

class Renderer
{
private $templating;
private $formTemplate;

public function __construct(EngineInterface $templating, $formTemplate)
{
$this->templating = $templating;
$this->formTemplate = $formTemplate;
}

public function render(AuthenticationContextInterface $context) {
return $this->templating->renderResponse($this->formTemplate, [
'useTrustedOption' => $context->useTrustedOption(),
]);
}
}
25 changes: 13 additions & 12 deletions Tests/Security/TwoFactor/Provider/Email/TwoFactorProviderTest.php
Expand Up @@ -21,7 +21,7 @@ class TwoFactorProviderTest extends TestCase
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $templating;
private $renderer;

/**
* @var string
Expand All @@ -39,9 +39,9 @@ public function setUp()

$this->authenticator = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Validation\CodeValidatorInterface');

$this->templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$this->renderer = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\Renderer');

$this->provider = new TwoFactorProvider($this->generator, $this->authenticator, $this->templating, $this->formTemplate, 'authCodeName');
$this->provider = new TwoFactorProvider($this->generator, $this->authenticator, $this->renderer, $this->formTemplate, 'authCodeName');
}

/**
Expand Down Expand Up @@ -218,13 +218,14 @@ public function beginAuthentication_interfaceNotImplemented_returnFalse()
*/
public function requestAuthenticationCode_trustedOption_assignToTemplate($trustedOption)
{
$context = $this->getAuthenticationContext(null, null, null, $trustedOption);

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->with($this->formTemplate, array('useTrustedOption' => $trustedOption));
->method('render')
->with($context);

$context = $this->getAuthenticationContext(null, null, null, $trustedOption);
$this->provider->requestAuthenticationCode($context);
}

Expand Down Expand Up @@ -255,9 +256,9 @@ public function requestAuthenticationCode_notPostRequest_displayForm()
->method('checkCode');

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->method('render')
->will($this->returnValue(new Response('<form></form>')));

$returnValue = $this->provider->requestAuthenticationCode($context);
Expand Down Expand Up @@ -301,10 +302,10 @@ public function requestAuthenticationCode_invalidCode_displayFlashMessage()
->with('two_factor', 'scheb_two_factor.code_invalid');

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->with($this->formTemplate, $this->anything())
->method('render')
->with($context)
->will($this->returnValue(new Response('<form></form>')));

$returnValue = $this->provider->requestAuthenticationCode($context);
Expand Down
30 changes: 13 additions & 17 deletions Tests/Security/TwoFactor/Provider/Google/TwoFactorProviderTest.php
Expand Up @@ -16,12 +16,7 @@ class TwoFactorProviderTest extends TestCase
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $templating;

/**
* @var string
*/
private $formTemplate = 'AcmeTestBundle:Test:auth.html.twig';
private $renderer;

/**
* @var TwoFactorProvider
Expand All @@ -31,8 +26,8 @@ class TwoFactorProviderTest extends TestCase
public function setUp()
{
$this->authenticator = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\Validation\CodeValidatorInterface');
$this->templating = $this->createMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$this->provider = new TwoFactorProvider($this->authenticator, $this->templating, $this->formTemplate, 'authCodeName');
$this->renderer = $this->createMock('Scheb\TwoFactorBundle\Security\TwoFactor\Renderer');
$this->provider = new TwoFactorProvider($this->authenticator, $this->renderer, 'authCodeName');
}

/**
Expand Down Expand Up @@ -192,13 +187,14 @@ public function beginAuthentication_interfaceNotImplemented_returnFalse()
*/
public function requestAuthenticationCode_trustedOption_assignToTemplate($trustedOption)
{
$context = $this->getAuthenticationContext(null, null, null, $trustedOption);

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->with($this->formTemplate, array('useTrustedOption' => $trustedOption));
->method('render')
->with($context);

$context = $this->getAuthenticationContext(null, null, null, $trustedOption);
$this->provider->requestAuthenticationCode($context);
}

Expand Down Expand Up @@ -229,9 +225,9 @@ public function requestAuthenticationCode_notPostRequest_displayForm()
->method('checkCode');

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->method('render')
->will($this->returnValue(new Response('<form></form>')));

$returnValue = $this->provider->requestAuthenticationCode($context);
Expand Down Expand Up @@ -275,10 +271,10 @@ public function requestAuthenticationCode_invalidCode_displayFlashMessage()
->with('two_factor', 'scheb_two_factor.code_invalid');

//Mock the template engine
$this->templating
$this->renderer
->expects($this->once())
->method('renderResponse')
->with($this->formTemplate, $this->anything())
->method('render')
->with($context)
->will($this->returnValue(new Response('<form></form>')));

$returnValue = $this->provider->requestAuthenticationCode($context);
Expand Down

0 comments on commit 41ad4f8

Please sign in to comment.