Skip to content

Commit 8a27257

Browse files
committed
fix(envlite): address PR #35 review feedback
- Keep within the spec's PHP 7.4 floor: replace str_starts_with / str_contains / str_ends_with calls in envlite.php and the tests with strpos/substr equivalents. The preflight gates on PHP_VERSION_ID >= 70400, but the rest of the file used PHP 8.0+ helpers and would fatal before reaching preflight. - Convert phase 5-8 failures into the spec's diagnostic format. Phases throw RuntimeException on SHA256 mismatch, missing placeholders, and I/O errors; init now wraps each phase in envlite_init_phase_guard, which logs `envlite init: phase N: <cause>` and exits 1. - Handle file_get_contents() returning false in phases 5/6/7. Required reads (db.copy, sample configs) raise a structured `phase N: cannot read <path>` failure; optional current-file reads short-circuit cleanly so the owned_drifted branch never hashes null. The observe_ht_sqlite step skips on read failure rather than recording the empty-string hash. - Align preflight npm floor with package.json engines (>= 10.2.3 vs the previous 10.2.0) so preflight catches what `npm ci` would later. Spec text updated to match. - Correct the plan note that claimed no edits to package.json or .gitignore — both are touched in this branch.
1 parent e438771 commit 8a27257

10 files changed

Lines changed: 108 additions & 51 deletions

File tree

‎docs/superpowers/plans/2026-05-08-envlite.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838

3939
`envlite.php` will grow to ~700 lines. The single-file constraint comes from the spec; do not split it.
4040

41-
**Modified by this plan:** None. envlite is self-contained; no edits to `composer.json`, `package.json`, `phpunit.xml.dist`, or `.gitignore`.
41+
**Modified by this plan:**
42+
43+
- `package.json` — adds an `envlite` npm script (convenience wrapper around `php tools/local-env/envlite.php`).
44+
- `.gitignore` — ignores the `/.envlite/` state directory.
45+
46+
No edits to `composer.json`, `phpunit.xml.dist`, or `wp-config-sample.php`/`wp-tests-config-sample.php` (envlite reads the samples and writes adjacent files in their own paths).
4247

4348
---
4449

‎plans/ENVLITE_SPECIFICATION.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ charter.
1818
`pdo_sqlite`, `sqlite3`, `openssl`, `simplexml`, `zip`, and `hash`
1919
extensions loaded. Phase 0 verifies the full set; the brief here
2020
just names the unavoidable ones.
21-
- host `node` ≥ 20.10, `npm` ≥ 10.2.
21+
- host `node` ≥ 20.10, `npm` ≥ 10.2.3 (matching `package.json` `engines`).
2222
- host `composer` ≥ 2.
2323
- the SQLite Database Integration plugin from wordpress.org, pinned by
2424
SHA256: `44be096a14ebcea424b5e4bf764436ec85fb067f74ab47822c4c5346df21591e`.
@@ -167,7 +167,9 @@ assumptions. Cheap to run and informative on failure.
167167

168168
`hash` is non-disable-able since PHP 7.4 and is not checked.
169169
4. `node`, `npm`, and `composer` are present and meet minimum versions:
170-
`node` ≥ 20.10, `npm` ≥ 10.2, `composer` ≥ 2. Each is verified by a
170+
`node` ≥ 20.10, `npm` ≥ 10.2.3, `composer` ≥ 2. The `npm` floor matches
171+
`package.json`'s `engines.npm` so preflight catches the same constraint
172+
`npm ci` would otherwise hit later. Each is verified by a
171173
single `proc_open` call passing the binary as a command **array**
172174
with its version flag — `['node', '--version']`, `['npm', '--version']`,
173175
`['composer', '--version']` — and reading stdout. Passing an array

