diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..dd55a2a6e --- /dev/null +++ b/.env.example @@ -0,0 +1,12 @@ +# Common params +APP_NAME=TorrentPier +APP_ENV=local +APP_DEBUG=false + +# Database credentials +DB_CONNECTION=mysql +DB_HOST=localhost +DB_PORT=3306 +DB_DATABASE=torrentpier +DB_USERNAME=root +DB_PASSWORD=secret diff --git a/common.php b/common.php index ef02e04e8..8c4b6d479 100644 --- a/common.php +++ b/common.php @@ -50,10 +50,50 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) { } require_once __DIR__ . '/vendor/autoload.php'; +/** + * Gets the value of an environment variable. Supports boolean, empty and null. + * + * @param string $key + * @param mixed $default + * @return mixed + */ +function env($key, $default = null) +{ + $value = getenv($key); + if (!$value) return value($default); + switch (strtolower($value)) { + case 'true': + case '(true)': + return true; + case 'false': + case '(false)': + return false; + case '(null)': + return null; + case '(empty)': + return ''; + } + return $value; +} + +/** + * Return the default value of the given value. + * + * @param mixed $value + * @return mixed + */ +function value($value) +{ + return $value instanceof Closure ? $value() : $value; +} + // Get initial config +if (!getenv('APP_DEBUG') && file_exists(__DIR__ . '/.env')) { + (new Symfony\Component\Dotenv\Dotenv())->load(__DIR__ . '/.env'); +} require_once __DIR__ . '/library/config.php'; -// Get local config +// Local config if (file_exists(__DIR__ . '/library/config.local.php')) { require_once __DIR__ . '/library/config.local.php'; } @@ -96,9 +136,11 @@ define('BOT_UID', -746); * Progressive error reporting */ if ($bb_cfg['bugsnag']['enabled']) { - /** @var Bugsnag\Handler $bugsnag */ - $bugsnag = Bugsnag\Client::make($bb_cfg['bugsnag']['api_key']); - Bugsnag\Handler::register($bugsnag); + if (env('APP_ENV', 'production') !== 'local') { + /** @var Bugsnag\Handler $bugsnag */ + $bugsnag = Bugsnag\Client::make($bb_cfg['bugsnag']['api_key']); + Bugsnag\Handler::register($bugsnag); + } } else { if (DBG_USER) { /** @var Whoops\Run $whoops */ diff --git a/composer.json b/composer.json index 2c6b1430e..f20bac1f0 100644 --- a/composer.json +++ b/composer.json @@ -53,6 +53,7 @@ "rych/bencode": "v1.0.0", "samdark/sitemap": "2.4.0", "swiftmailer/swiftmailer": "v6.3.0", + "symfony/dotenv": "4.*", "symfony/polyfill": "v1.27.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 5bc745750..a7ec7e232 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2ac8346651466f89508650fddb2461db", + "content-hash": "530818326ea7baa3a459b76781f0157a", "packages": [ { "name": "bugsnag/bugsnag", @@ -1173,6 +1173,75 @@ "abandoned": "symfony/mailer", "time": "2021-10-18T15:26:12+00:00" }, + { + "name": "symfony/dotenv", + "version": "v4.4.37", + "source": { + "type": "git", + "url": "https://github.com/symfony/dotenv.git", + "reference": "fcedd6d382b3afc3e1e786aa4e4fc4cf06f564cf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/dotenv/zipball/fcedd6d382b3afc3e1e786aa4e4fc4cf06f564cf", + "reference": "fcedd6d382b3afc3e1e786aa4e4fc4cf06f564cf", + "shasum": "" + }, + "require": { + "php": ">=7.1.3" + }, + "require-dev": { + "symfony/process": "^3.4.2|^4.0|^5.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Dotenv\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Registers environment variables from a .env file", + "homepage": "https://symfony.com", + "keywords": [ + "dotenv", + "env", + "environment" + ], + "support": { + "source": "https://github.com/symfony/dotenv/tree/v4.4.37" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-01-02T09:41:36+00:00" + }, { "name": "symfony/polyfill", "version": "v1.27.0", @@ -1391,5 +1460,5 @@ "php": "^7.1.3" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" } diff --git a/library/config.php b/library/config.php index 1fe4940ec..1e113d474 100644 --- a/library/config.php +++ b/library/config.php @@ -29,7 +29,14 @@ $bb_cfg['tp_release_codename'] = 'Bison'; // Настройка баз данных ['db']['srv_name'] => (array) srv_cfg; // порядок параметров srv_cfg (хост, название базы, пользователь, пароль, charset, pconnect); $bb_cfg['db'] = [ - 'db' => ['localhost', 'torrentpier', 'root', 'root', 'utf8', false], + 'db' => [ + env('DB_HOST', 'localhost'), + env('DB_DATABASE', 'torrentpier'), + env('DB_USERNAME', 'root'), + env('DB_PASSWORD', 'pass'), + 'utf8', + false + ], ]; $bb_cfg['db_alias'] = [