From a628fc7b8ac078badaac18bec2670b3086552850 Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Wed, 18 Nov 2026 18:53:11 -0800 Subject: [PATCH] refactor(bootstrap): register the autoloader in the legacy bootstrap The classes this bootstrap loads depend on Composer packages and on the PSR-4 classes in src/: SmartyPage extends a Smarty class and SmartyRenderer implements a namespaced interface. Most web entry points include the bootstrap without including vendor/autoload.php first. That only worked because SmartyPage.php requires the autoloader itself and is included before the first class that needs it. Reordering the includes, or removing Smarty later, would have turned that into a fatal error on those entry points. Require the autoloader at the top of the bootstrap, so the dependency is stated where it exists. Nothing changes for current requests. The test loads the bootstrap in a separate PHP process without an autoloader, and checks that the autoloader is the first file the bootstrap includes. Assisted-by: Claude:claude-fable-5-2 --- lib/Common/namespace.php | 6 ++ tests/lib/Common/NamespaceBootstrapTest.php | 79 +++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 tests/lib/Common/NamespaceBootstrapTest.php diff --git a/lib/Common/namespace.php b/lib/Common/namespace.php index 95f95ce2a5..2f46dc7382 100644 --- a/lib/Common/namespace.php +++ b/lib/Common/namespace.php @@ -1,5 +1,11 @@ loadBootstrap(); + + $this->assertTrue($result['smartyPage'], 'SmartyPage extends a Composer class'); + $this->assertTrue($result['smartyRenderer'], 'SmartyRenderer implements a PSR-4 interface'); + } + + /** + * Otherwise the bootstrap only works while one of the files it includes + * happens to register the autoloader before the first class needs it. + */ + public function testRegistersTheAutoloaderBeforeIncludingAnythingElse(): void + { + $includedFiles = $this->loadBootstrap()['includedFiles']; + $root = $this->rootDirectory(); + + $bootstrap = array_search($root . self::BOOTSTRAP, $includedFiles, true); + + $this->assertIsInt($bootstrap, self::BOOTSTRAP . ' was not included'); + $this->assertSame($root . self::AUTOLOADER, $includedFiles[$bootstrap + 1]); + } + + /** + * @return array{includedFiles: string[], smartyPage: bool, smartyRenderer: bool} + */ + private function loadBootstrap(): array + { + $code = 'define("ROOT_DIR", ' . var_export($this->rootDirectory(), true) . ');' + . 'require ROOT_DIR . ' . var_export(self::BOOTSTRAP, true) . ';' + . 'echo json_encode([' + . '"includedFiles" => get_included_files(),' + . '"smartyPage" => class_exists("SmartyPage", false),' + . '"smartyRenderer" => class_exists("SmartyRenderer", false),' + . ']);'; + + $process = proc_open( + command: [PHP_BINARY, '-d', 'display_errors=stderr', '-d', 'error_reporting=E_ALL', '-r', $code], + descriptor_spec: [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], + pipes: $pipes, + ); + $this->assertIsResource($process, 'Failed to start PHP subprocess'); + + $stdout = stream_get_contents($pipes[1]); + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + $exitCode = proc_close($process); + + $this->assertSame(0, $exitCode, "The bootstrap exited non-zero. Output:\n" . $stdout . $stderr); + $this->assertSame('', $stderr, 'The bootstrap wrote to stderr'); + + return json_decode($stdout, associative: true, flags: JSON_THROW_ON_ERROR); + } + + private function rootDirectory(): string + { + return dirname(__DIR__, 3) . '/'; + } +}