Skip to content

Commit

Permalink
bug #628 Workaround to use DATABASE_URL with %kernel.project_dir% par…
Browse files Browse the repository at this point in the history
…ameter (yceruto)

This PR was merged into the master branch.

Discussion
----------

Workaround to use DATABASE_URL with %kernel.project_dir% parameter

Setting `DATABASE_URL=sqlite:///var/data/blog.sqlite` as default, causes this known error through `index.php`:
```
An exception occurred in driver: SQLSTATE[HY000] [14] unable to open database file
```
So the right relative path in this case should be `sqlite:///../var/data/blog.sqlite` BUT then it wouldn't work for `bin/console` entries. Then, relative paths shouldn't be the right way but absolute paths.

The right thing is that soon (in 3.4 & 4.0) we can use environment variables with configuration parameters symfony/symfony#23901, so the final configuration would be the one proposed. For now a workaround to make both scenarios work `index.php` and `bin/console`.

Commits
-------

e45b5d5 Workaround to use DATABASE_URL with %kernel.project_dir% parameter
  • Loading branch information
javiereguiluz committed Aug 30, 2017
2 parents 5358558 + e45b5d5 commit 1fc329c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .env.dist
Expand Up @@ -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 ###
Expand Down
19 changes: 19 additions & 0 deletions src/Kernel.php
Expand Up @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit 1fc329c

Please sign in to comment.