remove usage of getenv from codebase (#1861)

This commit is contained in:
Kevin Papst
2020-08-02 01:02:04 +02:00
committed by GitHub
parent 6f8c0e3cb6
commit 476e6ca150
14 changed files with 122 additions and 123 deletions

View File

@@ -28,14 +28,16 @@ class CreateReleaseCommand extends Command
/**
* @var string
*/
protected $rootDir = '';
private $rootDir = '';
/**
* @param string $projectDirectory
* @var string
*/
public function __construct(string $projectDirectory)
private $environment;
public function __construct(string $projectDirectory, string $kernelEnvironment)
{
$this->rootDir = realpath($projectDirectory);
$this->environment = $kernelEnvironment;
parent::__construct();
}
@@ -51,14 +53,16 @@ class CreateReleaseCommand extends Command
->addOption('directory', null, InputOption::VALUE_OPTIONAL, 'Directory where the release package will be stored', '/tmp/')
->addOption('release', null, InputOption::VALUE_OPTIONAL, 'The version that should be zipped', Constants::VERSION)
;
}
/*
* Hide this command in production.
* Maybe it should be de-activated completely?!
*/
if (getenv('APP_ENV') === 'prod') {
$this->setHidden(true);
}
/**
* Make sure that this command CANNOT be executed in production.
*
* @return bool
*/
public function isEnabled()
{
return $this->environment !== 'prod';
}
/**
@@ -70,12 +74,6 @@ class CreateReleaseCommand extends Command
{
$io = new SymfonyStyle($input, $output);
if (getenv('APP_ENV') === 'prod') {
$io->error('kimai:create-release is not allowed in production');
return -2;
}
$directory = $input->getOption('directory');
if ($directory[0] === '/') {

View File

@@ -29,6 +29,17 @@ use Symfony\Component\Console\Style\SymfonyStyle;
*/
class ResetCommand extends Command
{
/**
* @var string
*/
private $environment;
public function __construct(string $kernelEnvironment)
{
$this->environment = $kernelEnvironment;
parent::__construct();
}
/**
* {@inheritdoc}
*/
@@ -55,7 +66,7 @@ EOT
*/
public function isEnabled()
{
return getenv('APP_ENV') !== 'prod';
return $this->environment !== 'prod';
}
/**

View File

@@ -11,14 +11,22 @@ namespace App\Configuration;
class MailConfiguration
{
/**
* @var string
*/
private $mailFrom;
public function __construct(string $mailFrom)
{
$this->mailFrom = $mailFrom;
}
public function getFromAddress(): ?string
{
$from = getenv('MAILER_FROM');
if ($from === false || empty($from)) {
if (empty($this->mailFrom)) {
return null;
}
return $from;
return $this->mailFrom;
}
}

View File

@@ -49,10 +49,15 @@ class DoctorController extends AbstractController
* @var string
*/
private $projectDirectory;
/**
* @var string
*/
private $environment;
public function __construct(string $projectDirectory)
public function __construct(string $projectDirectory, string $kernelEnvironment)
{
$this->projectDirectory = $projectDirectory;
$this->environment = $kernelEnvironment;
}
/**
@@ -90,7 +95,7 @@ class DoctorController extends AbstractController
return $this->render('doctor/index.html.twig', array_merge(
[
'modules' => get_loaded_extensions(),
'dotenv' => $this->getEnvVars(),
'environment' => $this->environment,
'info' => $this->getPhpInfo(),
'settings' => $this->getIniSettings(),
'extensions' => $this->getLoadedExtensions(),
@@ -124,36 +129,26 @@ class DoctorController extends AbstractController
return $results;
}
private function getLogSize()
private function getLogSize(): int
{
$logfileName = 'var/log/' . getenv('APP_ENV') . '.log';
$logfile = $this->projectDirectory . '/' . $logfileName;
$logfile = $this->getLogFilename();
return filesize($logfile);
return file_exists($logfile) ? filesize($logfile) : 0;
}
private function getLogFilename(): string
{
// why is this check here ???
if (!\in_array(getenv('APP_ENV'), ['test', 'dev', 'prod'])) {
throw new \RuntimeException('Unsupported log environment');
}
$logfileName = 'var/log/' . getenv('APP_ENV') . '.log';
$logfileName = 'var/log/' . $this->environment . '.log';
return $this->projectDirectory . '/' . $logfileName;
}
private function getLog(int $lines = 100)
private function getLog(int $lines = 100): array
{
try {
$logfile = $this->getLogFilename();
} catch (\Exception $ex) {
return ['ATTENTION: ' . $ex->getMessage()];
}
$logfile = $this->getLogFilename();
if (!file_exists($logfile)) {
return ['ATTENTION: Missing logfile'];
return ['Missing logfile'];
}
if (!is_readable($logfile)) {
@@ -163,7 +158,7 @@ class DoctorController extends AbstractController
$file = new \SplFileObject($logfile, 'r');
if ($file->getSize() === 0) {
return ['Empty log'];
return ['Empty logfile'];
}
$file->seek($file->getSize());
@@ -173,8 +168,6 @@ class DoctorController extends AbstractController
}
$iterator = new \LimitIterator($file, $last_line - $lines, $last_line);
$result = [];
try {
$result = iterator_to_array($iterator);
} catch (\Exception $ex) {
@@ -182,7 +175,7 @@ class DoctorController extends AbstractController
}
if (!is_writable($logfile)) {
$result[] = 'ATTENTION: Cannot write log file';
$result[] = 'ATTENTION: Logfile is not writable';
}
return $result;
@@ -210,14 +203,6 @@ class DoctorController extends AbstractController
return $results;
}
private function getEnvVars()
{
return [
'APP_ENV' => getenv('APP_ENV'),
'CORS_ALLOW_ORIGIN' => getenv('CORS_ALLOW_ORIGIN'),
];
}
private function getIniSettings()
{
$ini = [

View File

@@ -21,7 +21,7 @@ class DoctrineCompilerPass implements CompilerPassInterface
/**
* @var string[]
*/
protected $allowedEngines = [
private $allowedEngines = [
'mysql',
'sqlite'
];
@@ -33,20 +33,32 @@ class DoctrineCompilerPass implements CompilerPassInterface
protected function findEngine()
{
$engine = null;
$databaseUrl = null;
if (null === $engine) {
$dbConfig = explode('://', getenv('DATABASE_URL'));
$engine = $dbConfig['0'] ?: null;
if (null === $databaseUrl && isset($_ENV['DATABASE_URL'])) {
$databaseUrl = $_ENV['DATABASE_URL'];
}
if (null === $databaseUrl && isset($_SERVER['DATABASE_URL'])) {
$databaseUrl = $_SERVER['DATABASE_URL'];
}
if (null === $databaseUrl && (false !== $envDbUrl = getenv('DATABASE_URL'))) {
$databaseUrl = $envDbUrl;
}
if (null !== $databaseUrl) {
$urlParts = explode('://', $databaseUrl);
$engine = $urlParts[0] ?: null;
}
if (null === $engine) {
$engine = getenv('DATABASE_ENGINE');
}
if (false === $engine) {
if (empty($engine)) {
throw new \Exception(
'Could not detect database engine. Please set the environment config DATABASE_ENGINE ' .
'to one of: "' . implode(', ', $this->allowedEngines) . '" in your .env file, e.g. DATABASE_ENGINE=sqlite'
'Could not detect database engine, make sure DATABASE_URL is available from $_SERVER or $_ENV. Check your .env file.'
);
}