Skip to content

Commit

Permalink
Replaced calls to internal Twig Environment loadTemplate method with …
Browse files Browse the repository at this point in the history
…load
  • Loading branch information
TimoBakx authored and jordisala1991 committed Mar 9, 2018
1 parent 52df713 commit ecae9ab
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 33 deletions.
5 changes: 5 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
UPGRADE 3.x
===========

## Deprecated SonataAdminExtension::output()

The `SonataAdminExtension::output()` method is deprecated and should not be
used anymore.

UPGRADE FROM 3.30 to 3.31
=========================

Expand Down
82 changes: 51 additions & 31 deletions src/Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Twig\Error\LoaderError;
use Twig\Extension\AbstractExtension;
use Twig\Template;
use Twig\TemplateWrapper;
use Twig\TwigFilter;
use Twig\TwigFunction;

Expand Down Expand Up @@ -143,7 +144,7 @@ public function renderListElement(
$environment
);

return $this->output($fieldDescription, $template, array_merge($params, [
return $this->render($fieldDescription, $template, array_merge($params, [
'admin' => $fieldDescription->getAdmin(),
'object' => $object,
'value' => $this->getValueFromFieldDescription($object, $fieldDescription),
Expand All @@ -152,6 +153,8 @@ public function renderListElement(
}

/**
* @deprecated since 3.x, to be removed in 4.0. Use render instead
*
* @return string
*/
public function output(
Expand All @@ -160,31 +163,12 @@ public function output(
array $parameters,
Environment $environment
) {
$content = $template->render($parameters);

if ($environment->isDebug()) {
$commentTemplate = <<<'EOT'
<!-- START
fieldName: %s
template: %s
compiled template: %s
-->
%s
<!-- END - fieldName: %s -->
EOT;

return sprintf(
$commentTemplate,
$fieldDescription->getFieldName(),
$fieldDescription->getTemplate(),
$template->getTemplateName(),
$content,
$fieldDescription->getFieldName()
);
}

return $content;
return $this->render(
$fieldDescription,
new TemplateWrapper($environment, $template),
$parameters,
$environment
);
}

/**
Expand Down Expand Up @@ -243,7 +227,7 @@ public function renderViewElement(
$value = null;
}

return $this->output($fieldDescription, $template, [
return $this->render($fieldDescription, $template, [
'field_description' => $fieldDescription,
'object' => $object,
'value' => $value,
Expand Down Expand Up @@ -298,7 +282,7 @@ public function renderViewElementCompare(
// Compare the rendered output of both objects by using the (possibly) overridden field block
$isDiff = $baseValueOutput !== $compareValueOutput;

return $this->output($fieldDescription, $template, [
return $this->render($fieldDescription, $template, [
'field_description' => $fieldDescription,
'value' => $baseValue,
'value_compare' => $compareValue,
Expand Down Expand Up @@ -497,7 +481,7 @@ final public function getCanonicalizedLocaleForSelect2(array $context)
*
* @param string $defaultTemplate
*
* @return \Twig_TemplateInterface
* @return TemplateWrapper
*/
protected function getTemplate(
FieldDescriptionInterface $fieldDescription,
Expand All @@ -507,15 +491,15 @@ protected function getTemplate(
$templateName = $fieldDescription->getTemplate() ?: $defaultTemplate;

try {
$template = $environment->loadTemplate($templateName);
$template = $environment->load($templateName);
} catch (LoaderError $e) {
@trigger_error(
'Relying on default template loading on field template loading exception '.
'is deprecated since 3.1 and will be removed in 4.0. '.
'A \Twig_Error_Loader exception will be thrown instead',
E_USER_DEPRECATED
);
$template = $environment->loadTemplate($defaultTemplate);
$template = $environment->load($defaultTemplate);

if (null !== $this->logger) {
$this->logger->warning(sprintf(
Expand All @@ -530,4 +514,40 @@ protected function getTemplate(

return $template;
}

/**
* @return string
*/
private function render(
FieldDescriptionInterface $fieldDescription,
TemplateWrapper $template,
array $parameters,
Environment $environment
) {
$content = $template->render($parameters);

if ($environment->isDebug()) {
$commentTemplate = <<<'EOT'
<!-- START
fieldName: %s
template: %s
compiled template: %s
-->
%s
<!-- END - fieldName: %s -->
EOT;

return sprintf(
$commentTemplate,
$fieldDescription->getFieldName(),
$fieldDescription->getTemplate(),
$template->getSourceContext()->getName(),
$content,
$fieldDescription->getFieldName()
);
}

return $content;
}
}
35 changes: 33 additions & 2 deletions tests/Controller/HelperControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Twig\Environment;
use Twig\Template;
use Twig\TemplateWrapper;

class AdminControllerHelper_Foo
{
Expand Down Expand Up @@ -91,6 +92,36 @@ public function getEnabled()

class HelperControllerTest extends TestCase
{
/**
* @var Pool
*/
private $pool;

/**
* @var Environment
*/
private $twig;

/**
* @var AdminHelper
*/
private $helper;

/**
* @var ValidatorInterface
*/
private $validator;

/**
* @var AbstractAdmin
*/
private $admin;

/**
* @var HelperController
*/
private $controller;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -215,7 +246,7 @@ public function testSetObjectFieldValueAction()
$this->twig->getExtension(SonataAdminExtension::class)->willReturn(
new SonataAdminExtension($pool->reveal(), null, $translator->reveal())
);
$this->twig->loadTemplate('admin_template')->willReturn($template->reveal());
$this->twig->load('admin_template')->willReturn(new TemplateWrapper($this->twig->reveal(), $template->reveal()));
$this->twig->isDebug()->willReturn(false);
$fieldDescription->getOption('editable')->willReturn(true);
$fieldDescription->getAdmin()->willReturn($this->admin->reveal());
Expand Down Expand Up @@ -262,7 +293,7 @@ public function testSetObjectFieldValueActionOnARelationField()
$this->twig->getExtension(SonataAdminExtension::class)->willReturn(
new SonataAdminExtension($pool->reveal(), null, $translator->reveal())
);
$this->twig->loadTemplate('field_template')->willReturn($template->reveal());
$this->twig->load('field_template')->willReturn(new TemplateWrapper($this->twig->reveal(), $template->reveal()));
$this->twig->isDebug()->willReturn(false);
$this->pool->getContainer()->willReturn($container->reveal());
$this->pool->getPropertyAccessor()->willReturn($propertyAccessor);
Expand Down
43 changes: 43 additions & 0 deletions tests/Twig/Extension/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,9 @@ public function testGetValueFromFieldDescriptionWithNoValueExceptionNewAdminInst
$this->assertSame('foo', $this->twigExtension->getValueFromFieldDescription($object, $fieldDescription));
}

/**
* @group legacy
*/
public function testOutput()
{
$this->fieldDescription->expects($this->any())
Expand Down Expand Up @@ -2079,6 +2082,46 @@ public function testOutput()
);
}

public function testRenderWithDebug()
{
$this->fieldDescription->expects($this->any())
->method('getTemplate')
->will($this->returnValue('@SonataAdmin/CRUD/base_list_field.html.twig'));

$this->fieldDescription->expects($this->any())
->method('getFieldName')
->will($this->returnValue('fd_name'));

$this->fieldDescription->expects($this->any())
->method('getValue')
->will($this->returnValue('foo'));

$parameters = [
'admin' => $this->admin,
'value' => 'foo',
'field_description' => $this->fieldDescription,
'object' => $this->object,
];

$this->environment->enableDebug();

$this->assertSame(
$this->removeExtraWhitespace(<<<'EOT'
<!-- START
fieldName: fd_name
template: @SonataAdmin/CRUD/base_list_field.html.twig
compiled template: @SonataAdmin/CRUD/base_list_field.html.twig
-->
<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>
<!-- END - fieldName: fd_name -->
EOT
),
$this->removeExtraWhitespace(
$this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription, $parameters)
)
);
}

public function testRenderRelationElementNoObject()
{
$this->assertSame('foo', $this->twigExtension->renderRelationElement('foo', $this->fieldDescription));
Expand Down

0 comments on commit ecae9ab

Please sign in to comment.