-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract type factory and registry from Type into TypeRegistry #3354
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Types; | ||
|
||
use Doctrine\DBAL\DBALException; | ||
use function array_search; | ||
use function in_array; | ||
|
||
/** | ||
* The type registry is responsible for holding a map of all known DBAL types. | ||
* The types are stored using the flyweight pattern so that one type only exists as exactly one instance. | ||
* | ||
* @internal TypeRegistry exists for forward compatibility, its API should not be considered stable. | ||
*/ | ||
final class TypeRegistry | ||
{ | ||
/** @var array<string, Type> Map of type names and their corresponding flyweight objects. */ | ||
private $instances = []; | ||
|
||
/** | ||
* Finds a type by the given name. | ||
* | ||
* @throws DBALException | ||
*/ | ||
public function get(string $name) : Type | ||
{ | ||
if (! isset($this->instances[$name])) { | ||
throw DBALException::unknownColumnType($name); | ||
} | ||
|
||
return $this->instances[$name]; | ||
} | ||
|
||
/** | ||
* Finds a name for the given type. | ||
* | ||
* @throws DBALException | ||
*/ | ||
public function lookupName(Type $type) : string | ||
{ | ||
$name = $this->findTypeName($type); | ||
|
||
if ($name === null) { | ||
throw DBALException::typeNotRegistered($type); | ||
} | ||
|
||
return $name; | ||
} | ||
|
||
/** | ||
* Checks if there is a type of the given name. | ||
*/ | ||
public function has(string $name) : bool | ||
{ | ||
return isset($this->instances[$name]); | ||
} | ||
|
||
/** | ||
* Registers a custom type to the type map. | ||
* | ||
* @throws DBALException | ||
*/ | ||
public function register(string $name, Type $type) : void | ||
{ | ||
if (isset($this->instances[$name])) { | ||
throw DBALException::typeExists($name); | ||
} | ||
|
||
if ($this->findTypeName($type) !== null) { | ||
throw DBALException::typeAlreadyRegistered($type); | ||
} | ||
|
||
$this->instances[$name] = $type; | ||
} | ||
|
||
/** | ||
* Overrides an already defined type to use a different implementation. | ||
* | ||
* @throws DBALException | ||
*/ | ||
public function override(string $name, Type $type) : void | ||
{ | ||
if (! isset($this->instances[$name])) { | ||
throw DBALException::typeNotFound($name); | ||
} | ||
|
||
if (! in_array($this->findTypeName($type), [$name, null], true)) { | ||
Majkl578 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw DBALException::typeAlreadyRegistered($type); | ||
} | ||
|
||
$this->instances[$name] = $type; | ||
} | ||
|
||
/** | ||
* Gets the map of all registered types and their corresponding type instances. | ||
* | ||
* @internal | ||
* | ||
* @return array<string, Type> | ||
*/ | ||
public function getMap() : array | ||
{ | ||
return $this->instances; | ||
} | ||
|
||
private function findTypeName(Type $type) : ?string | ||
{ | ||
$name = array_search($type, $this->instances, true); | ||
|
||
if ($name === false) { | ||
return null; | ||
} | ||
|
||
return $name; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On one hand, not a big fan of making a big exception class that grows with time, on the other hand, if these exceptions are not meant to be caught but to help the user figure out their issue, maybe this is fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only for 2.x.