Skip to content

Commit

Permalink
Merge pull request #76 from szymach/2.1
Browse files Browse the repository at this point in the history
Dropped support for DataSource 1.x, Twig 1.x and Symfony 2.x
  • Loading branch information
rn0 authored Aug 19, 2019
2 parents ee7c147 + 6b34feb commit f912147
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 228 deletions.
75 changes: 17 additions & 58 deletions DataSource/Extension/Symfony/Form/Field/FormFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
);
Expand All @@ -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);
}
Expand All @@ -223,7 +220,7 @@ protected function buildBetweenComparisonForm(
): void {
$betweenBuilder = $this->getFormFactory()->createNamedBuilder(
$field->getName(),
$this->isFqcnFormTypePossible() ? BetweenType::class : 'datasource_between',
BetweenType::class,
null,
$options
);
Expand Down Expand Up @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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');
}
}
25 changes: 0 additions & 25 deletions DataSource/Extension/Symfony/Form/Type/BetweenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
22 changes: 9 additions & 13 deletions DependencyInjection/Compiler/TemplatePathPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
);
}
}
82 changes: 38 additions & 44 deletions DependencyInjection/FSIDataSourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
;
}
}
16 changes: 8 additions & 8 deletions Resources/views/datasource.html.twig
Original file line number Diff line number Diff line change
@@ -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 %}
<div>
{% for child in field.getAttribute('form').children %}
Expand All @@ -19,11 +19,11 @@
{% endfor %}
</div>
{% endif %}
{% endfilter %}
{% endapply %}
{% endblock %}

{% block datasource_sort %}
{% filter spaceless %}
{% apply spaceless %}
{% if (field.getAttribute('sorted_ascending')) %}
<span>{{ ascending|raw }}</span>
{% else %}
Expand All @@ -34,11 +34,11 @@
{% else %}
<a href="{{ descending_url }}">{{ descending|raw }}</a>
{% endif %}
{% endfilter %}
{% endapply %}
{% endblock %}

{% block datasource_pagination %}
{% filter spaceless %}
{% apply spaceless %}
<ul>
<li{% if current == first %} class="{{ disabled_class }}"{% endif %}>
<a href="{% if current != first %}{{ first_url }}{% else %}#{% endif %}">{{ 'datasource.pagination.first'|trans({}, translation_domain) }}</a>
Expand All @@ -58,7 +58,7 @@
<a href="{% if current != last %}{{ last_url }}{% else %}#{% endif %}">{{ 'datasource.pagination.last'|trans({}, translation_domain) }}</a>
</li>
</ul>
{% endfilter %}
{% endapply %}
{% endblock %}

{% block datasource_max_results_widget %}
Expand Down
Loading

0 comments on commit f912147

Please sign in to comment.