diff --git a/app/AppKernel.php b/app/AppKernel.php index 61b734e06..314b34390 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -97,5 +97,37 @@ class AppKernel extends Kernel // $container->setParameter('container.dumper.inline_class_loader', true); $container->addObjectResource($this); }); + + $loader->load(function (ContainerBuilder $container) { + $this->processDatabaseParameters($container); + }); + } + + private function processDatabaseParameters(ContainerBuilder $container) + { + switch ($container->getParameter('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('database_driver')); + } + + $container->setParameter('database_scheme', $scheme); + + if ('sqlite' === $scheme) { + $container->setParameter('database_name', $container->getParameter('database_path')); + } + + $container->setParameter('database_user', (string) $container->getParameter('database_user')); + $container->setParameter('database_password', (string) $container->getParameter('database_password')); + $container->setParameter('database_port', (string) $container->getParameter('database_port')); + $container->setParameter('database_socket', (string) $container->getParameter('database_socket')); } } diff --git a/app/config/config.yml b/app/config/config.yml index 37f6d06b3..d413ceca2 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -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 diff --git a/app/config/config_test.yml b/app/config/config_test.yml index d738a49d6..4ea1c0a48 100644 --- a/app/config/config_test.yml +++ b/app/config/config_test.yml @@ -3,6 +3,9 @@ imports: - { resource: parameters_test.yml } - { resource: services_test.yml } +parameters: + fosuser_registration: true + framework: test: ~ session: @@ -20,14 +23,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)%" + dbname_suffix: '_test' # for MySQL and PostgreSQL orm: metadata_cache_driver: diff --git a/app/config/parameters_test.yml b/app/config/parameters_test.yml index 9800b2605..551092d74 100644 --- a/app/config/parameters_test.yml +++ b/app/config/parameters_test.yml @@ -1,11 +1,5 @@ parameters: - test_database_driver: pdo_sqlite - test_database_host: 127.0.0.1 - test_database_port: null - test_database_name: null - test_database_user: null - test_database_password: null - test_database_path: "%env(TEST_DATABASE_PATH)%" + # Using an environment variable in order to avoid the error "attempt to write a readonly database" + # when the schema is dropped then recreate + database_path: "%env(TEST_DATABASE_PATH)%" env(TEST_DATABASE_PATH): "%kernel.project_dir%/data/db/wallabag_test.sqlite" - test_database_charset: utf8 - fosuser_registration: true diff --git a/app/config/tests/parameters_test.mysql.yml b/app/config/tests/parameters_test.mysql.yml index 9c6eebc97..6e1a87b79 100644 --- a/app/config/tests/parameters_test.mysql.yml +++ b/app/config/tests/parameters_test.mysql.yml @@ -1,11 +1,2 @@ parameters: - test_database_driver: pdo_mysql - test_database_host: 127.0.0.1 - test_database_port: 3306 - test_database_name: wallabag_test - test_database_user: root - test_database_password: root - test_database_path: ~ - env(TEST_DATABASE_PATH): ~ - test_database_charset: utf8mb4 - fosuser_registration: true + env(DATABASE_URL): mysql://root:root@127.0.0.1:3306/wallabag?charset=utf8mb4 diff --git a/app/config/tests/parameters_test.pgsql.yml b/app/config/tests/parameters_test.pgsql.yml index e5f87fdd5..8a3d0498e 100644 --- a/app/config/tests/parameters_test.pgsql.yml +++ b/app/config/tests/parameters_test.pgsql.yml @@ -1,11 +1,2 @@ parameters: - test_database_driver: pdo_pgsql - test_database_host: localhost - test_database_port: - test_database_name: wallabag_test - test_database_user: wallabag - test_database_password: wallabagrocks - test_database_path: ~ - env(TEST_DATABASE_PATH): ~ - test_database_charset: utf8 - fosuser_registration: true + env(DATABASE_URL): postgres://wallabag:wallabagrocks@localhost/wallabag?charset=utf8 diff --git a/app/config/tests/parameters_test.sqlite.yml b/app/config/tests/parameters_test.sqlite.yml index 30f11e115..ac76d61ec 100644 --- a/app/config/tests/parameters_test.sqlite.yml +++ b/app/config/tests/parameters_test.sqlite.yml @@ -1,13 +1,5 @@ parameters: - test_database_driver: pdo_sqlite - test_database_host: localhost - test_database_port: - test_database_name: ~ - test_database_user: ~ - test_database_password: ~ + env(DATABASE_URL): sqlite://:@localhost/%env(TEST_DATABASE_PATH)%?charset=utf8 # Using an environment variable in order to avoid the error "attempt to write a readonly database" # when the schema is dropped then recreate - test_database_path: "%env(TEST_DATABASE_PATH)%" env(TEST_DATABASE_PATH): "%kernel.project_dir%/data/db/wallabag_test.sqlite" - test_database_charset: utf8 - fosuser_registration: true