Skip to content

Commit

Permalink
Load the .env(.local) file using Symfony's Dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
StijnVrolijk committed Jun 25, 2019
1 parent 6d9b323 commit 0253798
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions autoload.php
@@ -1,6 +1,9 @@
<?php

// @TODO rename this file to bootstrap.php to better reflect its function

use Doctrine\Common\Annotations\AnnotationRegistry;
use Symfony\Component\Dotenv\Dotenv;

// use vendor generated autoloader
$loader = require __DIR__ . '/vendor/autoload.php';
Expand All @@ -10,4 +13,27 @@
set_include_path(__DIR__ . '/vendor/spoon/library' . PATH_SEPARATOR . get_include_path());
require_once 'spoon/spoon.php';

// load server variables
if (!array_key_exists('FORK_ENV', $_SERVER)) {
$_SERVER['FORK_ENV'] = $_ENV['FORK_ENV'] ?? null;
}

if ('prod' !== $_SERVER['FORK_ENV']) {
if (!class_exists(Dotenv::class)) {
throw new RuntimeException('The "FORK_ENV" environment variable is not set to "prod". Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
}
$dotenv = new Dotenv();

$dotenv->load(__DIR__ . '/.env');

// @TODO when updating to Fork 6, using Symfony 4, check if we still need to check the file's existence
if (file_exists(__DIR__ . '/.env.local')) {
$dotenv->load(__DIR__ . '/.env.local');
}
}

$_SERVER['FORK_ENV'] = $_ENV['FORK_ENV'] = $_SERVER['FORK_ENV'] ?: $_ENV['FORK_ENV'] ?: 'dev';
$_SERVER['FORK_DEBUG'] = $_SERVER['FORK_DEBUG'] ?? $_ENV['FORK_DEBUG'] ?? 'prod' !== $_SERVER['FORK_ENV'];
$_SERVER['FORK_DEBUG'] = $_ENV['FORK_DEBUG'] = (int) $_SERVER['FORK_DEBUG'] || filter_var($_SERVER['FORK_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';

return $loader;

0 comments on commit 0253798

Please sign in to comment.