Skip to content

Commit

Permalink
bug #30593 Fixed usage of TranslatorInterface in form extension (fixes
Browse files Browse the repository at this point in the history
…#30591) (althaus)

This PR was merged into the 4.2 branch.

Discussion
----------

Fixed usage of TranslatorInterface in form extension (fixes #30591)

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30591
| License       | MIT

This PR replaces the fixed usage of the deprecated TranslatorInterface in the form extension with a soft one accepting either the old or the new interface.

Commits
-------

d8092c7 Fixed usage of TranslatorInterface in form extension (fixes #30591)
  • Loading branch information
fabpot committed Mar 19, 2019
2 parents affaa45 + d8092c7 commit f407a2b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/CoreExtension.php
Expand Up @@ -19,7 +19,8 @@
use Symfony\Component\Form\Extension\Core\Type\TransformationFailureExtension;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Represents the main form extension, which loads the core functionality.
Expand All @@ -32,8 +33,14 @@ class CoreExtension extends AbstractExtension
private $choiceListFactory;
private $translator;

public function __construct(PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null, TranslatorInterface $translator = null)
/**
* @param TranslatorInterface|null $translator
*/
public function __construct(PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null, $translator = null)
{
if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf('Argument 3 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->choiceListFactory = $choiceListFactory ?: new CachingFactoryDecorator(new PropertyAccessDecorator(new DefaultChoiceListFactory(), $this->propertyAccessor));
$this->translator = $translator;
Expand Down
Expand Up @@ -15,7 +15,8 @@
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
Expand All @@ -24,8 +25,14 @@ class TransformationFailureListener implements EventSubscriberInterface
{
private $translator;

public function __construct(TranslatorInterface $translator = null)
/**
* @param TranslatorInterface|null $translator
*/
public function __construct($translator = null)
{
if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
$this->translator = $translator;
}

Expand Down
Expand Up @@ -14,7 +14,8 @@
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\EventListener\TransformationFailureListener;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
Expand All @@ -23,8 +24,14 @@ class TransformationFailureExtension extends AbstractTypeExtension
{
private $translator;

public function __construct(TranslatorInterface $translator = null)
/**
* @param TranslatorInterface|null $translator
*/
public function __construct($translator = null)
{
if (null !== $translator && !$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
}
$this->translator = $translator;
}

Expand Down

0 comments on commit f407a2b

Please sign in to comment.