diff --git a/.env.dist b/.env similarity index 98% rename from .env.dist rename to .env index 8d20bf8..e45b094 100644 --- a/.env.dist +++ b/.env @@ -3,7 +3,7 @@ # https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration ###> symfony/framework-bundle ### -APP_ENV=dev +APP_ENV=prod APP_SECRET=xxx #TRUSTED_PROXIES=127.0.0.1,127.0.0.2 #TRUSTED_HOSTS=localhost,example.com diff --git a/.gitignore b/.gitignore index bfea97f..118a8d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ /.idea/ ###> symfony/framework-bundle ### -/.env +/.env.local +/.env.local.php +/.env.*.local /public/bundles/ /var/ /vendor/ diff --git a/bin/console b/bin/console index 5187d02..19c2f6c 100755 --- a/bin/console +++ b/bin/console @@ -5,28 +5,31 @@ use App\Kernel; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Debug\Debug; -use Symfony\Component\Dotenv\Dotenv; + +if (false === in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) { + echo 'Warning: The console should be invoked via the CLI version of PHP, not the '.\PHP_SAPI.' SAPI'.\PHP_EOL; +} set_time_limit(0); -require __DIR__.'/../vendor/autoload.php'; +require dirname(__DIR__).'/vendor/autoload.php'; if (!class_exists(Application::class)) { - throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.'); -} - -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'); + throw new RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.'); } $input = new ArgvInput(); -$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev', true); -$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption('--no-debug', true); +if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) { + putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env); +} -if ($debug) { +if ($input->hasParameterOption('--no-debug', true)) { + putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'); +} + +require dirname(__DIR__).'/config/bootstrap.php'; + +if ($_SERVER['APP_DEBUG']) { umask(0000); if (class_exists(Debug::class)) { @@ -34,6 +37,6 @@ if ($debug) { } } -$kernel = new Kernel($env, $debug); +$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $application = new Application($kernel); $application->run($input); diff --git a/composer.lock b/composer.lock index 38515f4..2cc8994 100644 --- a/composer.lock +++ b/composer.lock @@ -1869,42 +1869,42 @@ }, { "name": "sensio/framework-extra-bundle", - "version": "v5.3.1", + "version": "v5.4.1", "source": { "type": "git", "url": "https://github.com/sensiolabs/SensioFrameworkExtraBundle.git", - "reference": "5f75c4658b03301cba17baa15a840b57b72b4262" + "reference": "585f4b3a1c54f24d1a8431c729fc8f5acca20c8a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sensiolabs/SensioFrameworkExtraBundle/zipball/5f75c4658b03301cba17baa15a840b57b72b4262", - "reference": "5f75c4658b03301cba17baa15a840b57b72b4262", + "url": "https://api.github.com/repos/sensiolabs/SensioFrameworkExtraBundle/zipball/585f4b3a1c54f24d1a8431c729fc8f5acca20c8a", + "reference": "585f4b3a1c54f24d1a8431c729fc8f5acca20c8a", "shasum": "" }, "require": { "doctrine/annotations": "^1.0", "doctrine/persistence": "^1.0", "php": ">=7.1.3", - "symfony/config": "^3.4|^4.2", - "symfony/dependency-injection": "^3.4|^4.2", - "symfony/framework-bundle": "^3.4|^4.2", - "symfony/http-kernel": "^3.4|^4.2" + "symfony/config": "^3.4|^4.3", + "symfony/dependency-injection": "^3.4|^4.3", + "symfony/framework-bundle": "^3.4|^4.3", + "symfony/http-kernel": "^3.4|^4.3" }, "require-dev": { "doctrine/doctrine-bundle": "^1.6", "doctrine/orm": "^2.5", "nyholm/psr7": "^1.1", - "symfony/browser-kit": "^3.4|^4.2", - "symfony/dom-crawler": "^3.4|^4.2", - "symfony/expression-language": "^3.4|^4.2", - "symfony/finder": "^3.4|^4.2", + "symfony/browser-kit": "^3.4|^4.3", + "symfony/dom-crawler": "^3.4|^4.3", + "symfony/expression-language": "^3.4|^4.3", + "symfony/finder": "^3.4|^4.3", "symfony/monolog-bridge": "^3.0|^4.0", "symfony/monolog-bundle": "^3.2", "symfony/phpunit-bridge": "^3.4.19|^4.1.8", "symfony/psr-http-message-bridge": "^1.1", - "symfony/security-bundle": "^3.4|^4.2", - "symfony/twig-bundle": "^3.4|^4.2", - "symfony/yaml": "^3.4|^4.2", + "symfony/security-bundle": "^3.4|^4.3", + "symfony/twig-bundle": "^3.4|^4.3", + "symfony/yaml": "^3.4|^4.3", "twig/twig": "~1.12|~2.0" }, "suggest": { @@ -1915,7 +1915,7 @@ "type": "symfony-bundle", "extra": { "branch-alias": { - "dev-master": "5.3.x-dev" + "dev-master": "5.4.x-dev" } }, "autoload": { @@ -1938,7 +1938,7 @@ "annotations", "controllers" ], - "time": "2019-04-10T06:00:20+00:00" + "time": "2019-07-08T08:31:25+00:00" }, { "name": "sentry/sentry", @@ -2796,16 +2796,16 @@ }, { "name": "symfony/flex", - "version": "v1.4.1", + "version": "v1.4.4", "source": { "type": "git", "url": "https://github.com/symfony/flex.git", - "reference": "a388cacccf6e3c5e5a395e020c5aa05849ea4ccc" + "reference": "4b5d25a5538e2dc37ff84645fe7498a9a04947b3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/flex/zipball/a388cacccf6e3c5e5a395e020c5aa05849ea4ccc", - "reference": "a388cacccf6e3c5e5a395e020c5aa05849ea4ccc", + "url": "https://api.github.com/repos/symfony/flex/zipball/4b5d25a5538e2dc37ff84645fe7498a9a04947b3", + "reference": "4b5d25a5538e2dc37ff84645fe7498a9a04947b3", "shasum": "" }, "require": { @@ -2841,7 +2841,7 @@ } ], "description": "Composer plugin for Symfony", - "time": "2019-06-28T18:01:09+00:00" + "time": "2019-07-12T06:29:25+00:00" }, { "name": "symfony/form", diff --git a/config/bootstrap.php b/config/bootstrap.php new file mode 100644 index 0000000..777a3e5 --- /dev/null +++ b/config/bootstrap.php @@ -0,0 +1,19 @@ +=1.2) +if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) { + foreach ($env as $k => $v) { + $_ENV[$k] = $_ENV[$k] ?? (isset($_SERVER[$k]) && 0 !== strpos($k, 'HTTP_') ? $_SERVER[$k] : $v); + } +} 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(false))->loadEnv(dirname(__DIR__).'/.env'); +} +$_SERVER += $_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/public/index.php b/public/index.php index 10878ce..e30f90c 100644 --- a/public/index.php +++ b/public/index.php @@ -2,37 +2,25 @@ use App\Kernel; use Symfony\Component\Debug\Debug; -use Symfony\Component\Dotenv\Dotenv; use Symfony\Component\HttpFoundation\Request; -require __DIR__.'/../vendor/autoload.php'; +require dirname(__DIR__).'/config/bootstrap.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 = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)); - -if ($debug) { +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]); } -$kernel = new Kernel($env, $debug); +$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send();