diff --git a/DataSource/Extension/Symfony/Form/Field/FormFieldExtension.php b/DataSource/Extension/Symfony/Form/Field/FormFieldExtension.php index 712e14e..6d014dc 100644 --- a/DataSource/Extension/Symfony/Form/Field/FormFieldExtension.php +++ b/DataSource/Extension/Symfony/Form/Field/FormFieldExtension.php @@ -28,7 +28,6 @@ use Symfony\Component\Form\FormFactory; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\FormTypeInterface; use Symfony\Component\Translation\TranslatorInterface; class FormFieldExtension extends FieldAbstractExtension @@ -173,18 +172,20 @@ protected function getForm(FieldTypeInterface $field, bool $force = false): ?For return $this->forms[$fieldOid]; } - $options = $field->getOption('form_options'); - $options = array_merge($options, ['required' => false, 'auto_initialize' => false]); + $options = array_merge( + $field->getOption('form_options'), + ['required' => false, 'auto_initialize' => false] + ); $form = $this->formFactory->createNamed( $datasource->getName(), - $this->isFqcnFormTypePossible() ? CollectionType::class : 'collection', + CollectionType::class, null, ['csrf_protection' => false] ); $fieldsForm = $this->formFactory->createNamed( DataSourceInterface::PARAMETER_FIELDS, - $this->isFqcnFormTypePossible() ? FormType::class : 'form', + FormType::class, null, ['auto_initialize' => false] ); @@ -193,18 +194,14 @@ protected function getForm(FieldTypeInterface $field, bool $force = false): ?For case 'between': $this->buildBetweenComparisonForm($fieldsForm, $field, $options); break; - case 'isNull': $this->buildIsNullComparisonForm($fieldsForm, $field, $options); break; - default: - switch ($field->getType()) { case 'boolean': $this->buildBooleanForm($fieldsForm, $field, $options); break; - default: $fieldsForm->add($field->getName(), $this->getFieldFormType($field), $options); } @@ -223,7 +220,7 @@ protected function buildBetweenComparisonForm( ): void { $betweenBuilder = $this->getFormFactory()->createNamedBuilder( $field->getName(), - $this->isFqcnFormTypePossible() ? BetweenType::class : 'datasource_between', + BetweenType::class, null, $options ); @@ -252,30 +249,17 @@ protected function buildIsNullComparisonForm(FormInterface $form, FieldTypeInter } $defaultOptions = [ + 'placeholder' => '', 'choices' => [ - $this->translator->trans('datasource.form.choices.is_null', [], 'DataSourceBundle') => 'null', - $this->translator->trans('datasource.form.choices.is_not_null', [], 'DataSourceBundle') => 'no_null' + $this->translator->trans('datasource.form.choices.is_null', [], 'DataSourceBundle') => 'null', + $this->translator->trans('datasource.form.choices.is_not_null', [], 'DataSourceBundle') => 'no_null' ], ]; - if ($this->isSymfonyForm27()) { - $defaultOptions['placeholder'] = ''; - } else { - $defaultOptions['empty_value'] = ''; - $defaultOptions['choices'] = array_flip($defaultOptions['choices']); - if (isset($options['choices'])) { - $options['choices'] = array_merge( - $defaultOptions['choices'], - array_intersect_key($options['choices'], $defaultOptions['choices']) - ); - } - } - - $options = array_merge($defaultOptions, $options); $form->add( $field->getName(), - $this->isFqcnFormTypePossible() ? ChoiceType::class : 'choice', - $options + ChoiceType::class, + array_merge($defaultOptions, $options) ); } @@ -291,27 +275,17 @@ protected function buildBooleanForm(FormInterface $form, FieldTypeInterface $fie } $defaultOptions = [ + 'placeholder' => '', 'choices' => [ $this->translator->trans('datasource.form.choices.yes', [], 'DataSourceBundle') => '1', - $this->translator->trans('datasource.form.choices.no', [], 'DataSourceBundle') => '0', + $this->translator->trans('datasource.form.choices.no', [], 'DataSourceBundle') => '0' ], ]; - if ($this->isSymfonyForm27()) { - $defaultOptions['placeholder'] = ''; - } else { - $defaultOptions['empty_value'] = ''; - $defaultOptions['choices'] = array_flip($defaultOptions['choices']); - if (isset($options['choices'])) { - $options['choices'] = array_intersect_key($options['choices'], $defaultOptions['choices']); - } - } - - $options = array_merge($defaultOptions, $options); $form->add( $field->getName(), - $this->isFqcnFormTypePossible() ? ChoiceType::class : 'choice', - $options + ChoiceType::class, + array_merge($defaultOptions, $options) ); } @@ -327,11 +301,6 @@ private function getFieldFormType(FieldTypeInterface $field): string } $declaredType = $field->getType(); - - if (!$this->isFqcnFormTypePossible()) { - return $declaredType; - } - switch ($declaredType) { case 'text': return TextType::class; @@ -372,7 +341,7 @@ private function getParameterValue(array $array, FieldTypeInterface $field) * @param FieldTypeInterface $field * @param mixed $value */ - private function setParameterValue(array &$array, FieldTypeInterface $field, $value) + private function setParameterValue(array &$array, FieldTypeInterface $field, $value): void { $array[$field->getDataSource()->getName()][DataSourceInterface::PARAMETER_FIELDS][$field->getName()] = $value; } @@ -381,14 +350,4 @@ private function clearParameterValue(array &$array, FieldTypeInterface $field): { unset($array[$field->getDataSource()->getName()][DataSourceInterface::PARAMETER_FIELDS][$field->getName()]); } - - private function isFqcnFormTypePossible(): bool - { - return class_exists('Symfony\Component\Form\Extension\Core\Type\RangeType'); - } - - private function isSymfonyForm27(): bool - { - return method_exists(FormTypeInterface::class, 'configureOptions'); - } } diff --git a/DataSource/Extension/Symfony/Form/Type/BetweenType.php b/DataSource/Extension/Symfony/Form/Type/BetweenType.php index d9bfd13..ba01fc6 100644 --- a/DataSource/Extension/Symfony/Form/Type/BetweenType.php +++ b/DataSource/Extension/Symfony/Form/Type/BetweenType.php @@ -11,39 +11,14 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolver; -use Symfony\Component\OptionsResolver\OptionsResolverInterface; class BetweenType extends AbstractType { - /** - * {@inheritdoc} - */ - public function setDefaultOptions(OptionsResolverInterface $resolver) - { - $resolver->setDefaults(['label' => false]); - } - - /** - * {@inheritdoc} - */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(['label' => false]); } - /** - * Returns the name of this type. - * - * @return string The name of this type - */ - public function getName() - { - return 'datasource_between'; - } - - /** - * @return string - */ public function getBlockPrefix() { return 'datasource_between'; diff --git a/DependencyInjection/Compiler/TemplatePathPass.php b/DependencyInjection/Compiler/TemplatePathPass.php index c15dd40..6afd069 100644 --- a/DependencyInjection/Compiler/TemplatePathPass.php +++ b/DependencyInjection/Compiler/TemplatePathPass.php @@ -9,28 +9,24 @@ namespace FSi\Bundle\DataSourceBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\ContainerBuilder; +use FSi\Bundle\DataSourceBundle\DataSourceBundle; +use ReflectionClass; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; class TemplatePathPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { - $loaderDefinition = null; - - if ($container->hasDefinition('twig.loader.filesystem')) { - $loaderDefinition = $container->getDefinition('twig.loader.filesystem'); - } elseif ($container->hasDefinition('twig.loader')) { - // Symfony 2.0 and 2.1 were not using an alias for the filesystem loader - $loaderDefinition = $container->getDefinition('twig.loader'); - } - + $loaderDefinition = $container->getDefinition('twig.loader.filesystem'); if (null === $loaderDefinition) { return; } - $refl = new \ReflectionClass('FSi\Bundle\DataSourceBundle\DataSourceBundle'); - $path = dirname($refl->getFileName()).'/Resources/views'; - $loaderDefinition->addMethodCall('addPath', [$path]); + $reflection = new ReflectionClass(DataSourceBundle::class); + $loaderDefinition->addMethodCall( + 'addPath', + [dirname($reflection->getFileName()).'/Resources/views'] + ); } } diff --git a/DependencyInjection/FSIDataSourceExtension.php b/DependencyInjection/FSIDataSourceExtension.php index 477fe38..205e55f 100644 --- a/DependencyInjection/FSIDataSourceExtension.php +++ b/DependencyInjection/FSIDataSourceExtension.php @@ -9,6 +9,15 @@ namespace FSi\Bundle\DataSourceBundle\DependencyInjection; +use FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Collection\EventSubscriberInterface; +use FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Collection\FieldEventSubscriberInterface; +use FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Doctrine\DBAL\EventSubscriberInterface; +use FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Doctrine\ORM; +use FSi\Component\DataSource\Driver\Collection\CollectionAbstractField; +use FSi\Component\DataSource\Driver\Doctrine\DBAL\DBALAbstractField; +use FSi\Component\DataSource\Driver\Doctrine\ORM\DoctrineAbstractField; +use FSi\Component\DataSource\Driver\DriverExtensionInterface; +use FSi\Component\DataSource\Driver\DriverFactoryInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -48,53 +57,38 @@ public function load(array $configs, ContainerBuilder $container) private function registerDrivers(LoaderInterface $loader): void { $loader->load('driver/collection.xml'); - /* doctrine driver is loaded for compatibility with fsi/datasource 1.x only */ - if (class_exists('FSi\Component\DataSource\Driver\Doctrine\DoctrineDriver')) { - $loader->load('driver/doctrine.xml'); - } - if (class_exists('FSi\Component\DataSource\Driver\Doctrine\ORM\DoctrineDriver')) { - $loader->load('driver/doctrine-orm.xml'); - } - if (class_exists('FSi\Component\DataSource\Driver\Doctrine\DBAL\DBALDriver')) { - $loader->load('driver/doctrine-dbal.xml'); - } + $loader->load('driver/doctrine-orm.xml'); + $loader->load('driver/doctrine-dbal.xml'); } private function registerForAutoconfiguration(ContainerBuilder $container): void { - $container->registerForAutoconfiguration('FSi\Component\DataSource\Driver\DriverFactoryInterface') - ->addTag('datasource.driver.factory'); - $container->registerForAutoconfiguration('FSi\Component\DataSource\Driver\DriverExtensionInterface') - ->addTag('datasource.driver.extension'); - $container->registerForAutoconfiguration('FSi\Component\DataSource\Driver\Collection\CollectionAbstractField') - ->addTag('datasource.driver.collection.field'); - $container->registerForAutoconfiguration('FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Collection\FieldEventSubscriberInterface') - ->addTag('datasource.driver.collection.field.subscriber'); - $container->registerForAutoconfiguration('FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Collection\EventSubscriberInterface') - ->addTag('datasource.driver.collection.subscriber'); - if (class_exists('FSi\Component\DataSource\Driver\Doctrine\DoctrineDriver')) { - $container->registerForAutoconfiguration('FSi\Component\DataSource\Driver\Doctrine\DoctrineAbstractField') - ->addTag('datasource.driver.doctrine.field'); - $container->registerForAutoconfiguration('FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Doctrine\FieldEventSubscriberInterface') - ->addTag('datasource.driver.doctrine.field.subscriber'); - $container->registerForAutoconfiguration('FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Doctrine\EventSubscriberInterface') - ->addTag('datasource.driver.doctrine.subscriber'); - } - if (class_exists('FSi\Component\DataSource\Driver\Doctrine\ORM\DoctrineDriver')) { - $container->registerForAutoconfiguration('FSi\Component\DataSource\Driver\Doctrine\ORM\DoctrineAbstractField') - ->addTag('datasource.driver.doctrine-orm.field'); - $container->registerForAutoconfiguration('FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Doctrine\ORM\FieldEventSubscriberInterface') - ->addTag('datasource.driver.doctrine-orm.field.subscriber'); - $container->registerForAutoconfiguration('FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Doctrine\ORM\EventSubscriberInterface') - ->addTag('datasource.driver.doctrine-orm.subscriber'); - } - if (class_exists('FSi\Component\DataSource\Driver\Doctrine\DBAL\DBALDriver')) { - $container->registerForAutoconfiguration('FSi\Component\DataSource\Driver\Doctrine\DBAL\DBALAbstractField') - ->addTag('datasource.driver.doctrine-dbal.field'); - $container->registerForAutoconfiguration('FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Doctrine\DBAL\FieldEventSubscriberInterface') - ->addTag('datasource.driver.doctrine-dbal.field.subscriber'); - $container->registerForAutoconfiguration('FSi\Bundle\DataSourceBundle\DataSource\Extension\Symfony\DependencyInjection\Driver\Doctrine\DBAL\EventSubscriberInterface') - ->addTag('datasource.driver.doctrine-dbal.subscriber'); - } + $container->registerForAutoconfiguration(DriverFactoryInterface::class)->addTag('datasource.driver.factory'); + $container->registerForAutoconfiguration(DriverExtensionInterface::class)->addTag('datasource.driver.extension'); + $container->registerForAutoconfiguration(CollectionAbstractField::class)->addTag('datasource.driver.collection.field'); + $container->registerForAutoconfiguration(FieldEventSubscriberInterface::class) + ->addTag('datasource.driver.collection.field.subscriber') + ; + $container->registerForAutoconfiguration(EventSubscriberInterface::class) + ->addTag('datasource.driver.collection.subscriber') + ; + $container->registerForAutoconfiguration(DoctrineAbstractField::class) + ->addTag('datasource.driver.doctrine-orm.field') + ; + $container->registerForAutoconfiguration(ORM\FieldEventSubscriberInterface::class) + ->addTag('datasource.driver.doctrine-orm.field.subscriber') + ; + $container->registerForAutoconfiguration(ORM\EventSubscriberInterface::class) + ->addTag('datasource.driver.doctrine-orm.subscriber') + ; + $container->registerForAutoconfiguration(DBALAbstractField::class) + ->addTag('datasource.driver.doctrine-dbal.field') + ; + $container->registerForAutoconfiguration(DBAL\FieldEventSubscriberInterface::class) + ->addTag('datasource.driver.doctrine-dbal.field.subscriber') + ; + $container->registerForAutoconfiguration(DBAL\EventSubscriberInterface::class) + ->addTag('datasource.driver.doctrine-dbal.subscriber') + ; } } diff --git a/Resources/views/datasource.html.twig b/Resources/views/datasource.html.twig index bcde60f..ff1882f 100644 --- a/Resources/views/datasource.html.twig +++ b/Resources/views/datasource.html.twig @@ -1,13 +1,13 @@ {% block datasource_filter %} -{% filter spaceless %} +{% apply spaceless %} {% for field in datasource.fields %} {{ datasource_field_widget(field, vars) }} {% endfor %} -{% endfilter %} +{% endapply %} {% endblock %} {% block datasource_field %} -{% filter spaceless %} +{% apply spaceless %} {% if field.hasAttribute('form') is not empty %}