Skip to content

Commit

Permalink
Add DebugInfo and small performance cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 2, 2017
1 parent 1785901 commit 5a5d2b7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Database/Type.php
Expand Up @@ -106,7 +106,8 @@ public static function build($name)
throw new InvalidArgumentException(sprintf('Unknown type "%s"', $name));
}
if (is_string(static::$_types[$name])) {
return static::$_builtTypes[$name] = new static::$_types[$name]($name);
static::$_types[$name] = new static::$_types[$name]($name);
return static::$_builtTypes[$name] = static::$_types[$name];
}

return static::$_builtTypes[$name] = static::$_types[$name];
Expand Down Expand Up @@ -144,9 +145,9 @@ public static function set($name, Type $instance)
* If called with no arguments it will return current types map array
* If $className is omitted it will return mapped class for $type
*
* @param string|array|\Cake\Database\Type|null $type if string name of type to map, if array list of arrays to be mapped
* @param string|null $className The classname to register.
* @return array|string|null if $type is null then array with current map, if $className is null string
* @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.
* @return array|string|null If $type is null then array with current map, if $className is null string
* configured class name for give $type, null otherwise
*/
public static function map($type = null, $className = null)
Expand Down Expand Up @@ -300,4 +301,19 @@ public function marshal($value)
{
return $this->_basicTypeCast($value);
}

/**
* Returns an array that can be used to describe the internal state of this
* object.
*
* @return array
*/
public function __debugInfo()
{
return [
'name' => $this->_name,
'types' => static::$_types,
'builtTypes' => static::$_builtTypes,
];
}
}
32 changes: 32 additions & 0 deletions tests/TestCase/Database/TypeTest.php
Expand Up @@ -15,6 +15,9 @@
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;
use TestApp\Database\Type\BarType;
Expand Down Expand Up @@ -251,4 +254,33 @@ public function testSet()
Type::set('random', $instance);
$this->assertSame($instance, Type::build('random'));
}

/**
* @return void
*/
public function testDebugInfo()
{
$type = new Type('foo');
Type::clear();
Type::map('bool', BoolType::class);
Type::map('int', IntegerType::class);
$uuidType = new UuidType('uuid');
Type::map('uuid', $uuidType);
Type::build('bool');

$result = $type->__debugInfo();
$boolType = new BoolType('bool');
$expected = [
'name' => 'foo',
'types' => [
'bool' => $boolType,
'int' => IntegerType::class,
'uuid' => $uuidType,
],
'builtTypes' => [
'bool' => $boolType,
],
];
$this->assertEquals($expected, $result);
}
}

0 comments on commit 5a5d2b7

Please sign in to comment.