Skip to content

Commit

Permalink
Avoid reliance on kernel version
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégoire Paris authored and greg0ire committed Feb 8, 2017
1 parent 8ea0b3a commit 435c1b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
Expand Up @@ -33,9 +33,9 @@ public function process(ContainerBuilder $container)
throw new \RuntimeException(sprintf('Please define an alias attribute for tag "form.type" of service "%s".', $id));
}

$paymentMethodFormTypes[$attrs[0]['alias']] = Legacy::supportsFormTypeName()
? $attrs[0]['alias']
: $definition->getClass()
$paymentMethodFormTypes[$attrs[0]['alias']] = Legacy::supportsFormTypeClass()
? $definition->getClass()
: $attrs[0]['alias']
;
}

Expand Down
10 changes: 5 additions & 5 deletions Form/ChoosePaymentMethodType.php
Expand Up @@ -98,9 +98,9 @@ protected function buildChoiceList(FormBuilderInterface $builder, array $options
}
}

$type = Legacy::supportsFormTypeName()
? 'choice'
: 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'
$type = Legacy::supportsFormTypeClass()
? 'Symfony\Component\Form\Extension\Core\Type\ChoiceType'
: 'choice'
;

$builder->add('method', $type, $options);
Expand Down Expand Up @@ -161,11 +161,11 @@ public function configureOptions(OptionsResolver $resolver)
'choice_options' => 'array',
);

if (Legacy::supportsFormTypeConfigureOptions()) {
if (Legacy::supportsOptionsResolverSetAllowedTypesAsArray()) {
$resolver->setAllowedTypes($allowedTypes);
} else {
foreach ($allowedTypes as $key => $value) {
$resolver->addAllowedTypes($key, $value);
$resolver->setAllowedTypes($key, $value);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions Tests/Form/ChoosePaymentMethodTypeTest.php
Expand Up @@ -173,9 +173,9 @@ private function createForm($options = array(), $data = array())
'currency' => 'EUR',
), $options);

$form = Legacy::supportsFormTypeName()
? 'jms_choose_payment_method'
: 'JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType'
$form = Legacy::supportsFormTypeClass()
? 'JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType'
: 'jms_choose_payment_method'
;

$form = $this->factory->create($form, null, $options);
Expand All @@ -196,24 +196,24 @@ protected function getExtensions()
{
$pluginType = new ExpressCheckoutType();

if (Legacy::supportsFormTypeName()) {
$pluginTypeName = $pluginType->getName();
} else {
if (Legacy::supportsFormTypeClass()) {
$pluginTypeName = get_class($pluginType);
} else {
$pluginTypeName = $pluginType->getName();
}

$type = new ChoosePaymentMethodType($this->pluginController, array(
'foo' => $pluginTypeName,
'bar' => $pluginTypeName,
));

if (Legacy::supportsFormTypeName()) {
if (Legacy::supportsFormTypeClass()) {
$extensions = array($pluginType, $type);
} else {
$extensions = array(
$pluginType->getName() => $pluginType,
$type->getName() => $type,
);
} else {
$extensions = array($pluginType, $type);
}

return array(new PreloadedExtension($extensions, array()));
Expand Down
6 changes: 3 additions & 3 deletions Tests/Functional/TestBundle/Controller/OrderController.php
Expand Up @@ -24,9 +24,9 @@ class OrderController extends Controller
*/
public function paymentDetailsAction(Order $order)
{
$formType = Legacy::supportsFormTypeName()
? 'jms_choose_payment_method'
: 'JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType'
$formType = Legacy::supportsFormTypeClass()
? 'JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType'
: 'jms_choose_payment_method'
;

$form = $this->get('form.factory')->create($formType, null, array(
Expand Down
40 changes: 31 additions & 9 deletions Util/Legacy.php
Expand Up @@ -11,23 +11,30 @@
class Legacy
{
/**
* Symfony 3.0 removes support for form type names.
* Symfony 2.8 adds support for form type as FQCN.
*
* Instead of referencing types by name, they must be referenced by their
* Instead of referencing types by name, they should be referenced by their
* fully-qualified class name (FQCN).
*/
public static function supportsFormTypeName()
public static function supportsFormTypeClass()
{
return version_compare(Kernel::VERSION, '3.0.0', '<');
return method_exists(
'Symfony\Component\Form\AbstractType',
'getBlockPrefix'
);
}

/**
* Symfony 3.0 requires using `configureOptions` instead of `setDefaultOptions`
* in form types.
* Before Symfony 2.6, setAllowedTypes() and addAllowedTypes() expected the
* values to be given as an array mapping option names to allowed types:
* $resolver->setAllowedTypes(array('port' => array('null', 'int')));
*/
public static function supportsFormTypeConfigureOptions()
public static function supportsOptionsResolverSetAllowedTypesAsArray()
{
return version_compare(Kernel::VERSION, '3.0.0', '<');
return !method_exists(
'Symfony\Component\OptionsResolver\OptionsResolver',
'setDefined'
);
}

/**
Expand All @@ -43,7 +50,22 @@ public static function supportsFormTypeConfigureOptions()
*/
public static function formChoicesAsValues()
{
return version_compare(Kernel::VERSION, '3.0.0', '<');
return !method_exists(
'Symfony\Component\Form\AbstractType',
'configureOptions'
);
}

/**
* When using `choices_as_values` before Symfony 3.0, one must make sure to
* set the `choices_as_values` option to true
*/
public static function needsChoicesAsValuesOptions()
{
return self::formChoicesAsValues() && !method_exists(
'Symfony\Component\Form\FormTypeInterface',
'setDefaultOptions'
);
}

/**
Expand Down

0 comments on commit 435c1b8

Please sign in to comment.