-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support custom backend routes (see #512).
- Loading branch information
Showing
11 changed files
with
295 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/** | ||
* Contao Open Source CMS | ||
* | ||
* Copyright (c) 2005-2016 Leo Feyer | ||
* | ||
* @license LGPL-3.0+ | ||
*/ | ||
|
||
namespace Contao; | ||
|
||
/** | ||
* Back end custom controller. | ||
* | ||
* @author Jim Schmid <https://github.com/sheeep> | ||
*/ | ||
class BackendCustom extends BackendMain | ||
{ | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
// Initialize the template in the constructor so it is available in the getTemplateObject() method | ||
$this->Template = new \BackendTemplate('be_main'); | ||
} | ||
|
||
|
||
/** | ||
* Return the template object | ||
* | ||
* @return BackendTemplate|object | ||
*/ | ||
public function getTemplateObject() | ||
{ | ||
return $this->Template; | ||
} | ||
|
||
|
||
/** | ||
* Run the controller and parse the template | ||
* | ||
* @return Response | ||
*/ | ||
public function run() | ||
{ | ||
$packages = $this->getContainer()->getParameter('kernel.packages'); | ||
|
||
$this->Template->version = $packages['contao/core-bundle']; | ||
|
||
// Ajax request | ||
if ($_POST && \Environment::get('isAjaxRequest')) | ||
{ | ||
$this->objAjax = new \Ajax(\Input::post('action')); | ||
$this->objAjax->executePreActions(); | ||
} | ||
|
||
return $this->output(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{ render_contao_backend_template({ | ||
main: block('main'), | ||
error: block('headline'), | ||
headline: block('error') | ||
}) | raw }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/** | ||
* Contao Open Source CMS | ||
* | ||
* Copyright (c) 2005-2016 Leo Feyer | ||
* | ||
* @license LGPL-3.0+ | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Twig\Extension; | ||
|
||
use Contao\BackendCustom; | ||
use Contao\CoreBundle\Framework\ContaoFrameworkInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* Contao template extension. | ||
* | ||
* @author Jim Schmid <https://github.com/sheeep> | ||
*/ | ||
class ContaoTemplateExtension extends \Twig_Extension | ||
{ | ||
/** | ||
* @var RequestStack | ||
*/ | ||
private $requestStack; | ||
|
||
/** | ||
* @var ContaoFrameworkInterface | ||
*/ | ||
private $contaoFramework; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param RequestStack $requestStack | ||
* @param ContaoFrameworkInterface $contaoFramework | ||
*/ | ||
public function __construct(RequestStack $requestStack, ContaoFrameworkInterface $contaoFramework) | ||
{ | ||
$this->requestStack = $requestStack; | ||
$this->contaoFramework = $contaoFramework; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFunctions() | ||
{ | ||
return [ | ||
new \Twig_SimpleFunction('render_contao_backend_template', [$this, 'renderContaoBackendTemplate']) | ||
]; | ||
} | ||
|
||
/** | ||
* Renders a Contao back end template with the given blocks. | ||
* | ||
* @param array $blocks | ||
* | ||
* @return string | ||
*/ | ||
public function renderContaoBackendTemplate(array $blocks = []) | ||
{ | ||
$scope = $this->requestStack->getCurrentRequest()->attributes->get('_scope'); | ||
|
||
if ('backend' !== $scope) { | ||
return ''; | ||
} | ||
|
||
/** @var BackendCustom $controller */ | ||
$controller = $this->contaoFramework->createInstance(BackendCustom::class); | ||
$template = $controller->getTemplateObject(); | ||
|
||
foreach ($blocks as $key => $content) { | ||
$template->{$key} = $content; | ||
} | ||
|
||
$response = $controller->run(); | ||
|
||
return $response->getContent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/** | ||
* Contao Open Source CMS | ||
* | ||
* Copyright (c) 2005-2016 Leo Feyer | ||
* | ||
* @license LGPL-3.0+ | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Test\Twig; | ||
|
||
use Contao\BackendCustom; | ||
use Contao\CoreBundle\Test\TestCase; | ||
use Contao\CoreBundle\Twig\Extension\ContaoTemplateExtension; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Tests the ContaoTemplateExtension class. | ||
* | ||
* @author Jim Schmid <https://github.com/sheeep> | ||
*/ | ||
class ContaoTemplateExtensionTest extends TestCase | ||
{ | ||
/** | ||
* Tests the renderContaoBackendTemplate() method. | ||
*/ | ||
public function testRenderContaoBackendTemplate() | ||
{ | ||
$backendRoute = $this | ||
->getMockBuilder(BackendCustom::class) | ||
->disableOriginalConstructor() | ||
->setMethods(['getTemplateObject', 'run']) | ||
->getMock() | ||
; | ||
|
||
$template = new \stdClass(); | ||
|
||
$backendRoute | ||
->expects($this->once()) | ||
->method('getTemplateObject') | ||
->willReturn($template) | ||
; | ||
|
||
$backendRoute | ||
->expects($this->once()) | ||
->method('run') | ||
->willReturn(new Response()) | ||
; | ||
|
||
$request = new Request(); | ||
$request->attributes->set('_scope', 'backend'); | ||
|
||
$requestStack = new RequestStack(); | ||
$requestStack->push($request); | ||
|
||
$contaoFramework = $this->mockContaoFramework(null, null, [], [ | ||
BackendCustom::class => $backendRoute | ||
]); | ||
|
||
$extension = new ContaoTemplateExtension($requestStack, $contaoFramework); | ||
|
||
$extension->renderContaoBackendTemplate([ | ||
'a' => 'a', | ||
'b' => 'b', | ||
'c' => 'c' | ||
]); | ||
|
||
$this->assertSame('a', $template->a); | ||
$this->assertSame('b', $template->b); | ||
$this->assertSame('c', $template->c); | ||
} | ||
|
||
/** | ||
* Tests the getFunctions() method. | ||
*/ | ||
public function testGetFunctions() | ||
{ | ||
$request = new Request(); | ||
$request->attributes->set('_scope', 'backend'); | ||
|
||
$requestStack = new RequestStack(); | ||
$requestStack->push($request); | ||
|
||
$contaoFramework = $this->mockContaoFramework(null, null, [], []); | ||
|
||
$extension = new ContaoTemplateExtension($requestStack, $contaoFramework); | ||
$functions = $extension->getFunctions(); | ||
|
||
$renderBaseTemplateFunction = array_filter($functions, function(\Twig_SimpleFunction $function) { | ||
return $function->getName() === 'render_contao_backend_template'; | ||
}); | ||
|
||
$this->assertCount(1, $renderBaseTemplateFunction); | ||
} | ||
|
||
/** | ||
* Tests the scope restriction. | ||
*/ | ||
public function testScopeRestriction() | ||
{ | ||
$request = new Request(); | ||
$request->attributes->set('_scope', 'frontend'); | ||
|
||
$requestStack = new RequestStack(); | ||
$requestStack->push($request); | ||
|
||
$contaoFramework = $this->mockContaoFramework(null, null, [], []); | ||
$extension = new ContaoTemplateExtension($requestStack, $contaoFramework); | ||
|
||
$this->assertEmpty($extension->renderContaoBackendTemplate()); | ||
} | ||
} |