‎tools/local-env/envlite.php‎

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ function envlite_path_relative_to(string $root, string $abs): string {
3939
$root = rtrim(envlite_path_to_posix($root), '/');
4040
$abs = envlite_path_to_posix($abs);
4141
if ($abs === $root) { return ''; }
42-
if (str_starts_with($abs, $root . '/')) {
43-
return substr($abs, strlen($root) + 1);
42+
$prefix = $root . '/';
43+
if (substr($abs, 0, strlen($prefix)) === $prefix) {
44+
return substr($abs, strlen($prefix));
4445
}
4546
throw new \InvalidArgumentException("path outside repo root: $abs");
4647
}
@@ -301,7 +302,7 @@ function envlite_phase0_run(string $repoRoot): void {
301302
}
302303
$tools = [
303304
['node', ['node', '--version'], [20, 10, 0]],
304-
['npm', ['npm', '--version'], [10, 2, 0]],
305+
['npm', ['npm', '--version'], [10, 2, 3]],
305306
['composer', ['composer', '--version'], [2, 0, 0]],
306307
];
307308
foreach ($tools as [$name, $cmd, $min]) {
@@ -437,8 +438,8 @@ function envlite_phase5_verify_sha256(string $path, string $expected): void {
437438
}
438439

439440
function envlite_phase5_assert_placeholder(string $dbCopyPath): void {
440-
$bytes = file_get_contents($dbCopyPath);
441-
if ($bytes === false || !str_contains($bytes, ENVLITE_SQLITE_PLACEHOLDER)) {
441+
$bytes = @file_get_contents($dbCopyPath);
442+
if ($bytes === false || strpos($bytes, ENVLITE_SQLITE_PLACEHOLDER) === false) {
442443
throw new \RuntimeException(
443444
"tripwire: " . ENVLITE_SQLITE_PLACEHOLDER . " placeholder missing from $dbCopyPath; spec assumption broken"
444445
);
@@ -479,15 +480,24 @@ function envlite_phase5_install(string $repoRoot, bool $force): void {
479480

480481
// Step 5: copy db.copy → db.php with manifest contract.
481482
if (!is_file($dbCopy)) {
482-
throw new \RuntimeException("db.copy missing at $dbCopy after extraction");
483+
throw new \RuntimeException("phase 5: db.copy missing at $dbCopy after extraction");
484+
}
485+
$dbBytes = @file_get_contents($dbCopy);
486+
if ($dbBytes === false) {
487+
throw new \RuntimeException("phase 5: cannot read $dbCopy");
483488
}
484-
$dbBytes = file_get_contents($dbCopy);
485489
$dbPhpAbs = "$repoRoot/$dbPhpRel";
486-
$current = is_file($dbPhpAbs) ? file_get_contents($dbPhpAbs) : null;
490+
$current = null;
491+
if (is_file($dbPhpAbs)) {
492+
$current = @file_get_contents($dbPhpAbs);
493+
if ($current === false) {
494+
throw new \RuntimeException("phase 5: cannot read $dbPhpAbs");
495+
}
496+
}
487497
$ownership = envlite_ownership($manifest, $dbPhpRel, $current);
488498
if ($ownership === 'owned_drifted') {
489499
$rec = $manifest[$dbPhpRel];
490-
$cur = hash('sha256', $current);
500+
$cur = $current !== null ? hash('sha256', $current) : null;
491501
envlite_prompt_or_abort($force, 'init', 'overwrite drifted file', $dbPhpRel, $rec, $cur);
492502
} elseif ($ownership === 'unowned') {
493503
envlite_prompt_or_abort($force, 'init', 'overwrite unowned file', $dbPhpRel, null, null);
@@ -513,7 +523,7 @@ function envlite_phase6_render(string $sample): string {
513523
}
514524
$out = strtr($sample, $replacements);
515525
foreach (array_keys($replacements) as $placeholder) {
516-
if (str_contains($out, $placeholder)) {
526+
if (strpos($out, $placeholder) !== false) {
517527
throw new \RuntimeException("phase 6: placeholder '$placeholder' still present after substitution");
518528
}
519529
}
@@ -525,14 +535,24 @@ function envlite_phase6_install(string $repoRoot, bool $force): void {
525535
$outRel = 'wp-tests-config.php';
526536
$outAbs = "$repoRoot/$outRel";
527537

528-
$sample = file_get_contents($samplePath);
538+
$sample = @file_get_contents($samplePath);
539+
if ($sample === false) {
540+
throw new \RuntimeException("phase 6: cannot read $samplePath");
541+
}
529542
$rendered = envlite_phase6_render($sample);
530543

531544
$manifest = envlite_manifest_load($repoRoot);
532-
$current = is_file($outAbs) ? file_get_contents($outAbs) : null;
545+
$current = null;
546+
if (is_file($outAbs)) {
547+
$current = @file_get_contents($outAbs);
548+
if ($current === false) {
549+
throw new \RuntimeException("phase 6: cannot read $outAbs");
550+
}
551+
}
533552
$ownership = envlite_ownership($manifest, $outRel, $current);
534553
if ($ownership === 'owned_drifted') {
535-
envlite_prompt_or_abort($force, 'init', 'overwrite drifted file', $outRel, $manifest[$outRel], hash('sha256', $current));
554+
$cur = $current !== null ? hash('sha256', $current) : null;
555+
envlite_prompt_or_abort($force, 'init', 'overwrite drifted file', $outRel, $manifest[$outRel], $cur);
536556
} elseif ($ownership === 'unowned') {
537557
envlite_prompt_or_abort($force, 'init', 'overwrite unowned file', $outRel, null, null);
538558
}
@@ -582,7 +602,7 @@ function envlite_phase7_fetch_salts(): ?string {
582602
try {
583603
$bytes = envlite_http_get(ENVLITE_SALT_URL, 5);
584604
// Sanity: must contain 8 define() lines and the keys we care about.
585-
if (substr_count($bytes, "define(") < 8 || !str_contains($bytes, 'NONCE_SALT')) {
605+
if (substr_count($bytes, "define(") < 8 || strpos($bytes, 'NONCE_SALT') === false) {
586606
return null;
587607
}
588608
return rtrim($bytes, "\n");
@@ -597,15 +617,25 @@ function envlite_phase7_install(string $repoRoot, int $port, bool $force): void
597617
$outRel = 'src/wp-config.php';
598618
$outAbs = "$repoRoot/$outRel";
599619

600-
$sample = file_get_contents($samplePath);
620+
$sample = @file_get_contents($samplePath);
621+
if ($sample === false) {
622+
throw new \RuntimeException("phase 7: cannot read $samplePath");
623+
}
601624
$salts = envlite_phase7_fetch_salts();
602625
$rendered = envlite_phase7_render($sample, $port, $salts);
603626

604627
$manifest = envlite_manifest_load($repoRoot);
605-
$current = is_file($outAbs) ? file_get_contents($outAbs) : null;
628+
$current = null;
629+
if (is_file($outAbs)) {
630+
$current = @file_get_contents($outAbs);
631+
if ($current === false) {
632+
throw new \RuntimeException("phase 7: cannot read $outAbs");
633+
}
634+
}
606635
$ownership = envlite_ownership($manifest, $outRel, $current);
607636
if ($ownership === 'owned_drifted') {
608-
envlite_prompt_or_abort($force, 'init', 'overwrite drifted file', $outRel, $manifest[$outRel], hash('sha256', $current));
637+
$cur = $current !== null ? hash('sha256', $current) : null;
638+
envlite_prompt_or_abort($force, 'init', 'overwrite drifted file', $outRel, $manifest[$outRel], $cur);
609639
} elseif ($ownership === 'unowned') {
610640
envlite_prompt_or_abort($force, 'init', 'overwrite unowned file', $outRel, null, null);
611641
}
@@ -706,29 +736,49 @@ function envlite_cmd_init(array $args, bool $force): int {
706736
// Phase 4: composer install
707737
envlite_phase4_composer_install($repoRoot);
708738

709-
// Phase 5: SQLite drop-in (must precede 6 and 7)
710-
envlite_phase5_install($repoRoot, $force);
711-
712-
// Phase 6: wp-tests-config.php
713-
envlite_phase6_install($repoRoot, $force);
714-
715-
// Phase 7: src/wp-config.php (consumes port)
716-
envlite_phase7_install($repoRoot, $resolvedPort, $force);
717-
718-
// Phase 8: router.php
719-
envlite_phase8_install($repoRoot, $force);
739+
// Phases 5-8 throw RuntimeException for diagnostic failures (e.g.,
740+
// SHA256 mismatch, missing placeholders, I/O errors). Convert each into
741+
// the spec's `envlite init: phase N: <cause>` line + exit 1.
742+
$phases = [
743+
[5, function () use ($repoRoot, $force) { envlite_phase5_install($repoRoot, $force); }],
744+
[6, function () use ($repoRoot, $force) { envlite_phase6_install($repoRoot, $force); }],
745+
[7, function () use ($repoRoot, $resolvedPort, $force) { envlite_phase7_install($repoRoot, $resolvedPort, $force); }],
746+
[8, function () use ($repoRoot, $force) { envlite_phase8_install($repoRoot, $force); }],
747+
];
748+
foreach ($phases as [$n, $fn]) {
749+
$rc = envlite_init_phase_guard($n, $fn);
750+
if ($rc !== 0) { return $rc; }
751+
}
720752

721753
fwrite(STDERR, "envlite init: ok (port $resolvedPort)\n");
722754
return 0;
723755
}
724756

757+
function envlite_init_phase_guard(int $n, callable $fn): int {
758+
try {
759+
$fn();
760+
return 0;
761+
} catch (\Throwable $e) {
762+
$msg = $e->getMessage();
763+
$prefix = "phase $n: ";
764+
if (strpos($msg, $prefix) !== 0) {
765+
$msg = $prefix . $msg;
766+
}
767+
envlite_log('init', $msg);
768+
return 1;
769+
}
770+
}
771+
725772
function envlite_observe_ht_sqlite(string $repoRoot): void {
726773
$rel = 'src/wp-content/database/.ht.sqlite';
727774
$abs = "$repoRoot/$rel";
728775
if (!is_file($abs)) { return; }
729776
$manifest = envlite_manifest_load($repoRoot);
730777
if (isset($manifest[$rel])) { return; }
731-
$bytes = file_get_contents($abs);
778+
$bytes = @file_get_contents($abs);
779+
// Read failure: leave the file unrecorded rather than capturing the
780+
// empty-string hash. clean will treat it as user-owned, which is correct.
781+
if ($bytes === false) { return; }
732782
$manifest[$rel] = hash('sha256', $bytes);
733783
envlite_manifest_save($repoRoot, $manifest);
734784
}

‎tools/local-env/tests/harness.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function envlite_test_run(string $dir): int {
2323
$after = get_defined_functions()['user'];
2424
$tests = array_values(array_filter(
2525
array_diff($after, $before),
26-
fn($fn) => str_starts_with($fn, 'test_')
26+
fn($fn) => substr($fn, 0, 5) === 'test_'
2727
));
2828
sort($tests);
2929
$failures = 0;

‎tools/local-env/tests/test_manifest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function test_manifest_save_emits_lf_only() {
3030
envlite_manifest_save($dir, ['router.php' => str_repeat('a', 64)]);
3131
$bytes = file_get_contents($dir . '/.envlite/manifest');
3232
envlite_assert(strpos($bytes, "\r") === false, 'manifest must not contain CR');
33-
envlite_assert(str_ends_with($bytes, "\n"), 'manifest must end with LF');
33+
envlite_assert(substr($bytes, -1) === "\n", 'manifest must end with LF');
3434
}
3535

3636
function test_manifest_load_skips_blank_and_malformed_lines() {

‎tools/local-env/tests/test_paths.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ function test_path_relative_to_root_throws_for_outside() {
1818
envlite_path_relative_to('/tmp/repo', '/etc/passwd');
1919
throw new \RuntimeException('expected exception');
2020
} catch (\InvalidArgumentException $e) {
21-
envlite_assert(str_contains($e->getMessage(), 'outside repo root'));
21+
envlite_assert(strpos($e->getMessage(), 'outside repo root') !== false);
2222
}
2323
}

‎tools/local-env/tests/test_phase5.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function test_phase5_verify_sha_throws_on_mismatch() {
1717
throw new \RuntimeException('expected exception');
1818
} catch (\RuntimeException $e) {
1919
unlink($tmp);
20-
envlite_assert(str_contains($e->getMessage(), 'SHA256 mismatch'));
20+
envlite_assert(strpos($e->getMessage(), 'SHA256 mismatch') !== false);
2121
}
2222
}
2323

@@ -34,6 +34,6 @@ function test_phase5_tripwire_throws_when_placeholder_missing() {
3434
envlite_phase5_assert_placeholder($dir . '/db.copy');
3535
throw new \RuntimeException('expected exception');
3636
} catch (\RuntimeException $e) {
37-
envlite_assert(str_contains($e->getMessage(), 'placeholder'));
37+
envlite_assert(strpos($e->getMessage(), 'placeholder') !== false);
3838
}
3939
}

‎tools/local-env/tests/test_phase6.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ function test_phase6_render_substitutes_placeholders() {
44
. "define( 'DB_USER', 'yourusernamehere' );\n"
55
. "define( 'DB_PASSWORD', 'yourpasswordhere' );\n";
66
$out = envlite_phase6_render($sample);
7-
envlite_assert(str_contains($out, "'wordpress_test'"));
8-
envlite_assert(str_contains($out, "'wp'"));
9-
envlite_assert(!str_contains($out, 'youremptytestdbnamehere'));
10-
envlite_assert(!str_contains($out, 'yourusernamehere'));
11-
envlite_assert(!str_contains($out, 'yourpasswordhere'));
7+
envlite_assert(strpos($out, "'wordpress_test'") !== false);
8+
envlite_assert(strpos($out, "'wp'") !== false);
9+
envlite_assert(strpos($out, 'youremptytestdbnamehere') === false);
10+
envlite_assert(strpos($out, 'yourusernamehere') === false);
11+
envlite_assert(strpos($out, 'yourpasswordhere') === false);
1212
}
1313

1414
function test_phase6_render_throws_when_placeholder_missing() {
1515
try {
1616
envlite_phase6_render("define( 'DB_NAME', 'someothername' );");
1717
throw new \RuntimeException('expected exception');
1818
} catch (\RuntimeException $e) {
19-
envlite_assert(str_contains($e->getMessage(), 'placeholder'));
19+
envlite_assert(strpos($e->getMessage(), 'placeholder') !== false);
2020
}
2121
}

‎tools/local-env/tests/test_phase7.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
function test_phase7_render_substitutes_db_constants() {
33
$sample = file_get_contents(dirname(__DIR__, 3) . '/wp-config-sample.php');
44
$out = envlite_phase7_render($sample, 8421, null);
5-
envlite_assert(str_contains($out, "'wordpress'"), 'DB_NAME substituted');
6-
envlite_assert(!str_contains($out, 'database_name_here'));
7-
envlite_assert(!str_contains($out, 'username_here'));
8-
envlite_assert(!str_contains($out, 'password_here'));
5+
envlite_assert(strpos($out, "'wordpress'") !== false, 'DB_NAME substituted');
6+
envlite_assert(strpos($out, 'database_name_here') === false);
7+
envlite_assert(strpos($out, 'username_here') === false);
8+
envlite_assert(strpos($out, 'password_here') === false);
99
}
1010

1111
function test_phase7_render_injects_wp_home_siteurl_before_marker() {
@@ -14,8 +14,8 @@ function test_phase7_render_injects_wp_home_siteurl_before_marker() {
1414
$home = "define( 'WP_HOME', 'http://127.0.0.1:8421' );";
1515
$site = "define( 'WP_SITEURL', 'http://127.0.0.1:8421' );";
1616
$marker = "/* That's all, stop editing! Happy publishing. */";
17-
envlite_assert(str_contains($out, $home));
18-
envlite_assert(str_contains($out, $site));
17+
envlite_assert(strpos($out, $home) !== false);
18+
envlite_assert(strpos($out, $site) !== false);
1919
// injection must precede the marker
2020
envlite_assert(strpos($out, $home) < strpos($out, $marker), 'WP_HOME must be before marker');
2121
envlite_assert(strpos($out, $site) < strpos($out, $marker), 'WP_SITEURL must be before marker');
@@ -32,7 +32,7 @@ function test_phase7_render_replaces_salts_when_provided() {
3232
. "define( 'LOGGED_IN_SALT', 'XYZ' );\n"
3333
. "define( 'NONCE_SALT', 'XYZ' );";
3434
$out = envlite_phase7_render($sample, 8421, $salts);
35-
envlite_assert(!str_contains($out, "'put your unique phrase here'"));
35+
envlite_assert(strpos($out, "'put your unique phrase here'") === false);
3636
envlite_assert(substr_count($out, "'XYZ'") === 8);
3737
}
3838

‎tools/local-env/tests/test_smoke.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function test_smoke_phases5_through_8_then_clean() {
4848
envlite_assert(isset($m['src/wp-content/plugins/sqlite-database-integration']));
4949

5050
// wp-config.php picked up the port.
51-
envlite_assert(str_contains(file_get_contents("$dir/src/wp-config.php"), 'http://127.0.0.1:8421'));
51+
envlite_assert(strpos(file_get_contents("$dir/src/wp-config.php"), 'http://127.0.0.1:8421') !== false);
5252

5353
// Now drive clean (force, no TTY).
5454
$paths = envlite_clean_collect($m);

0 commit comments

Comments
 (0)