Skip to content

Commit

Permalink
RenderElement test & small fixes in extension classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tambait authored and franmomu committed Jan 6, 2021
1 parent 1177e72 commit 1ef518a
Show file tree
Hide file tree
Showing 9 changed files with 2,596 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/Resources/config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
// NEXT_MAJOR: Remove next line.
new ReferenceConfigurator('security.authorization_checker'),
])
// NEXT_MAJOR: Remove next call.
->call('setXEditableTypeMapping', [
'%sonata.admin.twig.extension.x_editable_type_mapping%',
'sonata_deprecation_mute',
Expand Down Expand Up @@ -103,7 +104,7 @@
'%sonata.admin.twig.extension.x_editable_type_mapping%',
])

->set('sonata.renderElement.twig.extension', RenderElementExtension::class)
->set('sonata.render_element.twig.extension', RenderElementExtension::class)
->tag('twig.extension')
->args([
new ReferenceConfigurator('property_accessor'),
Expand Down
7 changes: 5 additions & 2 deletions src/Twig/Extension/CanonicalizeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ final class CanonicalizeExtension extends AbstractExtension
*/
private $requestStack;

/**
* @internal This class should only be used through Twig
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
Expand All @@ -44,8 +47,8 @@ public function getFunctions(): array
{
return [
//NEXT_MAJOR: Uncomment lines below
//new TwigFunction('canonicalize_locale_for_moment', [$this, 'getCanonicalizedLocaleForMoment'], ['needs_context' => true]),
//new TwigFunction('canonicalize_locale_for_select2', [$this, 'getCanonicalizedLocaleForSelect2'], ['needs_context' => true]),
//new TwigFunction('canonicalize_locale_for_moment', [$this, 'getCanonicalizedLocaleForMoment']),
//new TwigFunction('canonicalize_locale_for_select2', [$this, 'getCanonicalizedLocaleForSelect2']),
];
}

Expand Down
10 changes: 5 additions & 5 deletions src/Twig/Extension/RenderElementExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ final class RenderElementExtension extends AbstractExtension
*/
private $propertyAccessor;

/**
* @internal This class should only be used through Twig
*/
public function __construct(
?PropertyAccessorInterface $propertyAccessor = null,
PropertyAccessorInterface $propertyAccessor,
?ContainerInterface $templateRegistries = null,
?LoggerInterface $logger = null
) {
Expand Down Expand Up @@ -385,10 +388,7 @@ public function getObjectAndValueFromListElement(
return [$object, $value];
}

/**
* NEXT MAJOR: Make this method private.
*/
public function render(
private function render(
FieldDescriptionInterface $fieldDescription,
TemplateWrapper $template,
array $parameters,
Expand Down
3 changes: 3 additions & 0 deletions src/Twig/Extension/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ final class SecurityExtension extends AbstractExtension
*/
private $securityChecker;

/**
* @internal This class should only be used through Twig
*/
public function __construct(
?AuthorizationCheckerInterface $securityChecker = null
) {
Expand Down
59 changes: 30 additions & 29 deletions src/Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,36 @@ public function output(
array $parameters,
Environment $environment
) {
return $this->render(
$fieldDescription,
new TemplateWrapper($environment, $template),
$parameters,
$environment
);
@trigger_error(sprintf(
'The %s method is deprecated since version 3.33 and will be removed in 4.0.',
__METHOD__
), E_USER_DEPRECATED);

$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;
}

/**
Expand Down Expand Up @@ -731,29 +755,6 @@ protected function getTemplate(
return $this->renderElementExtension->getTemplate($fieldDescription, $defaultTemplate, $$environment);
}

/**
* NEXT_MAJOR: Remove this method.
*/
private function render(
FieldDescriptionInterface $fieldDescription,
TemplateWrapper $template,
array $parameters,
Environment $environment
): string {
if ('sonata_deprecation_mute' !== (\func_get_args()[4] ?? null)) {
@trigger_error(sprintf(
'The %s method is deprecated in favor of RenderElementExtension::render since version 3.x and will be removed in 4.0.',
__METHOD__
), E_USER_DEPRECATED);
}

if (null === $this->renderElementExtension) {
$this->renderElementExtension = new RenderElementExtension($this->propertyAccessor, $this->templateRegistries, $this->logger);
}

return $this->renderElementExtension->render($fieldDescription, $template, $parameters, $environment);
}

/**
* NEXT_MAJOR: Remove this method
* Extracts the object and requested value from the $listElement.
Expand Down
10 changes: 10 additions & 0 deletions src/Twig/Extension/XEditableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ final class XEditableExtension extends AbstractExtension

/**
* @param string[] $xEditableTypeMapping
*
* @internal This class should only be used through Twig
*/
public function __construct(
TranslatorInterface $translator,
Expand Down Expand Up @@ -69,6 +71,14 @@ public function getXEditableType(string $type)
return $this->xEditableTypeMapping[$type] ?? false;
}

/**
* @param string[] $xEditableTypeMapping
*/
public function setXEditableTypeMapping($xEditableTypeMapping)
{
$this->xEditableTypeMapping = $xEditableTypeMapping;
}

/**
* Return xEditable choices based on the field description choices options & catalogue options.
* With the following choice options:
Expand Down
4 changes: 2 additions & 2 deletions tests/Controller/HelperControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function testSetObjectFieldValueAction(): void
$container->set('sonata.post.admin.template_registry', $templateRegistry);
$this->pool->method('getPropertyAccessor')->willReturn($propertyAccessor);
$this->twig->method('getExtension')->with(SonataAdminExtension::class)->willReturn(
new SonataAdminExtension($pool, null, $translator, $container)
new SonataAdminExtension($pool, null, $translator, $container, $propertyAccessor)
);
$this->twig->method('load')->with('admin_template')->willReturn(new TemplateWrapper($this->twig, $template));
$this->twig->method('isDebug')->willReturn(false);
Expand Down Expand Up @@ -280,7 +280,7 @@ public function testSetObjectFieldValueActionOnARelationField(): void
$this->admin->method('getModelManager')->willReturn($modelManager);
$this->validator->method('validate')->with($object)->willReturn(new ConstraintViolationList([]));
$this->twig->method('getExtension')->with(SonataAdminExtension::class)->willReturn(
new SonataAdminExtension($this->pool, null, $translator, $container)
new SonataAdminExtension($this->pool, null, $translator, $container, $propertyAccessor)
);
$this->twig->method('load')->with('field_template')->willReturn(new TemplateWrapper($this->twig, $template));
$this->twig->method('isDebug')->willReturn(false);
Expand Down
Loading

0 comments on commit 1ef518a

Please sign in to comment.