Skip to content
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

Allow disabling the templating component #1675

Merged
merged 2 commits into from Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Resources/config/view.xml
Expand Up @@ -9,7 +9,7 @@
<service id="fos_rest.view_handler.default" class="FOS\RestBundle\View\ViewHandler" public="false">
<argument type="service" id="fos_rest.router" />
<argument type="service" id="fos_rest.serializer" />
<argument type="service" id="fos_rest.templating" />
<argument type="service" id="fos_rest.templating" on-invalid="null" />
<argument type="service" id="request_stack" />
<argument type="collection" /> <!-- formats -->
<argument /> <!-- failed validation -->
Expand Down
13 changes: 13 additions & 0 deletions Tests/View/ViewHandlerTest.php
Expand Up @@ -562,6 +562,19 @@ public function exceptionWrapperSerializeResponseContentProvider()
];
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface must be in injected in FOS\RestBundle\View\ViewHandler to render templates.
*/
public function testTemplatingNotInjected()
{
$this->templating = null;

$view = (new View())->setTemplate('foo.html.twig');
$viewHandler = $this->createViewHandler();
$viewHandler->renderTemplate($view, 'html');
}

private function createViewHandler($formats = null, $failedValidationCode = Response::HTTP_BAD_REQUEST, $emptyContentCode = Response::HTTP_NO_CONTENT, $serializeNull = false, $forceRedirects = null, $defaultEngine = 'twig')
{
return new ViewHandler(
Expand Down
6 changes: 5 additions & 1 deletion View/ViewHandler.php
Expand Up @@ -119,7 +119,7 @@ class ViewHandler implements ConfigurableViewHandlerInterface
public function __construct(
UrlGeneratorInterface $urlGenerator,
Serializer $serializer,
EngineInterface $templating,
EngineInterface $templating = null,
RequestStack $requestStack,
array $formats = null,
$failedValidationCode = Response::HTTP_BAD_REQUEST,
Expand Down Expand Up @@ -341,6 +341,10 @@ public function createRedirectResponse(View $view, $location, $format)
*/
public function renderTemplate(View $view, $format)
{
if (null === $this->templating) {
throw new \LogicException(sprintf('An instance of %s must be in injected in %s to render templates.', EngineInterface::class, __CLASS__));
}

$data = $this->prepareTemplateParameters($view);

$template = $view->getTemplate();
Expand Down