瀏覽代碼

Don't overwrite DiExtraBundle default configuration (#4320)

Preserve the default for the annotation_patterns option of
DiExtraBundle, otherwise annotations provided by DiExtraBundle itself
break if the project has no custom configuration for
annotation_patterns.
Andreas Ferber 8 年之前
父節點
當前提交
fece120760

+ 4 - 0
DependencyInjection/Configuration.php

@@ -104,6 +104,10 @@ class Configuration implements ConfigurationInterface
                             ->defaultFalse()
                             ->info('Enable locking when editing an object, if the corresponding object manager supports it.')
                         ->end()
+                        ->booleanNode('enable_jms_di_extra_autoregistration') // NEXT_MAJOR: remove this option
+                            ->defaultTrue()
+                            ->info('Enable automatic registration of annotations with JMSDiExtraBundle')
+                        ->end()
                     ->end()
                 ->end()
                 ->arrayNode('dashboard')

+ 49 - 9
DependencyInjection/SonataAdminExtension.php

@@ -208,22 +208,62 @@ class SonataAdminExtension extends Extension implements PrependExtensionInterfac
     /**
      * Allow an extension to prepend the extension configurations.
      *
+     * NEXT_MAJOR: remove all code that deals with JMSDiExtraBundle
+     *
      * @param ContainerBuilder $container
      */
     public function prepend(ContainerBuilder $container)
     {
         $bundles = $container->getParameter('kernel.bundles');
 
-        if (isset($bundles['JMSDiExtraBundle'])) {
-            $container->prependExtensionConfig(
-                'jms_di_extra',
-                array(
-                    'annotation_patterns' => array(
-                        'Sonata\AdminBundle\Annotation',
-                    ),
-                )
-            );
+        if (!isset($bundles['JMSDiExtraBundle'])) {
+            return;
+        }
+
+        $configs = $container->getExtensionConfig($this->getAlias());
+        $config = $this->processConfiguration(new Configuration(), $configs);
+        if (!$config['options']['enable_jms_di_extra_autoregistration']) {
+            return;
+        }
+
+        $sonataAdminPattern = 'Sonata\AdminBundle\Annotation';
+        $annotationPatternsConfigured = false;
+
+        $diExtraConfigs = $container->getExtensionConfig('jms_di_extra');
+        foreach ($diExtraConfigs as $diExtraConfig) {
+            if (isset($diExtraConfig['annotation_patterns'])) {
+                // don't add our own pattern if user has already done so
+                if (array_search($sonataAdminPattern, $diExtraConfig['annotation_patterns']) !== false) {
+                    return;
+                }
+                $annotationPatternsConfigured = true;
+                break;
+            }
+        }
+
+        @trigger_error(
+            'Automatic registration of annotations is deprecated since 3.14, to be removed in 4.0.',
+            E_USER_DEPRECATED
+        );
+
+        if ($annotationPatternsConfigured) {
+            $annotationPatterns = array($sonataAdminPattern);
+        } else {
+            // get annotation_patterns default from DiExtraBundle configuration
+            $diExtraConfigDefinition = new \JMS\DiExtraBundle\DependencyInjection\Configuration();
+            // FIXME: this will break if DiExtraBundle adds any mandatory configuration
+            $diExtraConfig = $this->processConfiguration($diExtraConfigDefinition, array());
+
+            $annotationPatterns = $diExtraConfig['annotation_patterns'];
+            $annotationPatterns[] = $sonataAdminPattern;
         }
+
+        $container->prependExtensionConfig(
+            'jms_di_extra',
+            array(
+                'annotation_patterns' => $annotationPatterns,
+            )
+        );
     }
 
     public function configureClassesToCompile()

+ 24 - 0
Resources/doc/reference/annotations.rst

@@ -10,6 +10,30 @@ All annotations require jms/di-extra-bundle, it can easily be installed by compo
 
 if you want to know more: http://jmsyst.com/bundles/JMSDiExtraBundle
 
+The annotations get registered with JMSDiExtraBundle automatically if it is installed.
+If you need to disable this for some reason, you can do this via the configuration:
+
+.. configuration-block::
+
+    .. code-block:: yaml
+
+        sonata_admin:
+            options:
+                enable_jms_di_extra_autoregistration: false
+
+.. note::
+
+    Starting with version 4.0, SonataAdminBundle will no longer register
+    annotations with JMSDiExtraBundle automatically. Please add the following to
+    your config.yml to register the annotations yourself:
+
+    .. code-block:: yaml
+
+        jms_di_extra:
+            annotation_patterns:
+                - JMS\DiExtraBundle\Annotation
+                - Sonata\AdminBundle\Annotation
+
 
 Define Admins
 ^^^^^^^^^^^^^

+ 3 - 0
Resources/doc/reference/configuration.rst

@@ -82,6 +82,9 @@ Full Configuration Options
 
                 # Enable locking when editing an object, if the corresponding object manager supports it.
                 lock_protection:      false
+
+                # Enable automatic registration of annotations with JMSDiExtraBundle
+                enable_jms_di_extra_autoregistration: true
             dashboard:
                 groups:
 

+ 17 - 2
UPGRADE-3.x.md

@@ -1,6 +1,21 @@
 UPGRADE 3.x
 ===========
 
+## Deprecated automatic annotation registration with JMSDiExtraBundle
+
+Starting with version 4.0, SonataAdminBundle will no longer register
+annotations with JMSDiExtraBundle automatically. Please add the following to
+your config.yml to register the annotations yourself:
+
+
+```yaml
+jms_di_extra:
+    annotation_patterns:
+        - JMS\DiExtraBundle\Annotation
+        - Sonata\AdminBundle\Annotation
+```
+
+
 UPGRADE FROM 3.11 to 3.12
 =========================
 
@@ -46,8 +61,8 @@ Use `Sonata\AdminBundle\Form\Type\ModelListType` instead.
 
 ### Tests
 
-All files under the ``Tests`` directory are now correctly handled as internal test classes. 
-You can't extend them anymore, because they are only loaded when running internal tests. 
+All files under the ``Tests`` directory are now correctly handled as internal test classes.
+You can't extend them anymore, because they are only loaded when running internal tests.
 More information can be found in the [composer docs](https://getcomposer.org/doc/04-schema.md#autoload-dev).
 
 UPGRADE FROM 3.2 to 3.3