Skip to content
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

feat: Support doctrine 3. #1

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DoctrineManagerFactory $doctrineManagerFactory
): DoctrineManager => $doctrineManagerFactory->create($params['yiisoft/yii-doctrine'] ?? []),
'reset' => function (): void {
/** @var DoctrineManager $this */
$this->resetAllManager();
},
],
Expand Down
7 changes: 7 additions & 0 deletions config/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
Expand Down
7 changes: 6 additions & 1 deletion src/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
12 changes: 8 additions & 4 deletions src/Dbal/Factory/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
16 changes: 6 additions & 10 deletions src/Dbal/Factory/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand All @@ -25,32 +23,30 @@ public function __construct(
* auto_commit: bool|empty,
* custom_types: array<string, class-string<Type>>|empty,
* events: array|empty,
* middlewares: array<array-key, class-string<\Doctrine\DBAL\Driver\Middleware>>|empty,
* middlewares: array<array-key, class-string<Middleware>>|empty,
* params: array<string, mixed>,
* 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');
}

$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<string, class-string<\Doctrine\DBAL\Types\Type>>|empty $customTypes
* @psalm-param array<string, class-string<Type>>|empty $customTypes
*
* @throws Exception
*/
Expand Down
15 changes: 7 additions & 8 deletions src/Dbal/Factory/DynamicConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,20 +28,18 @@ public function __construct(
* params: array<string, mixed>,
* 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;
}
}
27 changes: 0 additions & 27 deletions src/Dbal/Model/ConnectionModel.php

This file was deleted.

52 changes: 15 additions & 37 deletions src/DoctrineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,8 +26,8 @@ final class DoctrineManager implements ManagerRegistry
private string $proxyInterfaceName = Proxy::class;

/**
* @param ConnectionModel[] $connections
* @param EntityManagerInterface[] $managers
* @psalm-param array<string, object<Connection>> $connections
* @psalm-param array<string, object<EntityManagerInterface>> $managers
*/
public function __construct(
private array $connections,
Expand All @@ -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]);
}
Expand All @@ -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
Expand All @@ -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<array-key>
*/
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();
Expand Down Expand Up @@ -200,7 +178,7 @@ public function flushAllManager(): void
/**
* @psalm-return list<array-key>
*/
public function getManagerNames()
public function getManagerNames(): array
{
return array_keys($this->managers);
}
Expand Down Expand Up @@ -272,7 +250,7 @@ public function getManagerForClass($class): ?EntityManagerInterface
}

/**
* @psalm-return array<string, \Doctrine\Persistence\ObjectManager>
* @psalm-return array<string, ObjectManager>
*/
public function getManagers(): array
{
Expand Down
11 changes: 1 addition & 10 deletions src/EventManager/EventManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'] ?? []);
}
}
14 changes: 9 additions & 5 deletions src/Factory/DoctrineManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,6 +24,9 @@ public function __construct(
) {
}

/**
* @throws Exception
*/
public function create(array $doctrineConfig): DoctrineManager
{
// init connections
Expand All @@ -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'] ?? []
Expand Down
Loading
Loading