New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Custom backend routes #512
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
69ce151
Enables the usage of custom backend routes.
sheeep 5fea667
CS Fixes. (Newlines)
sheeep 0e86193
Refactored custom backend view implementations.
sheeep c793c88
Renamed twig function to 'surround' content with the Contao base temp…
sheeep f04644e
Tested contao backend template twig extension.
sheeep dcb9fdb
Test if backend template only returns something if _scope is backend.
sheeep 921dedb
Fixed a bug in the service configuration.
sheeep ba7d4a5
Fixed toggleNavigation by setting an explicit url.
sheeep a556133
Use a different toggleNavigationUrl than the href, as it only works i…
sheeep 203d710
Renamed service contao.twig.base_template_wrapper to contao.twig.base…
sheeep 20d70b7
CS Fixes.
sheeep da6b56f
Change stuff as discussed with @sheeep.
leofeyer bd61ca4
Fix some things.
leofeyer 9cf1b23
Fixed namespaces in tests.
sheeep 0b0296a
Tested getFunctions method
sheeep aecd0a1
PHPDoc for testGetFunctionsContainsRenderBaseTemplateFunction
sheeep bdda814
Set minimal twig version to 1.26 since this was the lowest version wh…
sheeep d50ae08
Renamed test function
sheeep File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use the ScopeMatcher service instead