Use URL to configure DBAL

This commit is contained in:
Yassine Guedidi 2024-01-04 02:04:20 +01:00
parent a6af149fa3
commit aa7dd67131
3 changed files with 46 additions and 18 deletions

View File

@ -97,5 +97,47 @@ class AppKernel extends Kernel
// $container->setParameter('container.dumper.inline_class_loader', true);
$container->addObjectResource($this);
});
$loader->load(function (ContainerBuilder $container) {
$this->processDatabaseParameters($container);
if ('test' === $container->getParameter('kernel.environment')) {
$this->processDatabaseParameters($container, true);
}
});
}
private function processDatabaseParameters(ContainerBuilder $container, bool $isTest = false)
{
$prefix = '';
if ($isTest) {
$prefix = 'test_';
}
switch ($container->getParameter($prefix . 'database_driver')) {
case 'pdo_mysql':
$scheme = 'mysql';
break;
case 'pdo_pgsql':
$scheme = 'pgsql';
break;
case 'pdo_sqlite':
$scheme = 'sqlite';
break;
default:
throw new \RuntimeException('Unsupported database driver: ' . $container->getParameter($prefix . 'database_driver'));
}
$container->setParameter($prefix . 'database_scheme', $scheme);
if ('sqlite' === $scheme) {
$container->setParameter($prefix . 'database_name', $container->getParameter($prefix . 'database_path'));
}
$container->setParameter($prefix . 'database_user', (string) $container->getParameter($prefix . 'database_user'));
$container->setParameter($prefix . 'database_password', (string) $container->getParameter($prefix . 'database_password'));
$container->setParameter($prefix . 'database_port', (string) $container->getParameter($prefix . 'database_port'));
$container->setParameter($prefix . 'database_socket', (string) $container->getParameter($prefix . 'database_socket'));
}
}

View File

@ -8,6 +8,7 @@ parameters:
# Allows to use the live reload feature for changes in assets
use_webpack_dev_server: false
craue_config.cache_adapter.class: Craue\ConfigBundle\CacheAdapter\SymfonyCacheComponentAdapter
env(DATABASE_URL): '%database_scheme%://%database_user%:%database_password%@%database_host%:%database_port%/%database_name%?unix_socket=%database_socket%&charset=%database_charset%'
framework:
#esi: ~
@ -48,15 +49,7 @@ twig:
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: "%database_charset%"
path: "%database_path%"
unix_socket: "%database_socket%"
url: '%env(resolve:DATABASE_URL)%'
types:
json_array: Wallabag\CoreBundle\Doctrine\JsonArrayType

View File

@ -4,6 +4,7 @@ imports:
- { resource: services_test.yml }
parameters:
env(TEST_DATABASE_URL): '%test_database_scheme%://%test_database_user%:%test_database_password%@%test_database_host%:%test_database_port%/%test_database_name%?unix_socket=%test_database_socket%&charset=%test_database_charset%'
fosuser_registration: true
framework:
@ -23,15 +24,7 @@ web_profiler:
doctrine:
dbal:
driver: "%test_database_driver%"
host: "%test_database_host%"
port: "%test_database_port%"
dbname: "%test_database_name%"
user: "%test_database_user%"
password: "%test_database_password%"
charset: "%test_database_charset%"
path: "%env(TEST_DATABASE_PATH)%"
unix_socket: "%test_database_socket%"
url: '%env(resolve:TEST_DATABASE_URL)%'
orm:
metadata_cache_driver: