From b6efcf62eb9c1841cd99f796d42938ed0e24f104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20B=C5=82oszyk?= Date: Tue, 26 May 2020 14:56:48 +0200 Subject: [PATCH] Improve CoreBundle before split Improve deprecation messages --- .../Compiler/FormCompilerPass.php | 158 ++++++++++++++++++ .../Compiler/TwigCompilerPass.php | 107 ++++++++++++ .../SonataCoreExtension.php | 10 +- src/CoreBundle/Resources/config/date.xml | 8 - src/CoreBundle/Resources/config/flash.xml | 14 -- .../Resources/config/form_types.xml | 30 ---- src/CoreBundle/Resources/config/twig.xml | 12 -- src/CoreBundle/SonataCoreBundle.php | 4 + tests/CoreBundle/SonataCoreBundleTest.php | 16 +- 9 files changed, 289 insertions(+), 70 deletions(-) create mode 100644 src/CoreBundle/DependencyInjection/Compiler/FormCompilerPass.php create mode 100644 src/CoreBundle/DependencyInjection/Compiler/TwigCompilerPass.php delete mode 100644 src/CoreBundle/Resources/config/date.xml delete mode 100644 src/CoreBundle/Resources/config/flash.xml diff --git a/src/CoreBundle/DependencyInjection/Compiler/FormCompilerPass.php b/src/CoreBundle/DependencyInjection/Compiler/FormCompilerPass.php new file mode 100644 index 00000000..92c9607f --- /dev/null +++ b/src/CoreBundle/DependencyInjection/Compiler/FormCompilerPass.php @@ -0,0 +1,158 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\CoreBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** + * @author Thomas Rabaix + * + * @deprecated since sonata-project/core-bundle 3.x, to be removed in 4.0. + */ +final class FormCompilerPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + $this->registerDateAliases($container); + $this->registerFormTypeAlias($container); + $this->forceUserToMoveConfig($container); + } + + private function registerDateAliases(ContainerBuilder $container) + { + $container + ->setAlias('sonata.core.date.moment_format_converter', 'sonata.form.date.moment_format_converter') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.date.moment_format_converter" instead.' + ); + } + + private function registerFormTypeAlias(ContainerBuilder $container) + { + $container + ->setAlias('sonata.core.form.type.array', 'sonata.form.type.array') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.array" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.boolean', 'sonata.form.type.boolean') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.boolean" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.collection', 'sonata.form.type.collection') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.collection" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.date_range', 'sonata.form.type.date_range') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.date_range" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.datetime_range', 'sonata.form.type.datetime_range') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.datetime_range" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.date_picker', 'sonata.form.type.date_picker') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.date_picker" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.datetime_picker', 'sonata.form.type.datetime_picker') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.datetime_picker" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.date_range_picker', 'sonata.form.type.date_range_picker') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.date_range_picker" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.datetime_range_picker', 'sonata.form.type.datetime_range_picker') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.datetime_range_picker" instead.' + ); + + $container + ->setAlias('sonata.core.form.type.equal', 'sonata.form.type.equal') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.form.type.equal" instead.' + ); + } + + private function forceUserToMoveConfig(ContainerBuilder $container) + { + $bundles = $container->getParameter('kernel.bundles'); + + if (!isset($bundles['SonataFormBundle'])) { + return; + } + + $defaultForm = [ + 'mapping' => [ + 'enabled' => true, + 'type' => [], + 'extension' => [], + ], + ]; + $defaultSerializer = [ + 'formats' => [ + 0 => 'json', + 1 => 'xml', + 2 => 'yml', + ], + ]; + + if ($container->getParameter('sonata.core.form') !== $defaultForm) { + throw new \Exception('Move bundle config from sonata_core.form to sonata_form.form'); + } + + if ($container->getParameter('sonata.core.serializer') !== $defaultSerializer) { + throw new \Exception('Move bundle config from sonata_core.serializer to sonata_form.serializer'); + } + } +} diff --git a/src/CoreBundle/DependencyInjection/Compiler/TwigCompilerPass.php b/src/CoreBundle/DependencyInjection/Compiler/TwigCompilerPass.php new file mode 100644 index 00000000..26de27ca --- /dev/null +++ b/src/CoreBundle/DependencyInjection/Compiler/TwigCompilerPass.php @@ -0,0 +1,107 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\CoreBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** + * @author Thomas Rabaix + * + * @deprecated since sonata-project/core-bundle 3.x, to be removed in 4.0. + */ +final class TwigCompilerPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + $this->registerFlashAliases($container); + $this->registerTwigAliases($container); + $this->forceUserToMoveConfig($container); + } + + private function registerFlashAliases(ContainerBuilder $container) + { + $container + ->setAlias('sonata.core.flashmessage.manager', 'sonata.twig.flashmessage.manager') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.twig.flashmessage.manager" instead.' + ); + + $container + ->setAlias('sonata.core.flashmessage.twig.runtime', 'sonata.twig.flashmessage.twig.runtime') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.twig.flashmessage.twig.runtime" instead.' + ); + + $container + ->setAlias('sonata.core.flashmessage.twig.extension', 'sonata.twig.flashmessage.twig.extension') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.twig.flashmessage.twig.extension" instead.' + ); + } + + private function registerTwigAliases(ContainerBuilder $container) + { + $container + ->setAlias('sonata.core.twig.extension.wrapping', 'sonata.twig.extension.wrapping') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.twig.extension.wrapping" instead.' + ); + + $container + ->setAlias('sonata.core.twig.status_runtime', 'sonata.twig.status_runtime') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.twig.status_runtime" instead.' + ); + + $container + ->setAlias('sonata.core.twig.deprecated_template_extension', 'sonata.twig.deprecated_template_extension') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.twig.deprecated_template_extension" instead.' + ); + + $container + ->setAlias('sonata.core.twig.template_extension', 'sonata.twig.template_extension') + ->setPublic(true) + ->setDeprecated( + true, + 'The "%alias_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.twig.template_extension" instead.' + ); + } + + private function forceUserToMoveConfig(ContainerBuilder $container) + { + $bundles = $container->getParameter('kernel.bundles'); + + if (!isset($bundles['SonataTwigBundle'])) { + return; + } + + if (!empty($container->getParameter('sonata.core.flashmessage'))) { + throw new \Exception('Move bundle config from sonata_core.flashmessage to sonata_twig.flashmessage'); + } + } +} diff --git a/src/CoreBundle/DependencyInjection/SonataCoreExtension.php b/src/CoreBundle/DependencyInjection/SonataCoreExtension.php index e780a0e4..3586134d 100644 --- a/src/CoreBundle/DependencyInjection/SonataCoreExtension.php +++ b/src/CoreBundle/DependencyInjection/SonataCoreExtension.php @@ -94,12 +94,10 @@ public function load(array $configs, ContainerBuilder $container) $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('commands.xml'); $loader->load('core.xml'); - $loader->load('date.xml'); - $loader->load('flash.xml'); $loader->load('form_types.xml'); $loader->load('model_adapter.xml'); - $loader->load('twig.xml'); $loader->load('validator.xml'); + $loader->load('twig.xml'); if (!isset($bundles['SonataFormBundle'])) { $loader->load('form/date.xml'); @@ -110,10 +108,14 @@ public function load(array $configs, ContainerBuilder $container) if (!isset($bundles['SonataTwigBundle'])) { $loader->load('twig/flash.xml'); $loader->load('twig/twig.xml'); + + $this->registerFlashTypes($container, $config); } - $this->registerFlashTypes($container, $config); + $container->setParameter('sonata.core.form', $config['form']); $container->setParameter('sonata.core.form_type', $config['form_type']); + $container->setParameter('sonata.core.flashmessage', $config['flashmessage']); + $container->setParameter('sonata.core.serializer', $config['serializer']); $this->configureFormFactory($container, $config); if (\PHP_VERSION_ID < 70000) { diff --git a/src/CoreBundle/Resources/config/date.xml b/src/CoreBundle/Resources/config/date.xml deleted file mode 100644 index 3d6018d9..00000000 --- a/src/CoreBundle/Resources/config/date.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - diff --git a/src/CoreBundle/Resources/config/flash.xml b/src/CoreBundle/Resources/config/flash.xml deleted file mode 100644 index 7b0b550c..00000000 --- a/src/CoreBundle/Resources/config/flash.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - diff --git a/src/CoreBundle/Resources/config/form_types.xml b/src/CoreBundle/Resources/config/form_types.xml index c94be41e..6c677613 100644 --- a/src/CoreBundle/Resources/config/form_types.xml +++ b/src/CoreBundle/Resources/config/form_types.xml @@ -63,35 +63,5 @@ The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - diff --git a/src/CoreBundle/Resources/config/twig.xml b/src/CoreBundle/Resources/config/twig.xml index e68c68c5..b62fcae8 100644 --- a/src/CoreBundle/Resources/config/twig.xml +++ b/src/CoreBundle/Resources/config/twig.xml @@ -1,25 +1,13 @@ - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "sonata.twig.status_extension" instead. - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - - - The "%service_id%" service is deprecated since sonata-project/core-bundle 3.19 and will be removed in 4.0. Use "%alias_id%" instead. - diff --git a/src/CoreBundle/SonataCoreBundle.php b/src/CoreBundle/SonataCoreBundle.php index 9709ee01..b083fa3a 100644 --- a/src/CoreBundle/SonataCoreBundle.php +++ b/src/CoreBundle/SonataCoreBundle.php @@ -15,8 +15,10 @@ use Nelmio\ApiDocBundle\Form\Extension\DescriptionFormTypeExtension; use Sonata\CoreBundle\DependencyInjection\Compiler\AdapterCompilerPass; +use Sonata\CoreBundle\DependencyInjection\Compiler\FormCompilerPass; use Sonata\CoreBundle\DependencyInjection\Compiler\FormFactoryCompilerPass; use Sonata\CoreBundle\DependencyInjection\Compiler\StatusRendererCompilerPass; +use Sonata\CoreBundle\DependencyInjection\Compiler\TwigCompilerPass; use Sonata\CoreBundle\Form\FormHelper; use Sonata\CoreBundle\Form\Type\ColorSelectorType; use Sonata\CoreBundle\Form\Type\ColorType; @@ -80,6 +82,8 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new StatusRendererCompilerPass()); $container->addCompilerPass(new AdapterCompilerPass()); $container->addCompilerPass(new FormFactoryCompilerPass()); + $container->addCompilerPass(new FormCompilerPass()); + $container->addCompilerPass(new TwigCompilerPass()); $this->registerFormMapping(); } diff --git a/tests/CoreBundle/SonataCoreBundleTest.php b/tests/CoreBundle/SonataCoreBundleTest.php index d5e7ba12..f3ab135d 100644 --- a/tests/CoreBundle/SonataCoreBundleTest.php +++ b/tests/CoreBundle/SonataCoreBundleTest.php @@ -15,8 +15,10 @@ use PHPUnit\Framework\TestCase; use Sonata\CoreBundle\DependencyInjection\Compiler\AdapterCompilerPass; +use Sonata\CoreBundle\DependencyInjection\Compiler\FormCompilerPass; use Sonata\CoreBundle\DependencyInjection\Compiler\FormFactoryCompilerPass; use Sonata\CoreBundle\DependencyInjection\Compiler\StatusRendererCompilerPass; +use Sonata\CoreBundle\DependencyInjection\Compiler\TwigCompilerPass; use Sonata\CoreBundle\Form\FormHelper; use Sonata\CoreBundle\Form\Type\ColorSelectorType; use Sonata\CoreBundle\Form\Type\ColorType; @@ -93,11 +95,21 @@ public function testBuild() return; } + if ($pass instanceof FormCompilerPass) { + return; + } + + if ($pass instanceof TwigCompilerPass) { + return; + } + $this->fail(sprintf( 'Compiler pass is not one of the expected types. Expects "Sonata\AdminBundle\DependencyInjection\Compiler\StatusRendererCompilerPass", - "Sonata\AdminBundle\DependencyInjection\Compiler\AdapterCompilerPass" or - "Sonata\AdminBundle\DependencyInjection\Compiler\FormFactoryCompilerPass", but got "%s".', + "Sonata\CoreBundle\DependencyInjection\Compiler\AdapterCompilerPass" or + "Sonata\CoreBundle\DependencyInjection\Compiler\FormFactoryCompilerPass or + "Sonata\CoreBundle\DependencyInjection\Compiler\FormCompilerPass or + "Sonata\CoreBundle\DependencyInjection\Compiler\TwigCompilerPass", but got "%s".', \get_class($pass) )); });