Skip to content

Commit 600b27c

Browse files
authored
Fix #190: Use environment variables instead of constants (#193)
1 parent 577d981 commit 600b27c

8 files changed

Lines changed: 52 additions & 76 deletions

File tree

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
YII_ENV=
2+
YII_DEBUG=true

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
"test": "phpunit --testdox --no-interaction",
1414
"test-watch": "phpunit-watcher watch",
1515
"post-update-cmd": [
16-
"App\\Installer::postUpdate"
16+
"App\\Installer::postUpdate",
17+
"App\\Installer::copyEnvFile"
18+
],
19+
"post-create-project-cmd": [
20+
"App\\Installer::copyEnvFile"
1721
]
1822
},
1923
"require": {
@@ -23,6 +27,7 @@
2327
"psr/http-message": "^1.0",
2428
"psr/http-server-handler": "^1.0",
2529
"symfony/console": "^5.3",
30+
"vlucas/phpdotenv": "^5.3",
2631
"yiisoft/aliases": "^2.0",
2732
"yiisoft/assets": "^1.0",
2833
"yiisoft/cache": "^1.0",

preload.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Dotenv\Dotenv;
6+
7+
require_once __DIR__ . '/vendor/autoload.php';
8+
9+
$dotenv = Dotenv::createImmutable(__DIR__);
10+
$dotenv->load();
11+
12+
$_ENV['YII_ENV'] = empty($_ENV['YII_ENV']) ? null : (string)$_ENV['YII_ENV'];
13+
$_SERVER['YII_ENV'] = $_ENV['YII_ENV'];
14+
15+
$_ENV['YII_DEBUG'] = filter_var(
16+
!empty($_ENV['YII_DEBUG']) ? $_ENV['YII_DEBUG'] : true,
17+
FILTER_VALIDATE_BOOLEAN,
18+
FILTER_NULL_ON_FAILURE
19+
) ?? true;
20+
$_SERVER['YII_DEBUG'] = $_ENV['YII_DEBUG'];

psalm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
88
>
99
<projectFiles>
10-
<directory name="src" />
10+
<directory name="src"/>
1111
<file name="public/index.php"/>
12-
<file name="public/index-test.php"/>
1312
<file name="yii"/>
13+
<file name="preload.php"/>
1414
</projectFiles>
1515
</psalm>
1616

public/index-test.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

public/index.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use App\Runner\WebApplicationRunner;
66

7+
/**
8+
* @psalm-var string $_SERVER['REQUEST_URI']
9+
*/
10+
711
// PHP built-in server routing.
812
if (PHP_SAPI === 'cli-server') {
913
// Serve static files as is.
@@ -17,21 +21,15 @@
1721
$_SERVER['SCRIPT_NAME'] = '/index.php';
1822
}
1923

20-
require_once dirname(__DIR__) . '/vendor/autoload.php';
21-
22-
/**
23-
* Set debug value for web application runner, for default its `true` add additionally the validation of the
24-
* container-di configurations (debug mode).
25-
*/
26-
define('YII_DEBUG', getenv('YII_DEBUG') ? (bool)getenv('YII_DEBUG') : true);
24+
require_once dirname(__DIR__) . '/preload.php';
2725

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

3533
// Run web application runner
36-
$runner = new WebApplicationRunner(YII_DEBUG, YII_ENV);
34+
$runner = new WebApplicationRunner($_ENV['YII_DEBUG'], $_ENV['YII_ENV']);
3735
$runner->run();

src/Installer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ private static function chmodRecursive(string $path, int $mode): void
3333
chmod($item, $mode);
3434
}
3535
}
36+
37+
public static function copyEnvFile(): void
38+
{
39+
if (!file_exists('.env')) {
40+
copy('.env.example', '.env');
41+
}
42+
}
3643
}

yii

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,8 @@ declare(strict_types=1);
55

66
use App\Runner\ConsoleApplicationRunner;
77

8-
require_once __DIR__ . '/vendor/autoload.php';
9-
10-
/**
11-
* Set debug value for console application runner, for default its `true` add additionally the validation of the
12-
* container-di configurations (debug mode).
13-
*/
14-
define('YII_DEBUG', getenv('YII_DEBUG') ? (bool)getenv('YII_DEBUG') : true);
15-
16-
/**
17-
* Set environment value for web application runner, for default its `null`.
18-
*
19-
* @link https://github.com/yiisoft/config#environments
20-
*/
21-
define('YII_ENV', getenv('YII_ENV') ?: null);
8+
require_once __DIR__ . '/preload.php';
229

2310
// Run console application runner
24-
$consoleRunner = new ConsoleApplicationRunner(YII_DEBUG, YII_ENV);
25-
$consoleRunner->run();
11+
$runner = new ConsoleApplicationRunner($_ENV['YII_DEBUG'], $_ENV['YII_ENV']);
12+
$runner->run();

0 commit comments

Comments
 (0)