diff --git a/.env.dist b/.env.dist index 5b3e8dedf..61dfee9ff 100644 --- a/.env.dist +++ b/.env.dist @@ -12,7 +12,7 @@ APP_SECRET=67d829bf61dc5f87a73fd814e2c9f629 # Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db" # Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls -DATABASE_URL=sqlite:///var/data/blog.sqlite +DATABASE_URL=sqlite:///%kernel.project_dir%/var/data/blog.sqlite ###< doctrine/doctrine-bundle ### ###> symfony/swiftmailer-bundle ### diff --git a/src/Kernel.php b/src/Kernel.php index 03572cebd..bb367d375 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -14,6 +14,7 @@ use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\Dotenv\Dotenv; use Symfony\Component\HttpKernel\Kernel as BaseKernel; use Symfony\Component\Routing\RouteCollectionBuilder; @@ -26,6 +27,24 @@ final class Kernel extends BaseKernel private const CONFIG_EXTS = '.{php,xml,yaml,yml}'; + public function __construct($environment, $debug) + { + /* + * Workaround to avoid: An exception occurred in driver: SQLSTATE[HY000] [14] unable to open database file + * As environment variables is not supported yet to be used with configuration parameters. + * + * @TODO remove in 3.4 + */ + + if (isset($_ENV['DATABASE_URL']) && false !== mb_strpos($_ENV['DATABASE_URL'], '%kernel.project_dir%')) { + (new Dotenv())->populate([ + 'DATABASE_URL' => str_replace('%kernel.project_dir%', $this->getProjectDir(), $_ENV['DATABASE_URL']), + ]); + } + + parent::__construct($environment, $debug); + } + public function getCacheDir(): string { return dirname(__DIR__).'/var/cache/'.$this->environment;