浏览代码

Replace BaseFieldDescription::camelize with Inflector::classify (#3788)

Oskar Stark 9 年之前
父节点
当前提交
5dc9071790

+ 14 - 3
Admin/AdminHelper.php

@@ -198,13 +198,13 @@ class AdminHelper
         $instance = $fieldDescription->getAssociationAdmin()->getNewInstance();
         $mapping = $fieldDescription->getAssociationMapping();
 
-        $method = sprintf('add%s', $this->camelize($mapping['fieldName']));
+        $method = sprintf('add%s', Inflector::classify($mapping['fieldName']));
 
         if (!method_exists($object, $method)) {
             $method = rtrim($method, 's');
 
             if (!method_exists($object, $method)) {
-                $method = sprintf('add%s', $this->camelize(Inflector::singularize($mapping['fieldName'])));
+                $method = sprintf('add%s', Inflector::classify(Inflector::singularize($mapping['fieldName'])));
 
                 if (!method_exists($object, $method)) {
                     throw new \RuntimeException(sprintf('Please add a method %s in the %s class!', $method, ClassUtils::getClass($object)));
@@ -223,10 +223,21 @@ class AdminHelper
      * @param string $property
      *
      * @return string
+     *
+     * @deprecated Deprecated since version 3.1. Use \Doctrine\Common\Inflector\Inflector::classify() instead.
      */
     public function camelize($property)
     {
-        return BaseFieldDescription::camelize($property);
+        @trigger_error(
+            sprintf(
+                'The %s method is deprecated since 3.1 and will be removed in 4.0. '.
+                'Use \Doctrine\Common\Inflector\Inflector::classify() instead.',
+                __METHOD__
+            ),
+            E_USER_DEPRECATED
+        );
+
+        return Inflector::classify($property);
     }
 
     /**

+ 14 - 3
Admin/BaseFieldDescription.php

@@ -11,8 +11,8 @@
 
 namespace Sonata\AdminBundle\Admin;
 
+use Doctrine\Common\Inflector\Inflector;
 use Sonata\AdminBundle\Exception\NoValueException;
-use Symfony\Component\DependencyInjection\Container;
 
 /**
  * A FieldDescription hold the information about a field. A typical
@@ -325,7 +325,7 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
             return;
         }
 
-        $camelizedFieldName = self::camelize($fieldName);
+        $camelizedFieldName = Inflector::classify($fieldName);
 
         $getters = array();
         $parameters = array();
@@ -422,10 +422,21 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
      * @param string $property
      *
      * @return string
+     *
+     * @deprecated Deprecated since version 3.1. Use \Doctrine\Common\Inflector\Inflector::classify() instead.
      */
     public static function camelize($property)
     {
-        return Container::camelize($property);
+        @trigger_error(
+            sprintf(
+                'The %s method is deprecated since 3.1 and will be removed in 4.0. '.
+                'Use \Doctrine\Common\Inflector\Inflector::classify() instead.',
+                __METHOD__
+            ),
+            E_USER_DEPRECATED
+        );
+
+        return Inflector::classify($property);
     }
 
     /**

+ 4 - 0
CHANGELOG.md

@@ -7,6 +7,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
 
 ### Changed
 - Updated AdminLTE theme to version 2.3.3
+ 
+### Deprecated
+- Deprecated BaseFieldDescription::camelize()
+- Deprecated AdminHelper::camelize()
 
 ## [3.0.0](https://github.com/sonata-project/SonataAdminBundle/compare/2.3.10...3.0.0) - 2016-05-08
 ### Added

+ 4 - 4
Controller/CRUDController.php

@@ -11,10 +11,10 @@
 
 namespace Sonata\AdminBundle\Controller;
 
+use Doctrine\Common\Inflector\Inflector;
 use Psr\Log\LoggerInterface;
 use Psr\Log\NullLogger;
 use Sonata\AdminBundle\Admin\AdminInterface;
-use Sonata\AdminBundle\Admin\BaseFieldDescription;
 use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
 use Sonata\AdminBundle\Exception\LockException;
 use Sonata\AdminBundle\Exception\ModelManagerException;
@@ -553,8 +553,8 @@ class CRUDController extends Controller
             throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
         }
 
-        $camelizedAction = BaseFieldDescription::camelize($action);
-        $isRelevantAction = sprintf('batchAction%sIsRelevant', ucfirst($camelizedAction));
+        $camelizedAction = Inflector::classify($action);
+        $isRelevantAction = sprintf('batchAction%sIsRelevant', $camelizedAction);
 
         if (method_exists($this, $isRelevantAction)) {
             $nonRelevantMessage = call_user_func(array($this, $isRelevantAction), $idx, $allElements);
@@ -601,7 +601,7 @@ class CRUDController extends Controller
         }
 
         // execute the action, batchActionXxxxx
-        $finalAction = sprintf('batchAction%s', ucfirst($camelizedAction));
+        $finalAction = sprintf('batchAction%s', $camelizedAction);
         if (!is_callable(array($this, $finalAction))) {
             throw new \RuntimeException(sprintf('A `%s::%s` method must be callable', get_class($this), $finalAction));
         }

+ 3 - 3
DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

@@ -11,7 +11,7 @@
 
 namespace Sonata\AdminBundle\DependencyInjection\Compiler;
 
-use Sonata\AdminBundle\Admin\BaseFieldDescription;
+use Doctrine\Common\Inflector\Inflector;
 use Sonata\AdminBundle\Datagrid\Pager;
 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -221,7 +221,7 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
         );
 
         foreach ($keys as $key) {
-            $method = 'set'.BaseFieldDescription::camelize($key);
+            $method = 'set'.Inflector::classify($key);
             if (!isset($attributes[$key]) || $definition->hasMethodCall($method)) {
                 continue;
             }
@@ -274,7 +274,7 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
         $definition->addMethodCall('setManagerType', array($manager_type));
 
         foreach ($defaultAddServices as $attr => $addServiceId) {
-            $method = 'set'.BaseFieldDescription::camelize($attr);
+            $method = 'set'.Inflector::classify($attr);
 
             if (isset($overwriteAdminConfiguration[$attr]) || !$definition->hasMethodCall($method)) {
                 $definition->addMethodCall($method, array(new Reference(isset($overwriteAdminConfiguration[$attr]) ? $overwriteAdminConfiguration[$attr] : $addServiceId)));

+ 3 - 0
Tests/Admin/BaseFieldDescriptionTest.php

@@ -194,6 +194,9 @@ class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
         $this->assertSame('ExtensionDomain', $description->getTranslationDomain());
     }
 
+    /**
+     * @group legacy
+     */
     public function testCamelize()
     {
         $this->assertSame('FooBar', BaseFieldDescription::camelize('foo_bar'));

+ 4 - 0
UPGRADE-3.0.md

@@ -5,6 +5,10 @@ UPGRADE FROM 2.x to 3.0
 
 You will need to follow the dependencies upgrade instructions.
 
+## Deprecated BaseFieldDescription::camelize() and AdminHelper::camelize()
+
+If you were using this method, please use `Doctrine\Common\Inflector\Inflector::classify()` instead.
+
 ## Datagrid builders
 
 If you have implemented a custom datagrid builder, you must adapt the signature of its `addFilter` method to match the one in `DatagridBuilderInterface` again.