From 34b84cced97e2c4231e93a5333b1e2e1430c56ab Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 21 Oct 2019 11:20:29 +0200 Subject: [PATCH] Update symfony configuration https://symfony.com/doc/current/configuration/dot-env-changes.html --- .env.dist => .env | 0 .env.test | 4 ++++ .gitignore | 4 +++- .htaccess | 2 +- config/bootstrap.php | 21 +++++++++++++++++++++ main/install/index.php | 8 ++++---- public/index.php | 28 ++++++---------------------- 7 files changed, 39 insertions(+), 28 deletions(-) rename .env.dist => .env (100%) create mode 100644 .env.test create mode 100644 config/bootstrap.php diff --git a/.env.dist b/.env similarity index 100% rename from .env.dist rename to .env diff --git a/.env.test b/.env.test new file mode 100644 index 0000000000..414430e7ff --- /dev/null +++ b/.env.test @@ -0,0 +1,4 @@ +# define your env variables for the test env here +KERNEL_CLASS='App\Kernel' +APP_SECRET='s$cretf0rt3st' +SYMFONY_DEPRECATIONS_HELPER=999999 diff --git a/.gitignore b/.gitignore index 3a698571c4..ab486ea852 100755 --- a/.gitignore +++ b/.gitignore @@ -55,7 +55,9 @@ composer.lock symfony.lock ###> symfony/framework-bundle ### -.env +/.env.local +/.env.local.php +/.env.*.local /public/bundles/ /var/ /vendor diff --git a/.htaccess b/.htaccess index ae56f9ed96..81932944e9 100644 --- a/.htaccess +++ b/.htaccess @@ -74,7 +74,7 @@ RewriteRule ^service/(\d{1,})$ plugin/buycourses/src/service_information.php?ser RewriteRule ^lti/os$ public/lti/os [L] # Deny access -RewriteRule ^(tests|.git|.env|.env.dist|config) - [F,L,NC] +RewriteRule ^(tests|.git|.env|.env.dist|.env.local|config) - [F,L,NC] # Add caching of woff font files to avoid loading 2*15KB each time with Chamilo # default OpenSans font diff --git a/config/bootstrap.php b/config/bootstrap.php new file mode 100644 index 0000000000..f6afb40429 --- /dev/null +++ b/config/bootstrap.php @@ -0,0 +1,21 @@ +=1.2) +if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) { + $_SERVER += $env; + $_ENV += $env; +} elseif (!class_exists(Dotenv::class)) { + throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.'); +} else { + // load all the .env files + (new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); +} + +$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'dev'; +$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV']; +$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0'; diff --git a/main/install/index.php b/main/install/index.php index 1bd138b486..4db12441f0 100755 --- a/main/install/index.php +++ b/main/install/index.php @@ -590,8 +590,8 @@ if (@$_POST['step2']) { migrateSwitch($my_old_version, $manager); // Create .env file - $envFile = api_get_path(SYS_PATH).'.env'; - $distFile = api_get_path(SYS_PATH).'.env.dist'; + $envFile = api_get_path(SYS_PATH).'.env.local'; + $distFile = api_get_path(SYS_PATH).'.env'; $params = [ '{{DATABASE_HOST}}' => $dbHostForm, @@ -654,8 +654,8 @@ if (@$_POST['step2']) { $manager = $database->getManager(); // Create .env file - $envFile = api_get_path(SYS_PATH).'.env'; - $distFile = api_get_path(SYS_PATH).'.env.dist'; + $envFile = api_get_path(SYS_PATH).'.env.local'; + $distFile = api_get_path(SYS_PATH).'.env'; $params = [ '{{DATABASE_HOST}}' => $dbHostForm, diff --git a/public/index.php b/public/index.php index 485c558c38..44996a1087 100644 --- a/public/index.php +++ b/public/index.php @@ -3,39 +3,23 @@ use Chamilo\Kernel; use Symfony\Component\Debug\Debug; -use Symfony\Component\Dotenv\Dotenv; use Symfony\Component\HttpFoundation\Request; -require __DIR__.'/../vendor/autoload.php'; - -// The check is to ensure we don't use .env in production -if (!isset($_SERVER['APP_ENV'])) { - if (!class_exists(Dotenv::class)) { - throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.'); - } - (new Dotenv())->load(__DIR__.'/../.env'); -} - -$env = $_SERVER['APP_ENV'] ?? 'dev'; -$debug = $_SERVER['APP_DEBUG'] ?? ('prod' !== $env); - -if ($debug) { +require dirname(__DIR__).'/config/bootstrap.php'; +if ($_SERVER['APP_DEBUG']) { umask(0000); - Debug::enable(); } - -if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) { +if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) { Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST); } - -if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) { - Request::setTrustedHosts(explode(',', $trustedHosts)); +if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) { + Request::setTrustedHosts([$trustedHosts]); } require_once __DIR__.'/legacy.php'; -$kernel = new Kernel($env, $debug); +$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); //$request = Request::createFromGlobals(); $request = Sonata\PageBundle\Request\RequestFactory::createFromGlobals( 'host_with_path_by_locale'