From 22c2e4f1d46c5c76879855f67517204412da0c81 Mon Sep 17 00:00:00 2001 From: fcnybok Date: Tue, 7 May 2024 19:05:08 +0300 Subject: [PATCH] feat: Support doctrine 3. --- config/di.php | 1 + config/example.php | 7 ++ src/Cache/CacheFactory.php | 7 +- src/Dbal/Factory/ConfigurationFactory.php | 12 +- src/Dbal/Factory/ConnectionFactory.php | 16 +-- src/Dbal/Factory/DynamicConnectionFactory.php | 15 ++- src/Dbal/Model/ConnectionModel.php | 27 ----- src/DoctrineManager.php | 52 +++----- src/EventManager/EventManagerFactory.php | 11 +- src/Factory/DoctrineManagerFactory.php | 14 ++- src/Orm/Factory/ConfigurationFactory.php | 112 ++++++++++++++---- .../Factory/DynamicEntityManagerFactory.php | 25 +++- src/Orm/Factory/EntityManagerFactory.php | 38 +++--- 13 files changed, 186 insertions(+), 151 deletions(-) delete mode 100644 src/Dbal/Model/ConnectionModel.php diff --git a/config/di.php b/config/di.php index 1e3f8b5..73b846e 100644 --- a/config/di.php +++ b/config/di.php @@ -13,6 +13,7 @@ DoctrineManagerFactory $doctrineManagerFactory ): DoctrineManager => $doctrineManagerFactory->create($params['yiisoft/yii-doctrine'] ?? []), 'reset' => function (): void { + /** @var DoctrineManager $this */ $this->resetAllManager(); }, ], diff --git a/config/example.php b/config/example.php index b6d5357..b47afa9 100644 --- a/config/example.php +++ b/config/example.php @@ -4,6 +4,8 @@ use Doctrine\DBAL\Schema\Sequence; use Doctrine\ORM\Events; +use Doctrine\ORM\Query; +use Doctrine\ORM\Repository\DefaultRepositoryFactory; use Yiisoft\Yii\Doctrine\DoctrineManager; use Yiisoft\Yii\Doctrine\Orm\Enum\DriverMappingEnum; @@ -61,6 +63,11 @@ // 'schema_ignore_classes' => [], // 'entity_listener_resolver' => DefaultEntityListenerResolver::class, // 'typed_field_mapper' => DefaultTypedFieldMapper::class, +// 'fetch_mode_sub_select_batch_size' => 100, +// 'repository_factory' => DefaultRepositoryFactory::class, +// 'default_query_hints' => [ +// Query::HINT_CUSTOM_OUTPUT_WALKER => Query\SqlWalker::class, +// ], 'mappings' => [ 'User' => [ 'dir' => '@src/User/Entity', diff --git a/src/Cache/CacheFactory.php b/src/Cache/CacheFactory.php index c3062b1..4fda322 100644 --- a/src/Cache/CacheFactory.php +++ b/src/Cache/CacheFactory.php @@ -24,7 +24,12 @@ final class CacheFactory private const CACHE_NAMESPACE = 'doctrine_'; /** - * @psalm-param array{namespace: string|null, driver: string|null, path: string, server: array{host: string, port: int}|null} $cacheConfig + * @psalm-param array{ + * namespace: string|null, + * driver: string|null, + * path: string, + * server: array{host: string, port: int}|null + * } $cacheConfig * * @throws CacheException */ diff --git a/src/Dbal/Factory/ConfigurationFactory.php b/src/Dbal/Factory/ConfigurationFactory.php index cd462ff..9df5612 100644 --- a/src/Dbal/Factory/ConfigurationFactory.php +++ b/src/Dbal/Factory/ConfigurationFactory.php @@ -49,15 +49,19 @@ function (string $classMiddleware): Middleware { private function configureAutoCommit(Configuration $configuration, ?bool $autoCommit): void { - if (null !== $autoCommit) { - $configuration->setAutoCommit($autoCommit); + if (null === $autoCommit) { + return; } + + $configuration->setAutoCommit($autoCommit); } private function configureSchemaAssetsFilter(Configuration $configuration, ?callable $schemaAssetsFilter): void { - if (null !== $schemaAssetsFilter) { - $configuration->setSchemaAssetsFilter($schemaAssetsFilter); + if (null === $schemaAssetsFilter) { + return; } + + $configuration->setSchemaAssetsFilter($schemaAssetsFilter); } } diff --git a/src/Dbal/Factory/ConnectionFactory.php b/src/Dbal/Factory/ConnectionFactory.php index c4dedc9..f1baf26 100644 --- a/src/Dbal/Factory/ConnectionFactory.php +++ b/src/Dbal/Factory/ConnectionFactory.php @@ -5,18 +5,16 @@ namespace Yiisoft\Yii\Doctrine\Dbal\Factory; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Driver\Middleware; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Exception; use Doctrine\DBAL\Types\Type; use InvalidArgumentException; -use Yiisoft\Yii\Doctrine\Dbal\Model\ConnectionModel; -use Yiisoft\Yii\Doctrine\EventManager\EventManagerFactory; final class ConnectionFactory { public function __construct( private readonly ConfigurationFactory $configurationFactory, - private readonly EventManagerFactory $eventManagerFactory, ) { } @@ -25,14 +23,14 @@ public function __construct( * auto_commit: bool|empty, * custom_types: array>|empty, * events: array|empty, - * middlewares: array>|empty, + * middlewares: array>|empty, * params: array, * schema_assets_filter: callable|empty * } $dbalConfig * * @throws Exception */ - public function create(array $dbalConfig): ConnectionModel + public function create(array $dbalConfig): Connection { if (!isset($dbalConfig['params'])) { throw new InvalidArgumentException('Not found "params" connection'); @@ -40,17 +38,15 @@ public function create(array $dbalConfig): ConnectionModel $configuration = $this->configurationFactory->create($dbalConfig); - $eventManager = $this->eventManagerFactory->createForDbal($dbalConfig['events'] ?? []); - - $connection = DriverManager::getConnection($dbalConfig['params'], $configuration, $eventManager); + $connection = DriverManager::getConnection($dbalConfig['params'], $configuration); $this->configureCustomTypes($connection, $dbalConfig['custom_types'] ?? []); - return new ConnectionModel($connection, $eventManager); + return $connection; } /** - * @psalm-param array>|empty $customTypes + * @psalm-param array>|empty $customTypes * * @throws Exception */ diff --git a/src/Dbal/Factory/DynamicConnectionFactory.php b/src/Dbal/Factory/DynamicConnectionFactory.php index b740e96..0512e38 100644 --- a/src/Dbal/Factory/DynamicConnectionFactory.php +++ b/src/Dbal/Factory/DynamicConnectionFactory.php @@ -4,8 +4,9 @@ namespace Yiisoft\Yii\Doctrine\Dbal\Factory; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Exception; use RuntimeException; -use Yiisoft\Yii\Doctrine\Dbal\Model\ConnectionModel; use Yiisoft\Yii\Doctrine\DoctrineManager; use function sprintf; @@ -27,20 +28,18 @@ public function __construct( * params: array, * schema_assets_filter: callable * } $dbalConfig + * @throws Exception */ - public function createConnection(array $dbalConfig, string $connectionName): ConnectionModel + public function createConnection(array $dbalConfig, string $connectionName): Connection { if ($this->doctrineManager->hasConnection($connectionName)) { throw new RuntimeException(sprintf('Connection "%s" already exist', $connectionName)); } - $connectionModel = $this->connectionFactory->create($dbalConfig); + $connection = $this->connectionFactory->create($dbalConfig); - $this->doctrineManager->addConnection( - $connectionName, - $connectionModel - ); + $this->doctrineManager->addConnection($connectionName, $connection); - return $connectionModel; + return $connection; } } diff --git a/src/Dbal/Model/ConnectionModel.php b/src/Dbal/Model/ConnectionModel.php deleted file mode 100644 index aca8eb4..0000000 --- a/src/Dbal/Model/ConnectionModel.php +++ /dev/null @@ -1,27 +0,0 @@ -connection; - } - - public function getEventManager(): EventManager - { - return $this->eventManager; - } -} diff --git a/src/DoctrineManager.php b/src/DoctrineManager.php index 4d8aa9a..1c50aeb 100644 --- a/src/DoctrineManager.php +++ b/src/DoctrineManager.php @@ -13,7 +13,6 @@ use Doctrine\Persistence\Proxy; use ReflectionClass; use RuntimeException; -use Yiisoft\Yii\Doctrine\Dbal\Model\ConnectionModel; use function array_keys; use function sprintf; @@ -27,8 +26,8 @@ final class DoctrineManager implements ManagerRegistry private string $proxyInterfaceName = Proxy::class; /** - * @param ConnectionModel[] $connections - * @param EntityManagerInterface[] $managers + * @psalm-param array> $connections + * @psalm-param array> $managers */ public function __construct( private array $connections, @@ -38,24 +37,24 @@ public function __construct( ) { } - public function addConnection(string $name, ConnectionModel $connectionModel): void + public function addConnection(string $name, Connection $connection): void { if (isset($this->connections[$name])) { throw new RuntimeException(sprintf('Connection by name "%s" already exists', $name)); } - $this->connections[$name] = $connectionModel; + $this->connections[$name] = $connection; } public function closeConnection(string $name): void { - $connectionModel = $this->connections[$name] ?? null; + $connection = $this->connections[$name] ?? null; - if (null === $connectionModel) { + if (null === $connection) { throw new RuntimeException(sprintf('Connection by name "%s" already is not exists', $name)); } - $connectionModel->getConnection()->close(); + $connection->close(); unset($this->connections[$name]); } @@ -66,13 +65,13 @@ public function getConnection($name = null): Connection $name = $this->getDefaultConnectionName(); } - $connectionModel = $this->connections[$name] ?? null; + $connection = $this->connections[$name] ?? null; - if (null === $connectionModel) { + if (null === $connection) { throw new RuntimeException(sprintf('Not found connection by name "%s"', $name)); } - return $connectionModel->getConnection(); + return $connection; } public function getDefaultConnectionName(): string @@ -99,41 +98,20 @@ public function hasManager(string $name): bool return isset($this->managers[$name]); } - public function getConnectionModel(string $name = null): ConnectionModel - { - if (null === $name) { - $name = $this->getDefaultConnectionName(); - } - - $connectionModel = $this->connections[$name] ?? null; - - if (null === $connectionModel) { - throw new RuntimeException(sprintf('Not found connection by name "%s"', $name)); - } - - return $connectionModel; - } - public function getConnections(): array { - $result = []; - - foreach ($this->connections as $name => $connectionModel) { - $result[$name] = $connectionModel->getConnection(); - } - - return $result; + return $this->connections; } /** * @psalm-return list */ - public function getConnectionNames() + public function getConnectionNames(): array { return array_keys($this->connections); } - public function resetManager(?string $name = null) + public function resetManager(?string $name = null): ObjectManager { if (null === $name) { $name = $this->getDefaultManagerName(); @@ -200,7 +178,7 @@ public function flushAllManager(): void /** * @psalm-return list */ - public function getManagerNames() + public function getManagerNames(): array { return array_keys($this->managers); } @@ -272,7 +250,7 @@ public function getManagerForClass($class): ?EntityManagerInterface } /** - * @psalm-return array + * @psalm-return array */ public function getManagers(): array { diff --git a/src/EventManager/EventManagerFactory.php b/src/EventManager/EventManagerFactory.php index e879665..84d544f 100644 --- a/src/EventManager/EventManagerFactory.php +++ b/src/EventManager/EventManagerFactory.php @@ -18,14 +18,12 @@ public function __construct( ) { } - public function createForDbal(array $eventConfig): EventManager + public function create(array $eventConfig): EventManager { $eventManager = new EventManager(); - // listener $this->configureListener($eventManager, $eventConfig['listeners'] ?? []); - // subscribers $this->configureSubscribers($eventManager, $eventConfig['subscribers'] ?? []); return $eventManager; @@ -52,11 +50,4 @@ private function configureSubscribers(EventManager $eventManager, array $subscri $eventManager->addEventSubscriber($subscriber); } } - - public function createForOrm(EventManager $eventManager, array $eventConfig): void - { - $this->configureListener($eventManager, $eventConfig['listeners'] ?? []); - - $this->configureSubscribers($eventManager, $eventConfig['subscribers'] ?? []); - } } diff --git a/src/Factory/DoctrineManagerFactory.php b/src/Factory/DoctrineManagerFactory.php index 49e6afb..886cba8 100644 --- a/src/Factory/DoctrineManagerFactory.php +++ b/src/Factory/DoctrineManagerFactory.php @@ -4,11 +4,12 @@ namespace Yiisoft\Yii\Doctrine\Factory; +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Exception; use Psr\Cache\CacheItemPoolInterface; use RuntimeException; use Symfony\Component\Cache\Adapter\NullAdapter; use Yiisoft\Yii\Doctrine\Dbal\Factory\ConnectionFactory; -use Yiisoft\Yii\Doctrine\Dbal\Model\ConnectionModel; use Yiisoft\Yii\Doctrine\DoctrineManager; use Yiisoft\Yii\Doctrine\Orm\Factory\EntityManagerFactory; @@ -23,6 +24,9 @@ public function __construct( ) { } + /** + * @throws Exception + */ public function create(array $doctrineConfig): DoctrineManager { // init connections @@ -47,17 +51,17 @@ public function create(array $doctrineConfig): DoctrineManager ); } - /** @var ConnectionModel|null $connectionModel */ - $connectionModel = $connections[$connectionName] ?? null; + /** @var Connection|null $connection */ + $connection = $connections[$connectionName] ?? null; - if (null === $connectionModel) { + if (null === $connection) { throw new RuntimeException( sprintf('Not found connection "%s"', $connectionName) ); } $entityManagers[$name] = $this->entityManagerFactory->create( - $connectionModel, + $connection, $this->cacheDriver, $entityManagerConfig, $doctrineConfig['orm']['proxies'] ?? [] diff --git a/src/Orm/Factory/ConfigurationFactory.php b/src/Orm/Factory/ConfigurationFactory.php index 0b1c809..d06e27c 100644 --- a/src/Orm/Factory/ConfigurationFactory.php +++ b/src/Orm/Factory/ConfigurationFactory.php @@ -15,6 +15,8 @@ use Doctrine\ORM\Mapping\TypedFieldMapper; use Doctrine\ORM\Query\AST\Functions\FunctionNode; use Doctrine\ORM\Query\Filter\SQLFilter; +use Doctrine\ORM\Repository\RepositoryFactory; +use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use Doctrine\Persistence\Mapping\Driver\PHPDriver; use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver; @@ -37,21 +39,29 @@ public function __construct( /** * @psalm-param array{ - * naming_strategy: class-string|empty, - * quote_strategy: class-string|empty, + * naming_strategy: class-string|empty, + * quote_strategy: class-string|empty, * schema_ignore_classes: list|empty, + * fetch_mode_sub_select_batch_size: int, * dql: array{ * custom_datetime_functions: array>|empty, * custom_numeric_functions: array>|empty, * custom_string_functions: array>|empty, * }|null, - * class_metadata_factory_name: class-string|empty, + * class_metadata_factory_name: class-string|empty, * default_repository_class: class-string>|empty, + * repository_factory: class-string|empty, * custom_hydration_modes: array>|empty, * filters: array>|empty, * entity_listener_resolver: class-string|empty, * typed_field_mapper: class-string|empty, - * mappings: array|empty, + * default_query_hints: array|empty, + * mappings: array, + * namespace: string, + * fileExtension: string|empty + * }>|empty, * events: array|empty, * connection: string * } $ormConfig @@ -109,6 +119,9 @@ public function create( // configure default repository class $this->configureDefaultRepositoryClass($configuration, $ormConfig['default_repository_class'] ?? null); + // configure repository factory + $this->configureRepositoryFactory($configuration, $ormConfig['repository_factory'] ?? null); + // configure custom hydration modes $this->configureCustomHydrationModes($configuration, $ormConfig['custom_hydration_modes'] ?? []); @@ -121,6 +134,15 @@ public function create( // configure typed field mapper $this->configureTypedFieldMapper($configuration, $ormConfig['typed_field_mapper'] ?? null); + // configure fetch mode sub select batch size + $this->configureFetchModeSubselectBatchSize( + $configuration, + $ormConfig['fetch_mode_sub_select_batch_size'] ?? null + ); + + // configure default query hints + $this->configureDefaultQueryHints($configuration, $ormConfig['default_query_hints'] ?? []); + // configure meta data drivers $this->configureMetaDataDrivers($configuration, $ormConfig['mappings'] ?? []); @@ -128,7 +150,7 @@ public function create( } /** - * @psalm-param class-string|null $className + * @psalm-param class-string|null $className */ private function configureNamingStrategy(Configuration $configuration, ?string $className): void { @@ -146,7 +168,7 @@ private function configureNamingStrategy(Configuration $configuration, ?string $ } /** - * @psalm-param class-string|null $className + * @psalm-param class-string|null $className */ private function configureQuoteStrategy(Configuration $configuration, ?string $className): void { @@ -182,7 +204,7 @@ private function getProxyName(?string $namespace): string } /** - * @param list $schemaIgnoreClasses + * @param list|empty $schemaIgnoreClasses */ private function configureSchemaIgnoreClasses(Configuration $configuration, array $schemaIgnoreClasses): void { @@ -228,19 +250,21 @@ private function configureCustomStringFunctions( } /** - * @psalm-param class-string|null $classMetadataFactoryName + * @psalm-param class-string|null $classMetadataFactoryName */ private function configureClassMetadataFactoryName( Configuration $configuration, ?string $classMetadataFactoryName ): void { - if (null !== $classMetadataFactoryName) { - $configuration->setClassMetadataFactoryName($classMetadataFactoryName); + if (null === $classMetadataFactoryName) { + return; } + + $configuration->setClassMetadataFactoryName($classMetadataFactoryName); } /** - * @psalm-param class-string $defaultRepositoryClass + * @psalm-param class-string|null $defaultRepositoryClass */ private function configureDefaultRepositoryClass( Configuration $configuration, @@ -251,6 +275,26 @@ private function configureDefaultRepositoryClass( } } + /** + * @psalm-param class-string|null $repositoryFactoryClass + */ + private function configureRepositoryFactory(Configuration $configuration, ?string $repositoryFactoryClass): void + { + if (null === $repositoryFactoryClass) { + return; + } + + $repositoryFactory = $this->injector->make($repositoryFactoryClass); + + if (!$repositoryFactory instanceof RepositoryFactory) { + throw new RuntimeException( + sprintf('Class %s not instance %s', $repositoryFactoryClass, RepositoryFactory::class) + ); + } + + $configuration->setRepositoryFactory($repositoryFactory); + } + /** * @psalm-param array>|empty $customHydrationModes */ @@ -272,7 +316,7 @@ private function configureFilters(Configuration $configuration, array $filters): } /** - * @psalm-param class-string|empty $entityListenerResolverClass + * @psalm-param class-string|null $entityListenerResolverClass */ private function configureEntityListenerResolver( Configuration $configuration, @@ -294,7 +338,7 @@ private function configureEntityListenerResolver( } /** - * @psalm-param class-string|empty $typedFieldMapperClass + * @psalm-param class-string|null $typedFieldMapperClass */ private function configureTypedFieldMapper(Configuration $configuration, ?string $typedFieldMapperClass): void { @@ -313,8 +357,33 @@ private function configureTypedFieldMapper(Configuration $configuration, ?string $configuration->setTypedFieldMapper($typedFieldMapper); } - /** - * @psalm-param array|empty $mappings + private function configureFetchModeSubselectBatchSize( + Configuration $configuration, + ?int $fetchModeSubselectBatchSize + ): void { + if (null === $fetchModeSubselectBatchSize) { + return; + } + + $configuration->setEagerFetchBatchSize($fetchModeSubselectBatchSize); + } + + /** + * @psalm-param array|empty $defaultQueryHints + */ + private function configureDefaultQueryHints(Configuration $configuration, array $defaultQueryHints): void + { + $configuration->setDefaultQueryHints($defaultQueryHints); + } + + /** + * @psalm-param array, + * namespace: string, + * fileExtension: + * string|empty + * }>|empty $mappings */ private function configureMetaDataDrivers(Configuration $configuration, array $mappings): void { @@ -346,24 +415,15 @@ private function configureMetaDataDrivers(Configuration $configuration, array $m break; case DriverMappingEnum::ATTRIBUTE_MAPPING: - if (PHP_VERSION_ID < 80000) { - throw new InvalidArgumentException( - sprintf( - 'Driver mapping "%s" only version php-8', - DriverMappingEnum::ATTRIBUTE_MAPPING->value - ) - ); - } - $driver = new AttributeDriver([$dir]); break; case DriverMappingEnum::PHP_MAPPING: - $driver = new PHPDriver($dir); + $driver = new PHPDriver([$dir]); break; case DriverMappingEnum::STATIC_PHP_MAPPING: - $driver = new StaticPHPDriver($dir); + $driver = new StaticPHPDriver([$dir]); break; default: diff --git a/src/Orm/Factory/DynamicEntityManagerFactory.php b/src/Orm/Factory/DynamicEntityManagerFactory.php index 1d3bfa0..65449f4 100644 --- a/src/Orm/Factory/DynamicEntityManagerFactory.php +++ b/src/Orm/Factory/DynamicEntityManagerFactory.php @@ -8,13 +8,18 @@ use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Internal\Hydration\AbstractHydrator; use Doctrine\ORM\Mapping\EntityListenerResolver; +use Doctrine\ORM\Mapping\NamingStrategy; +use Doctrine\ORM\Mapping\QuoteStrategy; use Doctrine\ORM\Mapping\TypedFieldMapper; use Doctrine\ORM\Query\AST\Functions\FunctionNode; use Doctrine\ORM\Query\Filter\SQLFilter; +use Doctrine\ORM\Repository\RepositoryFactory; +use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory; use Psr\Cache\CacheItemPoolInterface; use RuntimeException; use Symfony\Component\Cache\Adapter\NullAdapter; use Yiisoft\Yii\Doctrine\DoctrineManager; +use Yiisoft\Yii\Doctrine\Orm\Enum\DriverMappingEnum; use function sprintf; @@ -29,21 +34,29 @@ public function __construct( /** * @psalm-param array{ - * naming_strategy: class-string|empty, - * quote_strategy: class-string|empty, + * naming_strategy: class-string|empty, + * quote_strategy: class-string|empty, * schema_ignore_classes: list|empty, + * fetch_mode_sub_select_batch_size: int, * dql: array{ * custom_datetime_functions: array>|empty, * custom_numeric_functions: array>|empty, * custom_string_functions: array>|empty, * }|null, - * class_metadata_factory_name: class-string|empty, + * class_metadata_factory_name: class-string|empty, * default_repository_class: class-string>|empty, + * repository_factory: class-string|empty, * custom_hydration_modes: array>|empty, * filters: array>|empty, * entity_listener_resolver: class-string|empty, * typed_field_mapper: class-string|empty, - * mappings: array|empty, + * default_query_hints: array|empty, + * mappings: array, + * namespace: string, + * fileExtension: string|empty + * }>|empty, * events: array|empty, * connection: string * } $entityManagerConfig @@ -58,10 +71,10 @@ public function create( throw new RuntimeException(sprintf('Entity manager "%s" already exist', $entityManagerName)); } - $connectionModel = $this->doctrineManager->getConnectionModel($entityManagerConfig['connection']); + $connection = $this->doctrineManager->getConnection($entityManagerConfig['connection']); $entityManager = $this->entityManagerFactory->create( - $connectionModel, + $connection, $this->cache, $entityManagerConfig, $proxyConfig diff --git a/src/Orm/Factory/EntityManagerFactory.php b/src/Orm/Factory/EntityManagerFactory.php index 1f0af5a..e66d328 100644 --- a/src/Orm/Factory/EntityManagerFactory.php +++ b/src/Orm/Factory/EntityManagerFactory.php @@ -4,18 +4,22 @@ namespace Yiisoft\Yii\Doctrine\Orm\Factory; +use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; -use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\Internal\Hydration\AbstractHydrator; use Doctrine\ORM\Mapping\EntityListenerResolver; +use Doctrine\ORM\Mapping\NamingStrategy; +use Doctrine\ORM\Mapping\QuoteStrategy; use Doctrine\ORM\Mapping\TypedFieldMapper; use Doctrine\ORM\Query\AST\Functions\FunctionNode; use Doctrine\ORM\Query\Filter\SQLFilter; +use Doctrine\ORM\Repository\RepositoryFactory; +use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory; use Psr\Cache\CacheItemPoolInterface; -use Yiisoft\Yii\Doctrine\Dbal\Model\ConnectionModel; use Yiisoft\Yii\Doctrine\EventManager\EventManagerFactory; +use Yiisoft\Yii\Doctrine\Orm\Enum\DriverMappingEnum; final class EntityManagerFactory { @@ -27,30 +31,37 @@ public function __construct( /** * @psalm-param array{ - * naming_strategy: class-string|empty, - * quote_strategy: class-string|empty, + * naming_strategy: class-string|empty, + * quote_strategy: class-string|empty, * schema_ignore_classes: list|empty, + * fetch_mode_sub_select_batch_size: int, * dql: array{ * custom_datetime_functions: array>|empty, * custom_numeric_functions: array>|empty, * custom_string_functions: array>|empty, * }|null, - * class_metadata_factory_name: class-string|empty, + * class_metadata_factory_name: class-string|empty, * default_repository_class: class-string>|empty, + * repository_factory: class-string|empty, * custom_hydration_modes: array>|empty, * filters: array>|empty, * entity_listener_resolver: class-string|empty, * typed_field_mapper: class-string|empty, - * mappings: array|empty, + * default_query_hints: array|empty, + * mappings: array, + * namespace: string, + * fileExtension: string|empty + * }>|empty, * events: array|empty, * connection: string * } $entityManagerConfig * @psalm-param array{auto_generate: bool|null, path: string, namespace: string|null} $proxyConfig * - * @throws ORMException */ public function create( - ConnectionModel $connectionModel, + Connection $connection, CacheItemPoolInterface $cache, array $entityManagerConfig, array $proxyConfig @@ -61,15 +72,8 @@ public function create( $proxyConfig ); - $this->eventManagerFactory->createForOrm( - $connectionModel->getEventManager(), - $entityManagerConfig['events'] ?? [] - ); + $eventManager = $this->eventManagerFactory->create($entityManagerConfig['events'] ?? []); - return new EntityManager( - $connectionModel->getConnection(), - $configuration, - $connectionModel->getEventManager() - ); + return new EntityManager($connection, $configuration, $eventManager); } }