composer / composer

Dependency Manager for PHP

Home Page:https://getcomposer.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to ignore platform check with autoloader

llaville opened this issue · comments

Hello,

I'm working on one of my project to is able to parse source code with help of https://github.com/nikic/PHP-Parser.

PHP-Parser is able to parse source of a package that requires PHP min version greater than the current platform where the code is running.

On my project, I've recently added a new feature that should be able to parse dependencies on vendor directory, based on a provided autoloader.
BTW, I can't currently analyse a project installed for (e.g) PHP 8.2 on a PHP 8.1 platform.

Reason is located on vendor/composer/autoload_real.php with unconditional require __DIR__ . '/platform_check.php';

I know that there are options [1], [2] to disable this feature on autoloader creation. But once it's created, there is no option to ignore it.

[1] https://getcomposer.org/doc/07-runtime.md#platform-check
[2] https://getcomposer.org/doc/03-cli.md#composer-ignore-platform-req-or-composer-ignore-platform-reqs

My request is : Are you agree to add a check to COMPOSER_IGNORE_PLATFORM_REQS env var to bypass such verification.

If will look like :

<?php
// autoload_real.php @generated by Composer

class ComposerAutoloaderInitbd87f8a4013e5c5d8883671cf53e0027
{

    public static function getLoader()
    {

        $ignorePlatformReqEnv = self::getEnv('COMPOSER_IGNORE_PLATFORM_REQ');
        if (!$ignorePlatformReqEnv) {
            require __DIR__ . '/platform_check.php';
        }

    }

    // same code from "Composer\Util\Platform" embedded to avoid unecessary dependency at runtime
    private static function getEnv(string $name)
    {
        if (array_key_exists($name, $_SERVER)) {
            return (string) $_SERVER[$name];
        }
        if (array_key_exists($name, $_ENV)) {
            return (string) $_ENV[$name];
        }

        return getenv($name);
    }

}

Call example :

<?php 

        putenv('COMPOSER_IGNORE_PLATFORM_REQ=1');
        $loader = include $rootDir . '/../../github/ComposerRequireChecker/vendor/autoload.php';
        $symbolProvider = new ComposerProvider($symbolNameParser, $loader);

When trying to analyse clone of maglnet/composer-require-checker 4.11.x-dev requires php (~8.2.0 || ~8.3.0) installed in another workspace than my workspace analyser.

Hope my request is enough clear ?

IMO you should redump the autoloader with no platform check then, or if you know what you are doing and that machine is running into such problems often then disable this globally if you like with composer config -g platform-check false. Or even overwrite the platform check file yourself.. But if you are including the autoloader then some files may be autoloaded that require the stated PHP version, so it is not safe/guaranteed to work.

And if you are including the autoloader you aren't really just doing static analysis.