From 20b091217ffe0930f143720e6c43bc3e714bb8d2 Mon Sep 17 00:00:00 2001 From: W0rma Date: Mon, 29 Apr 2024 14:35:45 +0200 Subject: [PATCH] Fix deprecation layer --- src/ORMException.php | 96 ++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/src/ORMException.php b/src/ORMException.php index eb5aa6c7b40..a79871357eb 100644 --- a/src/ORMException.php +++ b/src/ORMException.php @@ -5,13 +5,31 @@ namespace Doctrine\ORM; use Doctrine\Common\Cache\Cache as CacheDriver; -use Doctrine\Persistence\ObjectRepository; +use Doctrine\ORM\Cache\Exception\InvalidResultCacheDriver; +use Doctrine\ORM\Cache\Exception\MetadataCacheNotConfigured; +use Doctrine\ORM\Cache\Exception\MetadataCacheUsesNonPersistentCache; +use Doctrine\ORM\Cache\Exception\QueryCacheNotConfigured; +use Doctrine\ORM\Cache\Exception\QueryCacheUsesNonPersistentCache; +use Doctrine\ORM\Exception\EntityManagerClosed; +use Doctrine\ORM\Exception\InvalidEntityRepository; +use Doctrine\ORM\Exception\InvalidHydrationMode; +use Doctrine\ORM\Exception\MismatchedEventManager; +use Doctrine\ORM\Exception\MissingIdentifierField; +use Doctrine\ORM\Exception\MissingMappingDriverImplementation; +use Doctrine\ORM\Exception\NamedNativeQueryNotFound; +use Doctrine\ORM\Exception\NamedQueryNotFound; +use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating; +use Doctrine\ORM\Exception\UnexpectedAssociationValue; +use Doctrine\ORM\Exception\UnknownEntityNamespace; +use Doctrine\ORM\Exception\UnrecognizedIdentifierFields; +use Doctrine\ORM\Persisters\Exception\CantUseInOperatorOnCompositeKeys; +use Doctrine\ORM\Persisters\Exception\InvalidOrientation; +use Doctrine\ORM\Persisters\Exception\UnrecognizedField; +use Doctrine\ORM\Repository\Exception\InvalidFindByCall; +use Doctrine\ORM\Repository\Exception\InvalidMagicMethodCall; +use Doctrine\ORM\Tools\Exception\NotSupported; use Exception; -use function get_debug_type; -use function implode; -use function sprintf; - /** * Base exception class for all ORM exceptions. * @@ -26,8 +44,7 @@ class ORMException extends Exception */ public static function missingMappingDriverImpl() { - return new self("It's a requirement to specify a Metadata Driver and pass it " . - 'to Doctrine\\ORM\\Configuration::setMetadataDriverImpl().'); + return MissingMappingDriverImplementation::create(); } /** @@ -39,11 +56,11 @@ public static function missingMappingDriverImpl() */ public static function namedQueryNotFound($queryName) { - return new self('Could not find a named query by the name "' . $queryName . '"'); + return NamedQueryNotFound::fromName($queryName); } /** - * @deprecated Use Doctrine\ORM\Exception\NamedQueryNotFound + * @deprecated Use Doctrine\ORM\Exception\NamedNativeQueryNotFound * * @param string $nativeQueryName * @@ -51,7 +68,7 @@ public static function namedQueryNotFound($queryName) */ public static function namedNativeQueryNotFound($nativeQueryName) { - return new self('Could not find a named native query by the name "' . $nativeQueryName . '"'); + return NamedNativeQueryNotFound::fromName($nativeQueryName); } /** @@ -63,7 +80,7 @@ public static function namedNativeQueryNotFound($nativeQueryName) */ public static function unrecognizedField($field) { - return new self(sprintf('Unrecognized field: %s', $field)); + return new UnrecognizedField(sprintf('Unrecognized field: %s', $field)); } /** @@ -78,7 +95,7 @@ public static function unrecognizedField($field) */ public static function unexpectedAssociationValue($class, $association, $given, $expected) { - return new self(sprintf('Found entity of type %s on association %s#%s, but expecting %s', $given, $class, $association, $expected)); + return UnexpectedAssociationValue::create($class, $association, $given, $expected); } /** @@ -91,7 +108,7 @@ public static function unexpectedAssociationValue($class, $association, $given, */ public static function invalidOrientation($className, $field) { - return new self('Invalid order by orientation specified for ' . $className . '#' . $field); + return InvalidOrientation::fromClassNameAndField($className, $field); } /** @@ -101,7 +118,7 @@ public static function invalidOrientation($className, $field) */ public static function entityManagerClosed() { - return new self('The EntityManager is closed.'); + return EntityManagerClosed::create(); } /** @@ -113,7 +130,7 @@ public static function entityManagerClosed() */ public static function invalidHydrationMode($mode) { - return new self(sprintf("'%s' is an invalid hydration mode.", $mode)); + return InvalidHydrationMode::fromMode($mode); } /** @@ -123,7 +140,7 @@ public static function invalidHydrationMode($mode) */ public static function mismatchedEventManager() { - return new self('Cannot use different EventManager instances for EntityManager and Connection.'); + return MismatchedEventManager::create(); } /** @@ -135,11 +152,11 @@ public static function mismatchedEventManager() */ public static function findByRequiresParameter($methodName) { - return new self("You need to pass a parameter to '" . $methodName . "'"); + return InvalidMagicMethodCall::onMissingParameter($methodName); } /** - * @deprecated Doctrine\ORM\Repository\Exception\InvalidFindByCall + * @deprecated Doctrine\ORM\Repository\Exception\InvalidMagicMethodCall::becauseFieldNotFoundIn() * * @param string $entityName * @param string $fieldName @@ -149,10 +166,7 @@ public static function findByRequiresParameter($methodName) */ public static function invalidMagicCall($entityName, $fieldName, $method) { - return new self( - "Entity '" . $entityName . "' has no field '" . $fieldName . "'. " . - "You can therefore not call '" . $method . "' on the entities' repository" - ); + return InvalidMagicMethodCall::becauseFieldNotFoundIn($entityName, $fieldName, $method); } /** @@ -165,10 +179,7 @@ public static function invalidMagicCall($entityName, $fieldName, $method) */ public static function invalidFindByInverseAssociation($entityName, $associationFieldName) { - return new self( - "You cannot search for the association field '" . $entityName . '#' . $associationFieldName . "', " . - 'because it is the inverse side of an association. Find methods only work on owning side associations.' - ); + return InvalidFindByCall::fromInverseSideUsage($entityName, $associationFieldName); } /** @@ -178,7 +189,7 @@ public static function invalidFindByInverseAssociation($entityName, $association */ public static function invalidResultCacheDriver() { - return new self('Invalid result cache driver; it must implement Doctrine\\Common\\Cache\\Cache.'); + return InvalidResultCacheDriver::create(); } /** @@ -188,7 +199,7 @@ public static function invalidResultCacheDriver() */ public static function notSupported() { - return new self('This behaviour is (currently) not supported by Doctrine 2'); + return NotSupported::create(); } /** @@ -198,7 +209,7 @@ public static function notSupported() */ public static function queryCacheNotConfigured() { - return new self('Query Cache is not configured.'); + return QueryCacheNotConfigured::create(); } /** @@ -208,7 +219,7 @@ public static function queryCacheNotConfigured() */ public static function metadataCacheNotConfigured() { - return new self('Class Metadata Cache is not configured.'); + return MetadataCacheNotConfigured::create(); } /** @@ -218,7 +229,7 @@ public static function metadataCacheNotConfigured() */ public static function queryCacheUsesNonPersistentCache(CacheDriver $cache) { - return new self('Query Cache uses a non-persistent cache driver, ' . get_debug_type($cache) . '.'); + return QueryCacheUsesNonPersistentCache::fromDriver($cache); } /** @@ -228,7 +239,7 @@ public static function queryCacheUsesNonPersistentCache(CacheDriver $cache) */ public static function metadataCacheUsesNonPersistentCache(CacheDriver $cache) { - return new self('Metadata Cache uses a non-persistent cache driver, ' . get_debug_type($cache) . '.'); + return MetadataCacheUsesNonPersistentCache::fromDriver($cache); } /** @@ -238,7 +249,7 @@ public static function metadataCacheUsesNonPersistentCache(CacheDriver $cache) */ public static function proxyClassesAlwaysRegenerating() { - return new self('Proxy Classes are always regenerating.'); + return ProxyClassesAlwaysRegenerating::create(); } /** @@ -250,9 +261,7 @@ public static function proxyClassesAlwaysRegenerating() */ public static function unknownEntityNamespace($entityNamespaceAlias) { - return new self( - sprintf("Unknown Entity namespace alias '%s'.", $entityNamespaceAlias) - ); + return UnknownEntityNamespace::fromNamespaceAlias($entityNamespaceAlias); } /** @@ -264,11 +273,7 @@ public static function unknownEntityNamespace($entityNamespaceAlias) */ public static function invalidEntityRepository($className) { - return new self(sprintf( - "Invalid repository class '%s'. It must be a %s.", - $className, - ObjectRepository::class - )); + return InvalidEntityRepository::fromClassName($className); } /** @@ -281,7 +286,7 @@ public static function invalidEntityRepository($className) */ public static function missingIdentifierField($className, $fieldName) { - return new self(sprintf('The identifier %s is missing for a query of %s', $fieldName, $className)); + return MissingIdentifierField::fromFieldAndClass($fieldName, $className); } /** @@ -294,10 +299,7 @@ public static function missingIdentifierField($className, $fieldName) */ public static function unrecognizedIdentifierFields($className, $fieldNames) { - return new self( - "Unrecognized identifier fields: '" . implode("', '", $fieldNames) . "' " . - "are not present on class '" . $className . "'." - ); + return UnrecognizedIdentifierFields::fromClassAndFieldNames($className, $fieldNames); } /** @@ -307,6 +309,6 @@ public static function unrecognizedIdentifierFields($className, $fieldNames) */ public static function cantUseInOperatorOnCompositeKeys() { - return new self("Can't use IN operator on entities that have composite keys."); + return CantUseInOperatorOnCompositeKeys::create(); } }