Skip to content

Commit

Permalink
Merge pull request #5 from stargazer-team/fear-update-config
Browse files Browse the repository at this point in the history
feat: Update config and dependency.
  • Loading branch information
stargazer-team authored Jan 13, 2025
2 parents 84e01d2 + 394300c commit ff65b64
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 75 deletions.
30 changes: 20 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
# Yii3 Doctrine Change Log

## 1.0.0 under development

- Initial release.
## 2.1.0

- Dbal:
- add config section "connections"
- move section "custom_types"
- add options "mapping_types", "disable_type_comments", "schema_manager_factory"
- ORM:
- add options "identity_generation_preferences"
- Update dependency

## 1.0.1
## 2.0.0

- Support doctrine orm 3.
- Update dependency
- Update cache
- Add config options
- Fix pipeline

## 1.0.2

- Fix readme.

## 2.0.0
## 1.0.1

- Update dependency
- Update cache
- Add config options
- Fix pipeline
- Support doctrine orm 3.

## 1.0.0 under development

- Initial release.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require": {
"php": ">=8.1.0",
"doctrine/migrations": "^3.8.2",
"doctrine/orm": "^3.3.0",
"doctrine/orm": "^3.3.1",
"psr/cache": "^2.0|^3.0",
"psr/log": "^2|^3",
"symfony/cache": "^6.4.16",
Expand Down
71 changes: 41 additions & 30 deletions config/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,48 @@
return [
'yiisoft/yii-doctrine' => [
DbalConfigOption::DBAL => [
'default' => [
// check params https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/configuration.html
DbalConfigOption::PARAMS => [
'driver' => 'pdo_pgsql',
'dbname' => 'dbname',
'host' => 'localhost',
'password' => 'secret',
'user' => 'postgres',
],
DbalConfigOption::CUSTOM_TYPES => [
// UuidType::NAME => UuidType::class
],
DbalConfigOption::AUTO_COMMIT => false,
// DbalConfigOption::SCHEMA_ASSETS_FILTER => static function (string|Sequence $table): bool {
// if (is_string($table)) {
// return $table === 'geo_locations';
// }
DbalConfigOption::CONNECTIONS => [
'default' => [
// check params https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/configuration.html
DbalConfigOption::PARAMS => [
'driver' => 'pdo_pgsql',
'dbname' => 'dbname',
'host' => 'localhost',
'password' => 'secret',
'user' => 'postgres',
],
// DbalConfigOption::AUTO_COMMIT => false,
// DbalConfigOption::DISABLE_TYPE_COMMENTS => false,
// DbalConfigOption::SCHEMA_MANAGER_FACTORY => DefaultSchemaManagerFactory::class,
// DbalConfigOption::SCHEMA_ASSETS_FILTER => static function (string|Sequence $table): bool {
// if (is_string($table)) {
// return $table === 'geo_locations';
// }
//
// return true;
// },
DbalConfigOption::MIDDLEWARES => [
// logger middleware
Doctrine\DBAL\Logging\Middleware::class
]
],
'mysql' => [
DbalConfigOption::PARAMS => [
'driver' => 'pdo_mysql',
'dbname' => 'dbname',
'host' => 'localhost',
'password' => 'secret',
'user' => 'root',
// check https://www.doctrine-project.org/projects/doctrine-orm/en/3.3/cookbook/custom-mapping-types.html
// DbalConfigOption::MAPPING_TYPES => [
// 'db_mytype' => 'mytype',
// ],
DbalConfigOption::MIDDLEWARES => [
// logger middleware
Doctrine\DBAL\Logging\Middleware::class
]
],
]
'mysql' => [
DbalConfigOption::PARAMS => [
'driver' => 'pdo_mysql',
'dbname' => 'dbname',
'host' => 'localhost',
'password' => 'secret',
'user' => 'root',
],
],
],
// DbalConfigOption::CUSTOM_TYPES => [
// UuidType::NAME => UuidType::class
// ],
],
OrmConfigOptions::ORM => [
OrmConfigOptions::PROXIES => [
Expand All @@ -69,6 +77,9 @@
// OrmConfigOptions::DEFAULT_QUERY_HINTS => [
// Query::HINT_CUSTOM_OUTPUT_WALKER => Query\SqlWalker::class,
// ],
// OrmConfigOptions::IDENTITY_GENERATION_PREFERENCES => [
// PostgreSQLPlatform::CLASS => ClassMetadata::GENERATOR_TYPE_SEQUENCE,
// ],
OrmConfigOptions::MAPPINGS => [
'User' => [
OrmConfigOptions::MAPPING_DIR => '@src/User/Entity',
Expand Down
49 changes: 49 additions & 0 deletions src/Dbal/CustomerTypeConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Doctrine\Dbal;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

use const PHP_EOL;

final class CustomerTypeConfigurator
{
public function add(array $customTypes): void
{
foreach ($customTypes as $name => $className) {
if (!Type::hasType($name)) {
Type::addType($name, $className);
}
}
}

public function registerDoctrineTypeMapping(Connection $connection, array $mappingTypes): void
{
$platform = $this->getDatabasePlatform($connection);

foreach ($mappingTypes as $dbType => $doctrineType) {
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
}
}

private function getDatabasePlatform(Connection $connection): AbstractPlatform
{
try {
return $connection->getDatabasePlatform();
} catch (DriverException $driverException) {
throw new ConnectionException(
'An exception occurred while establishing a connection to figure out your platform version.' . PHP_EOL .
"You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
'For further information have a look at:' . PHP_EOL,
0,
$driverException,
);
}
}
}
8 changes: 8 additions & 0 deletions src/Dbal/Enum/ConfigOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ final class ConfigOptions
{
public const AUTO_COMMIT = 'auto_commit';

public const CONNECTIONS = 'connections';

public const CUSTOM_TYPES = 'custom_types';

public const DISABLE_TYPE_COMMENTS = 'disable_type_comments';

public const DBAL = 'dbal';

public const MAPPING_TYPES = 'mapping_types';

public const MIDDLEWARES = 'middlewares';

public const PARAMS = 'params';

public const SCHEMA_ASSETS_FILTER = 'schema_assets_filter';

public const SCHEMA_MANAGER_FACTORY = 'schema_manager_factory';

private function __construct()
{
}
Expand Down
48 changes: 45 additions & 3 deletions src/Dbal/Factory/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Driver\Middleware;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\SchemaManagerFactory;
use RuntimeException;
use Yiisoft\Injector\Injector;
use Yiisoft\Yii\Doctrine\Dbal\Enum\ConfigOptions;

use function array_map;
use function sprintf;

final class ConfigurationFactory
{
public function __construct(
Expand All @@ -20,11 +24,13 @@ public function __construct(
/**
* @psalm-param array{
* auto_commit: bool,
* custom_types: array<string, class-string<Type>>,
* events: array<array-key, mixed>,
* middlewares: array<array-key, class-string<\Doctrine\DBAL\Driver\Middleware>>|empty,
* params: array<string, mixed>,
* schema_assets_filter: callable
* schema_assets_filter: callable,
* mapping_types: array<string, string>,
* disable_type_comments: bool,
* schema_manager_factory: class-string<SchemaManagerFactory>
* } $dbalConfig
*/
public function create(array $dbalConfig): Configuration
Expand All @@ -44,6 +50,10 @@ function (string $classMiddleware): Middleware {

$this->configureSchemaAssetsFilter($configuration, $dbalConfig[ConfigOptions::SCHEMA_ASSETS_FILTER] ?? null);

$this->configureDisableTypeComments($configuration, $dbalConfig[ConfigOptions::DISABLE_TYPE_COMMENTS] ?? null);

$this->configureSchemaManagerFactory($configuration, $dbalConfig[ConfigOptions::SCHEMA_MANAGER_FACTORY] ?? []);

return $configuration;
}

Expand All @@ -57,6 +67,19 @@ private function configureAutoCommit(Configuration $configuration, ?bool $autoCo
$configuration->setAutoCommit($autoCommit);
}

private function configureDisableTypeComments(Configuration $configuration, ?bool $disableTypeComments): void
{
if (null === $disableTypeComments) {
return;
}

if (!$disableTypeComments) {
return;
}

$configuration->setDisableTypeComments($disableTypeComments);
}

private function configureSchemaAssetsFilter(Configuration $configuration, ?callable $schemaAssetsFilter): void
{
if (null === $schemaAssetsFilter) {
Expand All @@ -65,4 +88,23 @@ private function configureSchemaAssetsFilter(Configuration $configuration, ?call

$configuration->setSchemaAssetsFilter($schemaAssetsFilter);
}

private function configureSchemaManagerFactory(
Configuration $configuration,
?string $configureSchemaManagerFactoryClass
): void {
if (null === $configureSchemaManagerFactoryClass) {
return;
}

$schemaManagerFactory = $this->injector->make($configureSchemaManagerFactoryClass);

if (!$schemaManagerFactory instanceof SchemaManagerFactory) {
throw new RuntimeException(
sprintf('Class %s not instance %s', $configureSchemaManagerFactoryClass, SchemaManagerFactory::class)
);
}

$configuration->setSchemaManagerFactory($schemaManagerFactory);
}
}
31 changes: 11 additions & 20 deletions src/Dbal/Factory/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@
use Doctrine\DBAL\Driver\Middleware;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\SchemaManagerFactory;
use InvalidArgumentException;
use Yiisoft\Yii\Doctrine\Dbal\CustomerTypeConfigurator;
use Yiisoft\Yii\Doctrine\Dbal\Enum\ConfigOptions;

final class ConnectionFactory
{
public function __construct(
private readonly ConfigurationFactory $configurationFactory,
private readonly CustomerTypeConfigurator $customerTypeConfigurator,
) {
}

/**
* @psalm-param array{
* auto_commit: bool|empty,
* custom_types: array<string, class-string<Type>>|empty,
* events: array|empty,
* middlewares: array<array-key, class-string<Middleware>>|empty,
* params: array<string, mixed>,
* schema_assets_filter: callable|empty
* schema_assets_filter: callable|empty,
* mapping_types: array<string, string>,
* disable_type_comments: bool,
* schema_manager_factory: class-string<SchemaManagerFactory>
* } $dbalConfig
*
* @throws Exception
Expand All @@ -41,24 +45,11 @@ public function create(array $dbalConfig): Connection

$connection = DriverManager::getConnection($dbalConfig[ConfigOptions::PARAMS], $configuration);

$this->configureCustomTypes($connection, $dbalConfig[ConfigOptions::CUSTOM_TYPES] ?? []);
$this->customerTypeConfigurator->registerDoctrineTypeMapping(
$connection,
$dbalConfig[ConfigOptions::PARAMS][ConfigOptions::MAPPING_TYPES] ?? [],
);

return $connection;
}

/**
* @psalm-param array<string, class-string<Type>>|empty $customTypes
*
* @throws Exception
*/
private function configureCustomTypes(Connection $connection, array $customTypes): void
{
foreach ($customTypes as $name => $className) {
if (!Type::hasType($name)) {
Type::addType($name, $className);
}

$connection->getDatabasePlatform()->registerDoctrineTypeMapping($name, $name);
}
}
}
7 changes: 5 additions & 2 deletions src/Dbal/Factory/DynamicConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\SchemaManagerFactory;
use RuntimeException;
use Yiisoft\Yii\Doctrine\DoctrineManager;

Expand All @@ -22,11 +23,13 @@ public function __construct(
/**
* @psalm-param array{
* auto_commit: bool,
* custom_types: array<string, class-string<\Doctrine\DBAL\Types\Type>>,
* events: array<array-key, mixed>,
* middlewares: array<array-key, class-string<\Doctrine\DBAL\Driver\Middleware>>|empty,
* params: array<string, mixed>,
* schema_assets_filter: callable
* schema_assets_filter: callable,
* mapping_types: array<string, string>,
* disable_type_comments: bool,
* schema_manager_factory: class-string<SchemaManagerFactory>
* } $dbalConfig
* @throws Exception
*/
Expand Down
Loading

0 comments on commit ff65b64

Please sign in to comment.