Browse Source

add .env file to support cli

main
ghost 1 year ago
parent
commit
a9f8fade7d
  1. 18
      .gitignore
  2. 3
      example/environment/nginx
  3. 29
      src/config/bootstrap.php

18
.gitignore vendored

@ -1,19 +1,15 @@
/.vscode /.vscode/
/vendor /vendor/
/database/yggtracker.mwb.bak /database/yggtracker.mwb.bak
/src/config/*.php /src/public/api/
!/src/config/bootstrap.php
/src/public/api/manifest.json /src/config/*
/src/public/api/users.json !/src/config/bootstrap.php
/src/public/api/magnets.json !/src/config/nodes.php
/src/public/api/downloads.json !/src/config/trackers.php
/src/public/api/comments.json
/src/public/api/stars.json
/src/public/api/views.json
/composer.lock /composer.lock

3
example/environment/nginx

@ -17,9 +17,6 @@ server {
location ~ \.php$ { location ~ \.php$ {
include snippets/fastcgi-php.conf; include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock; fastcgi_pass unix:/run/php/php8.1-fpm.sock;
# default, mirror, dev, test, prod
fastcgi_param PHP_ENV default;
} }
location ~ /\. { location ~ /\. {

29
src/config/bootstrap.php

@ -4,30 +4,35 @@
declare(strict_types=1); declare(strict_types=1);
// Init environment // 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);
chmod(__DIR__ . '/.env', 0770);
} }
// Validate environment whitelist else exit (_('Could not init environment file. Please check permissions.'));
if (!in_array($_SERVER['PHP_ENV'], ['default', 'mirror', 'dev', 'test', 'prod']))
{
exit (_('Environment not supported! Check /src/config/bootstrap.php to add exception.'));
} }
// Generate configuration file if not exists define('PHP_ENV', file_get_contents(__DIR__ . '/.env'));
if (!file_exists(__DIR__ . '/env.' . $_SERVER['PHP_ENV'] . '.php') && file_exists(__DIR__ . '/../../example/environment/env.example.php'))
// 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.')); else exit (_('Could not init configuration file. Please check permissions.'));
} }
// Load environment configuration // Load environment
require_once __DIR__ . '/env.' . $_SERVER['PHP_ENV'] . '.php'; require_once __DIR__ . '/env.' . PHP_ENV . '.php';
// Local internal dependencies // Local internal dependencies
require_once __DIR__ . '/../library/database.php'; require_once __DIR__ . '/../library/database.php';

Loading…
Cancel
Save