-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-boot-env.php
More file actions
113 lines (93 loc) · 4.8 KB
/
07-boot-env.php
File metadata and controls
113 lines (93 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
/**
* Example 07: bootEnv() — Environment-Aware Cascade Loading
*
* bootEnv() loads files in order (Symfony-inspired cascade):
* 1. .env — base defaults (committed to VCS)
* 2. .env.local — local overrides (gitignored)
* 3. .env.{env} — environment-specific defaults
* 4. .env.{env}.local — environment-specific local (skipped for 'test')
*
* The environment name is resolved from:
* 1. $environmentName argument (if passed)
* 2. DotenvConfiguration::environmentName
* 3. APP_ENV variable (already loaded by step 1)
* 4. Defaults to 'dev'
*
* !! PARAMETER REFERENCE (confirmed from implementations):
*
* $dotenv->bootEnv(environmentName: ?string = null):
* - environmentName: override env name (null → auto-resolve from APP_ENV)
* - ⚠️ env = 'test' skips .env.test.local for reproducibility in CI
*
* DotenvConfiguration(environmentName: string):
* - Sets the default environment name used by bootEnv() when no arg passed.
*
* $dotenv->debug(): array
* - Returns list of [key, value, source, loadedAt] for all loaded variables.
* - Useful to trace which file provided which variable in a cascade.
*/
require_once __DIR__ . '/../vendor/autoload.php';
use KaririCode\Dotenv\Dotenv;
use KaririCode\Dotenv\Enum\LoadMode;
use KaririCode\Dotenv\ValueObject\DotenvConfiguration;
echo "═══════════════════════════════════════════════════════════\n";
echo " 07 — bootEnv() Cascade Loading\n";
echo "═══════════════════════════════════════════════════════════\n\n";
$dir = __DIR__ . '/..';
// ── Prepare cascade files ──────────────────────────────────────────────────
// .env is already created. We also have .env.local.
// Let's also create a .env.staging for demonstration:
$stagingEnv = $dir . '/.env.staging';
file_put_contents($stagingEnv, implode("\n", [
'# Staging overrides',
'APP_ENV=staging',
'APP_DEBUG=false',
'DB_HOST=staging-db.internal',
'DB_POOL_SIZE=50',
]));
// ── 1. Default cascade (env resolved from APP_ENV in .env = 'local') ───────
echo "── Default cascade (APP_ENV=local from .env) ─────────────\n";
$config = new DotenvConfiguration(loadMode: LoadMode::Overwrite);
$dotenv = new Dotenv($dir, $config);
$dotenv->bootEnv();
echo " APP_ENV : " . $dotenv->get('APP_ENV') . "\n";
echo " APP_DEBUG : " . ($dotenv->get('APP_DEBUG') ? 'true' : 'false') . "\n";
echo " DB_HOST : " . $dotenv->get('DB_HOST') . "\n";
echo " DB_POOL_SIZE: " . $dotenv->get('DB_POOL_SIZE') . "\n";
// ── 2. Explicit environment override ──────────────────────────────────────
echo "\n── Explicit staging environment ──────────────────────────\n";
$config2 = new DotenvConfiguration(loadMode: LoadMode::Overwrite);
$dotenv2 = new Dotenv($dir, $config2);
$dotenv2->bootEnv('staging');
echo " APP_ENV : " . $dotenv2->get('APP_ENV') . "\n";
echo " APP_DEBUG : " . ($dotenv2->get('APP_DEBUG') ? 'true' : 'false') . "\n";
echo " DB_HOST : " . $dotenv2->get('DB_HOST') . "\n";
echo " DB_POOL_SIZE: " . $dotenv2->get('DB_POOL_SIZE') . "\n";
// ── 3. Test environment (skips .env.test.local for reproducibility) ────────
echo "\n── Test environment (env.test.local skipped) ─────────────\n";
$testEnv = $dir . '/.env.test';
file_put_contents($testEnv, implode("\n", [
'APP_ENV=test',
'APP_DEBUG=false',
'DB_NAME=kariricode_test',
]));
$config3 = new DotenvConfiguration(loadMode: LoadMode::Overwrite);
$dotenv3 = new Dotenv($dir, $config3);
$dotenv3->bootEnv('test');
echo " APP_ENV : " . $dotenv3->get('APP_ENV') . "\n";
echo " DB_NAME : " . $dotenv3->get('DB_NAME') . "\n";
echo " (note: .env.test.local would be skipped for 'test' env)\n";
// ── 4. Debug: which files were loaded ─────────────────────────────────────
echo "\n── debug() source tracking ───────────────────────────────\n";
$report = $dotenv2->debug();
$sources = array_unique(array_column($report, 'source'));
echo " Sources loaded in staging cascade:\n";
foreach ($sources as $source) {
echo " • {$source}\n";
}
// Cleanup temp files
@unlink($stagingEnv);
@unlink($testEnv);
echo "\n✓ Example 07 completed.\n\n";