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

Add support for symfony contracts translator interface #6342

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ parameters:
-
# will be fixed in v4. Code is marked as deprecated
message: "#^Result of \\&\\& is always false\\.$#"
count: 1
count: 2
path: ../src/Admin/AbstractAdmin.php

-
Expand Down Expand Up @@ -271,3 +271,8 @@ parameters:
message: "#^Method Symfony\\\\Contracts\\\\EventDispatcher\\\\EventDispatcherInterface\\:\\:dispatch\\(\\) invoked with 2 parameters, 1 required\\.$#"
count: 1
path: ../src/Menu/MenuBuilder.php

-
message: '#Else branch is unreachable because ternary operator condition is always true#'
count: 1
path: ../src/Admin/AbstractAdmin.php
27 changes: 23 additions & 4 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@
use Symfony\Component\Routing\Generator\UrlGeneratorInterface as RoutingUrlGeneratorInterface;
use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Component\Validator\Mapping\GenericMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt this available on sf4.4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is

Copy link
Member

@jordisala1991 jordisala1991 Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in master the compat code can be droped safely right? if thats the case we should add next major cments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed. On master it can be typehinted with Symfony\Contracts\Translation\TranslatorInterface and we can drop all usages of the old legacy interface

Copy link
Contributor Author

@dmaicher dmaicher Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/**
* @author Thomas Rabaix <[email protected]>
Expand Down Expand Up @@ -309,7 +310,7 @@ abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface, A
*
* NEXT_MAJOR: remove this property
*
* @var \Symfony\Component\Translation\TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*
* @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0
*/
Expand Down Expand Up @@ -2443,7 +2444,11 @@ public function transChoice($id, $count, array $parameters = [], $domain = null,

$domain = $domain ?: $this->getTranslationDomain();

return $this->translator->transChoice($id, $count, $parameters, $domain, $locale);
if ($this->translator instanceof LegacyTranslatorInterface) {
return $this->translator->transChoice($id, $count, $parameters, $domain, $locale);
}

return $this->translator->trans($id, ['%count%' => $count] + $parameters, $domain, $locale);
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
}

public function setTranslationDomain($translationDomain)
Expand All @@ -2461,9 +2466,11 @@ public function getTranslationDomain()
*
* NEXT_MAJOR: remove this method
*
* @param LegacyTranslatorInterface|TranslatorInterface $translator
*
* @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0
*/
public function setTranslator(TranslatorInterface $translator)
public function setTranslator($translator)
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
$args = \func_get_args();
if (isset($args[1]) && $args[1]) {
Expand All @@ -2473,6 +2480,16 @@ public function setTranslator(TranslatorInterface $translator)
), E_USER_DEPRECATED);
}

if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand All @@ -2482,6 +2499,8 @@ public function setTranslator(TranslatorInterface $translator)
* NEXT_MAJOR: remove this method
*
* @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0
*
* @return LegacyTranslatorInterface|TranslatorInterface
*/
public function getTranslator()
{
Expand Down
8 changes: 4 additions & 4 deletions src/Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @author Thomas Rabaix <[email protected]>
Expand Down Expand Up @@ -64,6 +65,7 @@
* @method void reorderFormGroup(string $group, array $keys)
* @method void defineFormBuilder(FormBuilderInterface $formBuilder)
* @method string getPagerType()
* @method void setTranslator($translator);
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
*/
interface AdminInterface extends AccessRegistryInterface, FieldDescriptionRegistryInterface, LifecycleHookProviderInterface, MenuBuilderInterface, ParentAdminInterface, UrlGeneratorInterface
{
Expand All @@ -90,10 +92,8 @@ public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder);
*/
public function getDatagridBuilder();

public function setTranslator(TranslatorInterface $translator);

/**
* @return TranslatorInterface
* @return TranslatorInterface|LegacyTranslatorInterface
*/
public function getTranslator();

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as FormChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand Down Expand Up @@ -47,12 +48,22 @@ class ChoiceType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/DateRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand All @@ -42,12 +43,22 @@ class DateRangeType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/DateTimeRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand All @@ -42,12 +43,22 @@ class DateTimeRangeType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\Extension\Core\Type\DateTimeType as FormDateTimeType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand Down Expand Up @@ -67,12 +68,22 @@ class DateTimeType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\Extension\Core\Type\DateType as FormDateType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand Down Expand Up @@ -67,12 +68,22 @@ class DateType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
17 changes: 14 additions & 3 deletions src/Form/Type/Filter/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use Symfony\Component\Form\Extension\Core\Type\NumberType as FormNumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @final since sonata-project/admin-bundle 3.52
Expand Down Expand Up @@ -57,12 +58,22 @@ class NumberType extends AbstractType
*
* @deprecated since sonata-project/admin-bundle 3.5, to be removed with 4.0
*
* @var TranslatorInterface
* @var TranslatorInterface|LegacyTranslatorInterface
*/
protected $translator;

public function __construct(TranslatorInterface $translator)
public function __construct($translator)
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 1 passed to "%s()" must be an instance of "%s" or "%s", %s given.',
__METHOD__,
LegacyTranslatorInterface::class,
TranslatorInterface::class,
\is_object($translator) ? 'instance of '.\get_class($translator) : \gettype($translator)
));
}

$this->translator = $translator;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
Expand Down Expand Up @@ -104,11 +104,11 @@ public function __construct(
), E_USER_DEPRECATED);
}

if (!$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslationInterface) {
if (!$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface) {
throw new \TypeError(sprintf(
'Argument 2 must be an instance of "%s" or preferably "%s", "%s given"',
TranslatorInterface::class,
LegacyTranslationInterface::class,
LegacyTranslatorInterface::class,
\get_class($translator)
));
}
Expand Down
Loading