Skip to content

Commit

Permalink
Use env variables instead of constants (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Oct 6, 2021
1 parent 933c28f commit 991c9d1
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .env.example
@@ -0,0 +1,2 @@
YII_ENV=
YII_DEBUG=true
7 changes: 6 additions & 1 deletion composer.json
Expand Up @@ -33,6 +33,7 @@
"psr/log": "^1.1",
"spiral/database": "^2.8",
"symfony/console": "^5.2",
"vlucas/phpdotenv": "^5.3",
"yiisoft/access": "^1.0",
"yiisoft/aliases": "^2.0",
"yiisoft/assets": "^1.0",
Expand Down Expand Up @@ -102,7 +103,11 @@
"scripts": {
"serve": "./yii serve",
"post-update-cmd": [
"App\\Installer::postUpdate"
"App\\Installer::postUpdate",
"App\\Installer::copyEnvFile"
],
"post-create-project-cmd": [
"App\\Installer::copyEnvFile"
],
"test": "phpunit --testdox --no-interaction",
"test-watch": "phpunit-watcher watch"
Expand Down
20 changes: 20 additions & 0 deletions preload.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Dotenv\Dotenv;

require_once __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$_ENV['YII_ENV'] = empty($_ENV['YII_ENV']) ? null : (string)$_ENV['YII_ENV'];
$_SERVER['YII_ENV'] = $_ENV['YII_ENV'];

$_ENV['YII_DEBUG'] = filter_var(
!empty($_ENV['YII_DEBUG']) ? $_ENV['YII_DEBUG'] : true,
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE
) ?? true;
$_SERVER['YII_DEBUG'] = $_ENV['YII_DEBUG'];
3 changes: 3 additions & 0 deletions psalm.xml
Expand Up @@ -8,5 +8,8 @@
>
<projectFiles>
<directory name="src" />
<file name="public/index.php"/>
<file name="yii"/>
<file name="preload.php"/>
</projectFiles>
</psalm>
42 changes: 0 additions & 42 deletions public/index-test.php

This file was deleted.

27 changes: 13 additions & 14 deletions public/index.php
Expand Up @@ -4,9 +4,14 @@

use App\Runner\WebApplicationRunner;

/**
* @psalm-var string $_SERVER['REQUEST_URI']
*/

// PHP built-in server routing.
if (PHP_SAPI === 'cli-server') {
// Serve static files as is.
/** @psalm-suppress MixedArgument */
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (is_file(__DIR__ . $path)) {
return false;
Expand All @@ -16,21 +21,15 @@
$_SERVER['SCRIPT_NAME'] = '/index.php';
}

require_once dirname(__DIR__) . '/vendor/autoload.php';

/**
* Set debug value for web application runner, for default its `true` add additionally the validation of the
* container-di configurations (debug mode).
*/
define('YII_DEBUG', getenv('YII_DEBUG') ?: true);
require_once dirname(__DIR__) . '/preload.php';

/**
* Set environment value for web application runner, for default its `null`.
*
* @link https://github.com/yiisoft/config#environments
*/
define('YII_ENV', getenv('YII_ENV') ?: null);
if ($_ENV['YII_ENV'] === 'test') {
$c3 = dirname(__DIR__) . '/c3.php';
if (file_exists($c3)) {
require_once $c3;
}
}

// Run web application runner
$runner = new WebApplicationRunner(YII_DEBUG, YII_ENV);
$runner = new WebApplicationRunner($_ENV['YII_DEBUG'], $_ENV['YII_ENV']);
$runner->run();
7 changes: 7 additions & 0 deletions src/Installer.php
Expand Up @@ -30,4 +30,11 @@ private static function chmodRecursive(string $path, int $mode): void
chmod($item, $mode);
}
}

public static function copyEnvFile(): void
{
if (!file_exists('.env')) {
copy('.env.example', '.env');
}
}
}
19 changes: 3 additions & 16 deletions yii
Expand Up @@ -5,21 +5,8 @@ declare(strict_types=1);

use App\Runner\ConsoleApplicationRunner;

require_once __DIR__ . '/vendor/autoload.php';

/**
* Set debug value for console application runner, for default its `true` add additionally the validation of the
* container-di configurations (debug mode).
*/
define('YII_DEBUG', getenv('YII_DEBUG') ?: true);

/**
* Set environment value for web application runner, for default its `null`.
*
* @link https://github.com/yiisoft/config#environments
*/
define('YII_ENV', getenv('YII_ENV') ?: null);
require_once __DIR__ . '/preload.php';

// Run console application runner
$consoleRunner = new ConsoleApplicationRunner(YII_DEBUG, YII_ENV);
$consoleRunner->run();
$runner = new ConsoleApplicationRunner($_ENV['YII_DEBUG'], $_ENV['YII_ENV']);
$runner->run();

0 comments on commit 991c9d1

Please sign in to comment.