Skip to content

Commit

Permalink
Use the scope matcher instead of checking the request attribute (see #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Schmid authored and leofeyer committed Jan 24, 2017
1 parent 33be460 commit 17ad628
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,6 @@ services:
arguments:
- "@request_stack"
- "@contao.framework"
- "@contao.routing.scope_matcher"
tags:
- { name: twig.extension }
22 changes: 14 additions & 8 deletions src/Twig/Extension/ContaoTemplateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Contao\BackendCustom;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Symfony\Component\HttpFoundation\RequestStack;

/**
Expand All @@ -29,18 +30,25 @@ class ContaoTemplateExtension extends \Twig_Extension
/**
* @var ContaoFrameworkInterface
*/
private $contaoFramework;
private $framework;

/**
* @var ScopeMatcher
*/
private $scopeMatcher;

/**
* Constructor.
*
* @param RequestStack $requestStack
* @param ContaoFrameworkInterface $contaoFramework
* @param ContaoFrameworkInterface $framework
* @param ScopeMatcher $scopeMatcher
*/
public function __construct(RequestStack $requestStack, ContaoFrameworkInterface $contaoFramework)
public function __construct(RequestStack $requestStack, ContaoFrameworkInterface $framework, ScopeMatcher $scopeMatcher)
{
$this->requestStack = $requestStack;
$this->contaoFramework = $contaoFramework;
$this->framework = $framework;
$this->scopeMatcher = $scopeMatcher;
}

/**
Expand All @@ -62,14 +70,12 @@ public function getFunctions()
*/
public function renderContaoBackendTemplate(array $blocks = [])
{
$scope = $this->requestStack->getCurrentRequest()->attributes->get('_scope');

if ('backend' !== $scope) {
if (!$this->scopeMatcher->isBackendRequest($this->requestStack->getCurrentRequest())) {
return '';
}

/** @var BackendCustom $controller */
$controller = $this->contaoFramework->createInstance(BackendCustom::class);
$controller = $this->framework->createInstance(BackendCustom::class);
$template = $controller->getTemplateObject();

foreach ($blocks as $key => $content) {
Expand Down
10 changes: 7 additions & 3 deletions tests/Twig/Extension/ContaoTemplateExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function testRenderContaoBackendTemplate()
BackendCustom::class => $backendRoute
]);

$extension = new ContaoTemplateExtension($requestStack, $contaoFramework);
$scopeMatcher = $this->mockScopeMatcher();
$extension = new ContaoTemplateExtension($requestStack, $contaoFramework, $scopeMatcher);

$extension->renderContaoBackendTemplate([
'a' => 'a',
Expand All @@ -85,8 +86,9 @@ public function testGetFunctions()
$requestStack->push($request);

$contaoFramework = $this->mockContaoFramework(null, null, [], []);
$scopeMatcher = $this->mockScopeMatcher();

$extension = new ContaoTemplateExtension($requestStack, $contaoFramework);
$extension = new ContaoTemplateExtension($requestStack, $contaoFramework, $scopeMatcher);
$functions = $extension->getFunctions();

$renderBaseTemplateFunction = array_filter($functions, function(\Twig_SimpleFunction $function) {
Expand All @@ -107,8 +109,10 @@ public function testScopeRestriction()
$requestStack = new RequestStack();
$requestStack->push($request);

$scopeMatcher = $this->mockScopeMatcher();

$contaoFramework = $this->mockContaoFramework(null, null, [], []);
$extension = new ContaoTemplateExtension($requestStack, $contaoFramework);
$extension = new ContaoTemplateExtension($requestStack, $contaoFramework, $scopeMatcher);

$this->assertEmpty($extension->renderContaoBackendTemplate());
}
Expand Down

0 comments on commit 17ad628

Please sign in to comment.