Composer platform configuration and platform-check execution timing
0 reputation · 02 Jun 2022, 03:00 UTC
0 reputation · 02 Jun 2022, 03:00 UTC
The goal is to guarantee that the runtime environment validation generated by Composer's platform-check feature runs before any application code is loaded, ensuring that a simulated PHP version and extensions are actually present.
When the platform setting is used to fake PHP 8.1 and certain extensions, Composer writes a platform_check.php file that is required by the autoloader. However, it is unclear whether this check is always executed when the autoloader is optimized with a class map or when the autoloader is dumped in different modes, e.g., -o vs --apcu-autoloader.
Specifically, does the platform-check script get included and executed in all autoloader variants, and can its execution be suppressed or bypassed by certain autoloader optimizations?
34775 reputation · 02 Jun 2022, 03:52 UTC
Yes, the platform-check script is executed in all autoloader variants, including those generated with -o (classmap) or --apcu-autoloader. These optimization flags change how Composer resolves class locations, but they do not modify the bootstrap sequence of the autoloader itself.
When the platform configuration is used to simulate a specific environment, Composer generates a platform_check.php file. This file is not part of the class-loading logic; instead, it is a hard-coded requirement within the autoloader's entry point. Because vendor/autoload.php is typically the first file required by an application's entry point (e.g., index.php), the platform validation occurs before any application code or third-party classes are loaded.
| Optimization Mode | Effect on Class Loading | Effect on Platform Check |
|---|---|---|
| Default | PSR-4/PSR-0 lookup | Executed via autoload.php |
-o (Classmap) |
Static array lookup | Executed via autoload.php |
--apcu-autoloader |
Cached lookup in APCu | Executed via autoload.php |
To verify that the check remains active in your specific environment, you can perform the following manual inspections:
vendor/composer/platform_check.php exists after running your install or update command.vendor/autoload.php. You should see a require statement specifically targeting the platform_check.php file.vendor/autoload.php to ensure the requirement persists:
composer dump-autoload -o
composer dump-autoload --apcu
This behavior assumes you are using Composer 2.x, where the platform-check feature was standardized. It is important to note that the platform check only validates the environment against the constraints defined in composer.json or the platform config; it does not perform a deep analysis of the actual source code requirements.
Diagnostic Note: Are you using a custom autoloader implementation or a framework that bypasses vendor/autoload.php? If so, the platform check will be skipped entirely.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.