Skip to content

Commit 74cbff1

Browse files
committed
feat(compiler): replace hardcoded autoloader path with dynamic resolution
- Replace direct require of TYPEPHP_ROOT_PATH . '/vendor/autoload.php' with resolveComposerAutoloader() function - Add new resolveComposerAutoloader() function to locate Composer autoloader from multiple candidate paths - Support both source checkout and packaged SDK environments by checking TYPEPHP_ROOT_PATH and PHP_HOME - Implement fallback mechanism to search for vendor/autoload.php in different locations - Add detailed error reporting when autoloader is not found with specific search paths - Include installation instructions in error output when dependencies are missing - Exit with error code 1 when autoloader cannot be located
1 parent 1b88263 commit 74cbff1

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

src/compiler.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function main(int $argc, array $argv): void
3232
// in bin/bootstrap.php. The AOT compiler starts here directly and therefore
3333
// must load the dependencies packaged alongside tpc itself.
3434
if (!defined('TYPEPHP_PHP_SCRIPT_ENTRY')) {
35-
require_once TYPEPHP_ROOT_PATH . '/vendor/autoload.php';
35+
require_once resolveComposerAutoloader();
3636
}
3737

3838
$completionStatus = CompletionCommand::execute($argv);
@@ -136,6 +136,43 @@ function main(int $argc, array $argv): void
136136
}
137137
}
138138

139+
/**
140+
* Locate the Composer autoloader that ships with the compiler.
141+
*
142+
* A source checkout and a Unix-like package keep it below TYPEPHP_ROOT_PATH,
143+
* while a packaged SDK extracts it below PHP_HOME.
144+
*/
145+
function resolveComposerAutoloader(): string
146+
{
147+
$candidates = [];
148+
$roots = [TYPEPHP_ROOT_PATH, getenv('PHP_HOME') ?: null];
149+
foreach ($roots as $root) {
150+
if (!is_string($root) || $root === '') {
151+
continue;
152+
}
153+
$candidate = rtrim($root, '/\\') . '/vendor/autoload.php';
154+
if (!in_array($candidate, $candidates, true)) {
155+
$candidates[] = $candidate;
156+
}
157+
}
158+
159+
foreach ($candidates as $candidate) {
160+
if (is_file($candidate)) {
161+
return $candidate;
162+
}
163+
}
164+
165+
fwrite(STDERR, "Unable to find the Composer autoloader (vendor/autoload.php).\n");
166+
fwrite(STDERR, "Searched in:\n");
167+
foreach ($candidates as $candidate) {
168+
fwrite(STDERR, " - {$candidate}\n");
169+
}
170+
fwrite(STDERR, "\nInstall the TypePHP dependencies with Composer first:\n");
171+
fwrite(STDERR, " cd " . TYPEPHP_ROOT_PATH . " && composer install\n");
172+
fwrite(STDERR, "Or point PHP_HOME at a TypePHP installation that already contains vendor/autoload.php.\n");
173+
exit(1);
174+
}
175+
139176
function shouldCompileNativeSourceProject(array $argv): bool
140177
{
141178
foreach (array_slice($argv, 1) as $argument) {

0 commit comments

Comments
 (0)