Skip to content

Commit

Permalink
Evaluate TypeRegistry as a runtime instance
Browse files Browse the repository at this point in the history
3 big changes seem to be required here:
- injecting the type registry in the Connection
- injecting the type registry in the Platform
- Dealing with Table::addColumn

Possible steps for the 2 first items:
- have a type registry in the configuration
- use it when instantiating the driver in DriverManager
- the driver can then instantiate the platform with the type registry
- the driver can provide the type registry to Connection
  • Loading branch information
greg0ire committed Nov 27, 2021
1 parent bb13b5f commit 72de5da
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,8 @@ public function isRollbackOnly()
*/
public function convertToDatabaseValue($value, $type)
{
// Either we deprecate this method, or we add the type registry as a
// dependency
return Type::getType($type)->convertToDatabaseValue($value, $this->getDatabasePlatform());
}

Expand All @@ -1612,6 +1614,7 @@ public function convertToDatabaseValue($value, $type)
*/
public function convertToPHPValue($value, $type)
{
// same as above
return Type::getType($type)->convertToPHPValue($value, $this->getDatabasePlatform());
}

Expand Down Expand Up @@ -1669,6 +1672,7 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t
private function getBindingInfo($value, $type): array
{
if (is_string($type)) {
// same as above
$type = Type::getType($type);
}

Expand Down
1 change: 1 addition & 0 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private function initializeAllDoctrineTypeMappings(): void
{
$this->initializeDoctrineTypeMappings();

// the platform will also need to know about the type registry
foreach (Type::getTypesMap() as $typeName => $className) {
foreach (Type::getType($typeName)->getMappedDatabaseTypes($this) as $dbType) {
$this->doctrineTypeMapping[$dbType] = $typeName;
Expand Down
2 changes: 2 additions & 0 deletions src/Schema/DB2SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ protected function _getPortableTableColumnDefinition($tableColumn)
$options['precision'] = $precision;
}

// most schema managers perform that kind of call. They know about both
// the connection and the platform
return new Column($tableColumn['colname'], Type::getType($type), $options);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ private function _createIndex(
*/
public function addColumn($name, $typeName, array $options = [])
{
// either the signature of addColumn should require a Type instance, or
// we should pass a string to Column::__construct and resolve the type later on
$column = new Column($name, Type::getType($typeName), $options);

$this->_addColumn($column);
Expand Down
1 change: 1 addition & 0 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING)

if ($type !== null) {
if (is_string($type)) {
// The type registry could be obtained from the connection
$type = Type::getType($type);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static function getType($name)
*
* @throws Exception
*/
public static function addType($name, $className)
public static function addType($name, $className) // this method is only ever used in tests
{
self::getTypeRegistry()->register($name, new $className());
}
Expand All @@ -165,7 +165,7 @@ public static function addType($name, $className)
*
* @return bool TRUE if type is supported; FALSE otherwise.
*/
public static function hasType($name)
public static function hasType($name) // used once in AbstractPlatform
{
return self::getTypeRegistry()->has($name);
}
Expand All @@ -180,7 +180,7 @@ public static function hasType($name)
*
* @throws Exception
*/
public static function overrideType($name, $className)
public static function overrideType($name, $className) // used exclusively in tests
{
self::getTypeRegistry()->override($name, new $className());
}
Expand All @@ -204,7 +204,7 @@ public function getBindingType()
*
* @return string[]
*/
public static function getTypesMap()
public static function getTypesMap() // used in AbstractPlatform
{
return array_map(
static function (Type $type): string {
Expand Down

0 comments on commit 72de5da

Please sign in to comment.