Browse Source

Backport changes done to Type class in 4.x.

Refs #11975, #11979 and #11994.
ADmad 8 years ago
parent
commit
ea567d9cad

+ 1 - 1
src/Database/Schema/TableSchema.php

@@ -440,7 +440,7 @@ class TableSchema implements TableSchemaInterface, SqlGeneratorInterface
             return null;
         }
 
-        if (Type::map($type)) {
+        if (Type::getMap($type)) {
             $type = Type::build($type)->getBaseType();
         }
 

+ 57 - 2
src/Database/Type.php

@@ -145,8 +145,12 @@ class Type implements TypeInterface
      * If called with no arguments it will return current types map array
      * If $className is omitted it will return mapped class for $type
      *
-     * Deprecated: The usage of $type as \Cake\Database\Type[] is deprecated. Please always use string[] if you pass an array
-     * as first argument.
+     * Deprecated 3.6.2:
+     * - The usage of $type as string[]|\Cake\Database\Type[] is deprecated.
+     *   Use Type::setMap() with string[] instead.
+     * - Passing $className as \Cake\Database\Type instance is deprecated, use
+     *   class name string only.
+     * - Using this method as getter is deprecated. Use Type::getMap() instead.
      *
      * @param string|string[]|\Cake\Database\Type[]|null $type If string name of type to map, if array list of arrays to be mapped
      * @param string|\Cake\Database\Type|null $className The classname or object instance of it to register.
@@ -156,22 +160,73 @@ class Type implements TypeInterface
     public static function map($type = null, $className = null)
     {
         if ($type === null) {
+            deprecationWarning(
+                'Using `Type::map()` as getter is deprecated. ' .
+                'Use `Type::getMap()` instead.'
+            );
+
             return static::$_types;
         }
         if (is_array($type)) {
+            deprecationWarning(
+                'Using `Type::map()` to set complete types map is deprecated. ' .
+                'Use `Type::setMap()` instead.'
+            );
+
             static::$_types = $type;
 
             return null;
         }
         if ($className === null) {
+            deprecationWarning(
+                'Using `Type::map()` as getter is deprecated. ' .
+                'Use `Type::getMap()` instead.'
+            );
+
             return isset(static::$_types[$type]) ? static::$_types[$type] : null;
         }
 
+        if (!is_string($className)) {
+            deprecationWarning(
+                'Passing $className as object to Type::map() is deprecated. ' .
+                'Use Type::set() instead.'
+            );
+        }
+
         static::$_types[$type] = $className;
         unset(static::$_builtTypes[$type]);
     }
 
     /**
+     * Set type to classname mapping.
+     *
+     * @param string[] $map List of types to be mapped.
+     * @return void
+     * @since 3.6.2
+     */
+    public static function setMap(array $map)
+    {
+        static::$_types = $map;
+        static::$_builtTypes = [];
+    }
+
+    /**
+     * Get mapped class name or instance for type(s).
+     *
+     * @param string|null $type Type name to get mapped class for or null to get map array.
+     * @return array|string|\Cake\Database\TypeInterface|null Configured class name or instance for give $type or map array.
+     * @since 3.6.2
+     */
+    public static function getMap($type = null)
+    {
+        if ($type === null) {
+            return static::$_types;
+        }
+
+        return isset(static::$_types[$type]) ? static::$_types[$type] : null;
+    }
+
+    /**
      * Clears out all created instances and mapped types classes, useful for testing
      *
      * @return void

+ 1 - 1
tests/TestCase/Database/ExpressionTypeCastingTest.php

@@ -49,7 +49,7 @@ class ExpressionTypeCastingTest extends TestCase
     public function setUp()
     {
         parent::setUp();
-        Type::map('test', new TestType);
+        Type::set('test', new TestType);
     }
 
     /**

+ 2 - 2
tests/TestCase/Database/Schema/TableSchemaTest.php

@@ -49,7 +49,7 @@ class TableTest extends TestCase
 
     public function setUp()
     {
-        $this->_map = Type::map();
+        $this->_map = Type::getMap();
         parent::setUp();
     }
 
@@ -57,7 +57,7 @@ class TableTest extends TestCase
     {
         $this->getTableLocator()->clear();
         Type::clear();
-        Type::map($this->_map);
+        Type::setMap($this->_map);
         parent::tearDown();
     }
 

+ 43 - 18
tests/TestCase/Database/TypeTest.php

@@ -15,8 +15,6 @@
 namespace Cake\Test\TestCase\Database;
 
 use Cake\Database\Type;
-use Cake\Database\Type\BoolType;
-use Cake\Database\Type\IntegerType;
 use Cake\Database\Type\UuidType;
 use Cake\TestSuite\TestCase;
 use PDO;
@@ -43,7 +41,7 @@ class TypeTest extends TestCase
      */
     public function setUp()
     {
-        $this->_originalMap = Type::map();
+        $this->_originalMap = Type::getMap();
         parent::setUp();
     }
 
@@ -56,7 +54,7 @@ class TypeTest extends TestCase
     {
         parent::tearDown();
 
-        Type::map($this->_originalMap);
+        Type::setMap($this->_originalMap);
     }
 
     /**
@@ -120,15 +118,19 @@ class TypeTest extends TestCase
      */
     public function testMapAndBuild()
     {
-        $map = Type::map();
-        $this->assertNotEmpty($map);
-        $this->assertArrayNotHasKey('foo', $map);
+        $this->deprecated(function () {
+            $map = Type::map();
+            $this->assertNotEmpty($map);
+            $this->assertArrayNotHasKey('foo', $map);
+        });
 
         $fooType = FooType::class;
         Type::map('foo', $fooType);
-        $map = Type::map();
+        $map = Type::getMap();
         $this->assertEquals($fooType, $map['foo']);
-        $this->assertEquals($fooType, Type::map('foo'));
+        $this->deprecated(function () use ($fooType) {
+            $this->assertEquals($fooType, Type::map('foo'));
+        });
 
         $type = Type::build('foo');
         $this->assertInstanceOf($fooType, $type);
@@ -136,9 +138,9 @@ class TypeTest extends TestCase
         $this->assertEquals('text', $type->getBaseType());
 
         Type::map('foo2', $fooType);
-        $map = Type::map();
+        $map = Type::getMap();
         $this->assertSame($fooType, $map['foo2']);
-        $this->assertSame($fooType, Type::map('foo2'));
+        $this->assertSame($fooType, Type::getMap('foo2'));
 
         $type = Type::build('foo2');
         $this->assertInstanceOf($fooType, $type);
@@ -169,14 +171,37 @@ class TypeTest extends TestCase
      */
     public function testMapAndBuildWithObjects()
     {
-        $map = Type::map();
+        $map = Type::getMap();
         Type::clear();
 
         $uuidType = new UuidType('uuid');
-        Type::map('uuid', $uuidType);
+        $this->deprecated(function () use ($uuidType) {
+            Type::map('uuid', $uuidType);
+        });
 
         $this->assertSame($uuidType, Type::build('uuid'));
-        Type::map($map);
+        Type::setMap($map);
+    }
+
+    /**
+     * testGetMapAndSetMap
+     *
+     * @return void
+     */
+    public function testGetMapAndSetMap()
+    {
+        $map = Type::getMap();
+        $this->assertNotEmpty($map);
+        $this->assertArrayNotHasKey('foo', $map);
+
+        $expected = [
+            'foo' => 'bar',
+            'ping' => 'pong',
+        ];
+        Type::setMap($expected);
+
+        $this->assertEquals($expected, Type::getMap());
+        $this->assertEquals('bar', Type::getMap('foo'));
     }
 
     /**
@@ -186,15 +211,15 @@ class TypeTest extends TestCase
      */
     public function testClear()
     {
-        $map = Type::map();
+        $map = Type::getMap();
         $this->assertNotEmpty($map);
 
         $type = Type::build('float');
         Type::clear();
 
-        $this->assertEmpty(Type::map());
-        Type::map($map);
-        $newMap = Type::map();
+        $this->assertEmpty(Type::getMap());
+        Type::setMap($map);
+        $newMap = Type::getMap();
 
         $this->assertEquals(array_keys($map), array_keys($newMap));
         $this->assertEquals($map['integer'], $newMap['integer']);