Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
tambait committed Dec 14, 2020
1 parent c05d7f2 commit 9d50602
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 97 deletions.
7 changes: 5 additions & 2 deletions src/Twig/Extension/CanonicalizeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ final class CanonicalizeExtension extends AbstractExtension
public function getFunctions(): array
{
return [
new TwigFunction('canonicalize_locale_for_moment', [$this, 'getCanonicalizedLocaleForMoment'], ['needs_context' => true]),
new TwigFunction('canonicalize_locale_for_select2', [$this, 'getCanonicalizedLocaleForSelect2'], ['needs_context' => true]),

//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]),
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Twig/Extension/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function __construct(
public function getFunctions(): array
{
return [
new TwigFunction('is_granted_affirmative', [$this, 'isGrantedAffirmative']),
//NEXT_MAJOR: Uncomment line below
//new TwigFunction('is_granted_affirmative', [$this, 'isGrantedAffirmative']),
];
}

Expand Down
123 changes: 29 additions & 94 deletions src/Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Security\Acl\Voter\FieldVote;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslationInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
Expand Down Expand Up @@ -102,6 +100,8 @@ class SonataAdminExtension extends AbstractExtension

/**
* NEXT_MAJOR: Make $propertyAccessor mandatory.
*
* @internal
*/
public function __construct(
Pool $pool,
Expand Down Expand Up @@ -574,7 +574,13 @@ public function getXEditableType($type)
), E_USER_DEPRECATED);
}

return $this->xEditableTypeMapping[$type] ?? false;
/* @phpstan-ignore-next-line */
if (null === $this->xEditableExtension) {
$this->xEditableExtension = new XEditableExtension($this->translator);
}

/* @phpstan-ignore-next-line */
return $this->xEditableExtension->xEditableType($type);
}

/**
Expand All @@ -599,44 +605,13 @@ public function getXEditableChoices(FieldDescriptionInterface $fieldDescription)
), E_USER_DEPRECATED);
}

$choices = $fieldDescription->getOption('choices', []);
$catalogue = $fieldDescription->getOption('catalogue');
$xEditableChoices = [];
if (!empty($choices)) {
reset($choices);
$first = current($choices);
// the choices are already in the right format
if (\is_array($first) && \array_key_exists('value', $first) && \array_key_exists('text', $first)) {
$xEditableChoices = $choices;
} else {
foreach ($choices as $value => $text) {
if ($catalogue) {
if (null !== $this->translator) {
$text = $this->translator->trans($text, [], $catalogue);
// NEXT_MAJOR: Remove this check
} elseif (method_exists($fieldDescription->getAdmin(), 'trans')) {
$text = $fieldDescription->getAdmin()->trans($text, [], $catalogue);
}
}

$xEditableChoices[] = [
'value' => $value,
'text' => $text,
];
}
}
}

if (false === $fieldDescription->getOption('required', true)
&& false === $fieldDescription->getOption('multiple', false)
) {
$xEditableChoices = array_merge([[
'value' => '',
'text' => '',
]], $xEditableChoices);
/* @phpstan-ignore-next-line */
if (null === $this->xEditableExtension) {
$this->xEditableExtension = new XEditableExtension($this->translator);
}

return $xEditableChoices;
/* @phpstan-ignore-next-line */
return $this->xEditableExtension->getXEditableChoices($fieldDescription);
}

/**
Expand All @@ -658,20 +633,13 @@ final public function getCanonicalizedLocaleForMoment(array $context)
), E_USER_DEPRECATED);
}

$locale = strtolower(str_replace('_', '-', $context['app']->getRequest()->getLocale()));

// "en" language doesn't require localization.
if (('en' === $lang = substr($locale, 0, 2)) && !\in_array($locale, ['en-au', 'en-ca', 'en-gb', 'en-ie', 'en-nz'], true)) {
return null;
}

foreach (self::MOMENT_UNSUPPORTED_LOCALES as $language => $locales) {
if ($language === $lang && !\in_array($locale, $locales, true)) {
$locale = $language;
}
/* @phpstan-ignore-next-line */
if (null === $this->canonicalizeExtension) {
$this->canonicalizeExtension = new CanonicalizeExtension();
}

return $locale;
/* @phpstan-ignore-next-line */
return $this->canonicalizeExtension->getCanonicalizedLocaleForMoment($context);
}

/**
Expand All @@ -693,30 +661,13 @@ final public function getCanonicalizedLocaleForSelect2(array $context)
), E_USER_DEPRECATED);
}

$locale = str_replace('_', '-', $context['app']->getRequest()->getLocale());

// "en" language doesn't require localization.
if ('en' === $lang = substr($locale, 0, 2)) {
return null;
}

switch ($locale) {
case 'pt':
$locale = 'pt-PT';
break;
case 'ug':
$locale = 'ug-CN';
break;
case 'zh':
$locale = 'zh-CN';
break;
default:
if (!\in_array($locale, ['pt-BR', 'pt-PT', 'ug-CN', 'zh-CN', 'zh-TW'], true)) {
$locale = $lang;
}
/* @phpstan-ignore-next-line */
if (null === $this->canonicalizeExtension) {
$this->canonicalizeExtension = new CanonicalizeExtension();
}

return $locale;
/* @phpstan-ignore-next-line */
return $this->canonicalizeExtension->getCanonicalizedLocaleForSelect2($context);
}

/**
Expand All @@ -739,29 +690,13 @@ public function isGrantedAffirmative($role, $object = null, $field = null)
), E_USER_DEPRECATED);
}

if (null === $this->securityChecker) {
return false;
}

if (null !== $field) {
$object = new FieldVote($object, $field);
}

if (!\is_array($role)) {
$role = [$role];
}

foreach ($role as $oneRole) {
try {
if ($this->securityChecker->isGranted($oneRole, $object)) {
return true;
}
} catch (AuthenticationCredentialsNotFoundException $e) {
// empty on purpose
}
/* @phpstan-ignore-next-line */
if (null === $this->securityExtension) {
$this->securityExtension = new SecurityExtension($this->securityChecker);
}

return false;
/* @phpstan-ignore-next-line */
return $this->securityExtension->isGrantedAffirmative($role, $object, $field);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Twig/Extension/XEditableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function __construct(
public function getFilters(): array
{
return [
//NEXT_MAJOR: Uncomment lines below

/*
new TwigFilter(
'sonata_xeditable_type',
[$this, 'getXEditableType']
Expand All @@ -50,6 +53,7 @@ public function getFilters(): array
'sonata_xeditable_choices',
[$this, 'getXEditableChoices']
),
*/
];
}

Expand Down

0 comments on commit 9d50602

Please sign in to comment.