From a9f8fade7d2448dbbbdf0be90e9ff71d80c1c63a Mon Sep 17 00:00:00 2001 From: ghost Date: Thu, 14 Sep 2023 00:09:27 +0300 Subject: [PATCH] add .env file to support cli --- .gitignore | 18 +++++++----------- example/environment/nginx | 3 --- src/config/bootstrap.php | 31 ++++++++++++++++++------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 83ed2f2..0a20b93 100644 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,15 @@ -/.vscode +/.vscode/ -/vendor +/vendor/ /database/yggtracker.mwb.bak -/src/config/*.php -!/src/config/bootstrap.php +/src/public/api/ -/src/public/api/manifest.json -/src/public/api/users.json -/src/public/api/magnets.json -/src/public/api/downloads.json -/src/public/api/comments.json -/src/public/api/stars.json -/src/public/api/views.json +/src/config/* +!/src/config/bootstrap.php +!/src/config/nodes.php +!/src/config/trackers.php /composer.lock diff --git a/example/environment/nginx b/example/environment/nginx index 6372b45..4bfd1c5 100644 --- a/example/environment/nginx +++ b/example/environment/nginx @@ -17,9 +17,6 @@ server { location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; - - # default, mirror, dev, test, prod - fastcgi_param PHP_ENV default; } location ~ /\. { diff --git a/src/config/bootstrap.php b/src/config/bootstrap.php index ebdad3a..bf091c7 100644 --- a/src/config/bootstrap.php +++ b/src/config/bootstrap.php @@ -4,30 +4,35 @@ declare(strict_types=1); // Init environment -if (empty($_SERVER['PHP_ENV'])) +if (!file_exists(__DIR__ . '/.env')) { - $_SERVER['PHP_ENV'] = 'default'; -} + if ($handle = fopen(__DIR__ . '/.env', 'w+')) + { + fwrite($handle, 'default'); + fclose($handle); -// Validate environment whitelist -if (!in_array($_SERVER['PHP_ENV'], ['default', 'mirror', 'dev', 'test', 'prod'])) -{ - exit (_('Environment not supported! Check /src/config/bootstrap.php to add exception.')); + chmod(__DIR__ . '/.env', 0770); + } + + else exit (_('Could not init environment file. Please check permissions.')); } -// Generate configuration file if not exists -if (!file_exists(__DIR__ . '/env.' . $_SERVER['PHP_ENV'] . '.php') && file_exists(__DIR__ . '/../../example/environment/env.example.php')) +define('PHP_ENV', file_get_contents(__DIR__ . '/.env')); + +// Init config +if (!file_exists(__DIR__ . '/env.' . PHP_ENV . '.php')) { - if (copy(__DIR__ . '/../../example/environment/env.example.php', __DIR__ . '/env.' . $_SERVER['PHP_ENV'] . '.php')) + if (copy(__DIR__ . '/../../example/environment/env.example.php', + __DIR__ . '/env.' . PHP_ENV . '.php')) { - chmod(__DIR__ . '/env.' . $_SERVER['PHP_ENV'] . '.php', 0770); + chmod(__DIR__ . '/env.' . PHP_ENV . '.php', 0770); } else exit (_('Could not init configuration file. Please check permissions.')); } -// Load environment configuration -require_once __DIR__ . '/env.' . $_SERVER['PHP_ENV'] . '.php'; +// Load environment +require_once __DIR__ . '/env.' . PHP_ENV . '.php'; // Local internal dependencies require_once __DIR__ . '/../library/database.php';