From 39b66d312b81ae509cd72d599981d5d1590c0b9a Mon Sep 17 00:00:00 2001 From: lashus Date: Fri, 11 Dec 2015 09:31:50 +0100 Subject: [PATCH] Add support for symfony 3.0 Add support for symfony 3.0 --- Command/GenerateCommand.php | 2 +- Command/InstallCommand.php | 2 +- DependencyInjection/AsseticConfiguration.php | 11 +++-- .../BraincraftedBootstrapExtension.php | 7 ++- DependencyInjection/Configuration.php | 5 +- Form/Extension/ButtonTypeExtension.php | 5 +- Form/Extension/InputGroupButtonExtension.php | 7 ++- Form/Extension/StaticControlExtension.php | 5 +- Form/Extension/TypeSetterExtension.php | 7 ++- Form/Type/BootstrapCollectionType.php | 27 +++++++++-- Form/Type/FormActionsType.php | 11 ++++- Form/Type/FormStaticControlType.php | 15 +++++- Form/Type/MoneyType.php | 11 ++++- Resources/config/services/form.xml | 8 ++-- Resources/views/Form/bootstrap.html.twig | 6 +-- Tests/Command/GenerateCommandTest.php | 4 +- Tests/Command/InstallCommandTest.php | 16 +++++-- .../AsseticConfigurationTest.php | 4 +- .../InputGroupButtonExtensionTest.php | 22 +++++---- .../Extension/TypeSetterExtensionTest.php | 6 ++- .../Form/Type/BootstrapCollectionTypeTest.php | 9 ++-- Tests/Form/Type/FormActionsTypeTest.php | 22 +++++++-- Tests/Form/Type/MoneyTypeTest.php | 7 +-- Util/LegacyFormHelper.php | 47 +++++++++++++++++++ composer.json | 19 ++++---- 25 files changed, 214 insertions(+), 71 deletions(-) create mode 100644 Util/LegacyFormHelper.php diff --git a/Command/GenerateCommand.php b/Command/GenerateCommand.php index dd0ef03..94eb0c7 100644 --- a/Command/GenerateCommand.php +++ b/Command/GenerateCommand.php @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return; } - $filter = $this->getContainer()->getParameter('braincrafted_bootstrap.less_filter'); + $filter = $this->getContainer()->getParameter('braincrafted_bootstrap.css_preprocessor'); if ('less' !== $filter && 'lessphp' !== $filter) { $output->writeln( 'Bundle must be configured with "less" or "lessphp" to generated bootstrap.less' diff --git a/Command/InstallCommand.php b/Command/InstallCommand.php index 659ee64..99f2e48 100644 --- a/Command/InstallCommand.php +++ b/Command/InstallCommand.php @@ -89,7 +89,7 @@ protected function getSrcDir() $this->getContainer()->getParameter('braincrafted_bootstrap.assets_dir'), ( // Sass version stores fonts in a different directory - in_array($this->getContainer()->getParameter('braincrafted_bootstrap.less_filter'), array('sass', 'scssphp')) ? + in_array($this->getContainer()->getParameter('braincrafted_bootstrap.css_preprocessor'), array('sass', 'scssphp')) ? 'fonts/bootstrap' : 'fonts' ) diff --git a/DependencyInjection/AsseticConfiguration.php b/DependencyInjection/AsseticConfiguration.php index 44745a9..9722a59 100644 --- a/DependencyInjection/AsseticConfiguration.php +++ b/DependencyInjection/AsseticConfiguration.php @@ -35,9 +35,10 @@ public function build(array $config) $config['output_dir'] .= '/'; } - if (in_array($config['less_filter'], array('sass', 'scssphp'))) { + // changed from css_preprocessor to css_preprocessor for 3.0 + if (in_array($config['css_preprocessor'], array('sass', 'scssphp'))) { $output['bootstrap_css'] = $this->buildCssWithSass($config); - } elseif ('none' !== $config['less_filter']) { + } elseif ('none' !== $config['css_preprocessor']) { $output['bootstrap_css'] = $this->buildCssWithLess($config); } else { $output['bootstrap_css'] = $this->buildCssWithoutLess($config); @@ -93,7 +94,7 @@ protected function buildCssWithLess(array $config) return array( 'inputs' => $inputs, - 'filters' => array($config['less_filter']), + 'filters' => array($config['css_preprocessor']), 'output' => $config['output_dir'].'css/bootstrap.css' ); } @@ -121,7 +122,7 @@ protected function buildCssWithSass(array $config) return array( 'inputs' => $inputs, - 'filters' => array($config['less_filter']), + 'filters' => array($config['css_preprocessor']), 'output' => $config['output_dir'].'css/bootstrap.css' ); } @@ -133,7 +134,7 @@ protected function buildCssWithSass(array $config) */ protected function buildJs(array $config) { - $path = !in_array($config['less_filter'], array('sass', 'scssphp')) ? "/js" : "/javascripts/bootstrap"; + $path = !in_array($config['css_preprocessor'], array('sass', 'scssphp')) ? "/js" : "/javascripts/bootstrap"; return array( 'inputs' => array( diff --git a/DependencyInjection/BraincraftedBootstrapExtension.php b/DependencyInjection/BraincraftedBootstrapExtension.php index 479e6f4..5321842 100644 --- a/DependencyInjection/BraincraftedBootstrapExtension.php +++ b/DependencyInjection/BraincraftedBootstrapExtension.php @@ -61,7 +61,9 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('braincrafted_bootstrap.fontawesome_dir', $config['fontawesome_dir']); $container->setParameter('braincrafted_bootstrap.fonts_dir', $config['fonts_dir']); $container->setParameter('braincrafted_bootstrap.output_dir', $config['output_dir']); - $container->setParameter('braincrafted_bootstrap.less_filter', $config['less_filter']); + + // changed from css_preprocessor to css_preprocessor for 3.0 + $container->setParameter('braincrafted_bootstrap.css_preprocessor', $config['css_preprocessor']); $container->setParameter('braincrafted_bootstrap.icon_prefix', $config['icon_prefix']); $container->setParameter('braincrafted_bootstrap.icon_tag', $config['icon_tag']); } @@ -114,7 +116,8 @@ public function prepend(ContainerBuilder $container) */ protected function processSassConfiguration(array $config) { - if (in_array($config['less_filter'], array('sass', 'scssphp'))) { + // changed from css_preprocessor to css_preprocessor for 3.0 + if (in_array($config['css_preprocessor'], array('sass', 'scssphp'))) { if ($config['assets_dir'] === Configuration::DEFAULT_ASSETS_DIR) { $config['assets_dir'] = Configuration::DEFAULT_ASSETS_DIR_SASS; } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 376cc11..2d8fd20 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -77,8 +77,9 @@ private function buildConfigTree() ->scalarNode('fonts_dir') ->defaultValue(self::DEFAULT_FONTS_DIR) ->end() - // TODO for v3.0: Rename to css_preprocessor - ->scalarNode('less_filter') + + // renamed from css_preprocessor to css_preprocessor + ->scalarNode('css_preprocessor') ->defaultValue('less') ->validate() ->ifNotInArray(array('less', 'lessphp', 'sass', 'scssphp', 'none')) diff --git a/Form/Extension/ButtonTypeExtension.php b/Form/Extension/ButtonTypeExtension.php index 7f39755..177608d 100644 --- a/Form/Extension/ButtonTypeExtension.php +++ b/Form/Extension/ButtonTypeExtension.php @@ -7,6 +7,8 @@ use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormInterface; +use Braincrafted\Bundle\BootstrapBundle\Util\LegacyFormHelper; + /** * FormControlStaticType * @@ -43,6 +45,7 @@ public function configureOptions(OptionsResolver $resolver) */ public function getExtendedType() { - return 'button'; + // map old class to new one using LegacyFormHelper + return LegacyFormHelper::getType('button'); } } \ No newline at end of file diff --git a/Form/Extension/InputGroupButtonExtension.php b/Form/Extension/InputGroupButtonExtension.php index ad231bf..51a7c20 100644 --- a/Form/Extension/InputGroupButtonExtension.php +++ b/Form/Extension/InputGroupButtonExtension.php @@ -8,6 +8,8 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; +use Braincrafted\Bundle\BootstrapBundle\Util\LegacyFormHelper; + /** * Class InputGroupButtonExtension * @@ -34,7 +36,8 @@ class InputGroupButtonExtension extends AbstractTypeExtension */ public function getExtendedType() { - return 'text'; + // map old class to new one using LegacyFormHelper + return LegacyFormHelper::getType('text'); } /** @@ -100,6 +103,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) */ protected function addButton(FormBuilderInterface $builder, $config) { + $options = (isset($config['options']))? $config['options'] : array(); return $builder->create($config['name'], $config['type'], $options); @@ -114,6 +118,7 @@ protected function addButton(FormBuilderInterface $builder, $config) */ protected function storeButton(ButtonBuilder $buttonBuilder, FormBuilderInterface $form, $position) { + if (!isset($this->buttons[$form->getName()])) { $this->buttons[$form->getName()] = array(); } diff --git a/Form/Extension/StaticControlExtension.php b/Form/Extension/StaticControlExtension.php index 7f591b5..e82a3aa 100644 --- a/Form/Extension/StaticControlExtension.php +++ b/Form/Extension/StaticControlExtension.php @@ -8,6 +8,8 @@ use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormBuilderInterface; +use Braincrafted\Bundle\BootstrapBundle\Util\LegacyFormHelper; + /** * StaticControlExtension * @@ -58,6 +60,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) */ public function getExtendedType() { - return 'form'; + // map old class to new one using LegacyFormHelper + return LegacyFormHelper::getType('form'); } } diff --git a/Form/Extension/TypeSetterExtension.php b/Form/Extension/TypeSetterExtension.php index 1746d4f..26a24c7 100644 --- a/Form/Extension/TypeSetterExtension.php +++ b/Form/Extension/TypeSetterExtension.php @@ -10,6 +10,8 @@ use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormInterface; +use Braincrafted\Bundle\BootstrapBundle\Util\LegacyFormHelper; + /** * TypeSetterExtension * @@ -27,7 +29,7 @@ class TypeSetterExtension extends AbstractTypeExtension */ public function buildView(FormView $view, FormInterface $form, array $options) { - $view->vars['original_type'] = $form->getConfig()->getType()->getName(); + $view->vars['original_type'] = LegacyFormHelper::isLegacy() ? $form->getConfig()->getType()->getName() : $form->getConfig()->getType()->getBlockPrefix(); } /** @@ -35,6 +37,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) */ public function getExtendedType() { - return "form"; + // map old class to new one using LegacyFormHelper + return LegacyFormHelper::getType('form'); } } diff --git a/Form/Type/BootstrapCollectionType.php b/Form/Type/BootstrapCollectionType.php index e71215a..ef68c7e 100644 --- a/Form/Type/BootstrapCollectionType.php +++ b/Form/Type/BootstrapCollectionType.php @@ -12,6 +12,8 @@ use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; +use Braincrafted\Bundle\BootstrapBundle\Util\LegacyFormHelper; + /** * BootstrapCollectionType * @@ -64,18 +66,23 @@ public function configureOptions(OptionsResolver $resolver) // @codeCoverageIgnoreEnd }; - $resolver->setDefaults(array( + $defaults = array( 'allow_add' => false, 'allow_delete' => false, 'prototype' => true, 'prototype_name' => '__name__', - 'type' => 'text', 'add_button_text' => 'Add', 'delete_button_text' => 'Delete', 'sub_widget_col' => 10, 'button_col' => 2, 'options' => array(), - )); + ); + + + // map old class to new one using LegacyFormHelper + $defaults['type'] = LegacyFormHelper::getType('text'); + + $resolver->setDefaults($defaults); $resolver->setNormalizer('options', $optionsNormalizer); } @@ -85,14 +92,24 @@ public function configureOptions(OptionsResolver $resolver) */ public function getParent() { - return 'collection'; + // map old class to new one using LegacyFormHelper + return LegacyFormHelper::getType('collection'); } /** * {@inheritDoc} */ - public function getName() + public function getBlockPrefix() { return 'bootstrap_collection'; } + + /** + * Backward compatibility for SF < 3.0 + * + * @return null|string + */ + public function getName() { + return $this->getBlockPrefix(); + } } diff --git a/Form/Type/FormActionsType.php b/Form/Type/FormActionsType.php index 80821ec..1b27f07 100644 --- a/Form/Type/FormActionsType.php +++ b/Form/Type/FormActionsType.php @@ -92,8 +92,17 @@ public function configureOptions(OptionsResolver $resolver) /** * {@inheritdoc} */ - public function getName() + public function getBlockPrefix() { return 'form_actions'; } + + /** + * Backward compatibility for SF < 3.0 + * + * @return null|string + */ + public function getName() { + return $this->getBlockPrefix(); + } } diff --git a/Form/Type/FormStaticControlType.php b/Form/Type/FormStaticControlType.php index 0039b07..c838804 100644 --- a/Form/Type/FormStaticControlType.php +++ b/Form/Type/FormStaticControlType.php @@ -7,6 +7,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Form\Extension\Core\Type\TextType; /** * FormStaticControlType @@ -37,13 +38,23 @@ public function configureOptions(OptionsResolver $resolver) */ public function getParent() { - return 'text'; + // map old class to new one using LegacyFormHelper + return LegacyFormHelper::getType('text'); + } + + /** + * Backward compatibility for SF < 3.0 + * + * @return null|string + */ + public function getName() { + return $this->getBlockPrefix(); } /** * {@inheritdoc} */ - public function getName() + public function getBlockPrefix() { return 'bs_static'; } diff --git a/Form/Type/MoneyType.php b/Form/Type/MoneyType.php index d2f5f45..4eaacd2 100644 --- a/Form/Type/MoneyType.php +++ b/Form/Type/MoneyType.php @@ -33,11 +33,20 @@ public function buildView(FormView $view, FormInterface $form, array $options) /** * {@inheritdoc} */ - public function getName() + public function getBlockPrefix() { return 'money'; } + /** + * Backward compatibility for SF < 3.0 + * + * @return null|string + */ + public function getName() { + return $this->getBlockPrefix(); + } + /** * Returns the pattern for this locale * diff --git a/Resources/config/services/form.xml b/Resources/config/services/form.xml index f0d94e3..31d085a 100644 --- a/Resources/config/services/form.xml +++ b/Resources/config/services/form.xml @@ -33,18 +33,18 @@ - + - + - + - + diff --git a/Resources/views/Form/bootstrap.html.twig b/Resources/views/Form/bootstrap.html.twig index c2f03a1..4787823 100644 --- a/Resources/views/Form/bootstrap.html.twig +++ b/Resources/views/Form/bootstrap.html.twig @@ -197,8 +197,8 @@ {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-control')|trim }) %}