<?php

declare(strict_types=1);



class Env
{
    private static ?string $phpCli = null;

    

    public static function phpCli(): string
    {
        if (self::$phpCli !== null) {
            return self::$phpCli;
        }
        return self::$phpCli = self::resolvePhpCli();
    }

    private static function resolvePhpCli(): string
    {
         
        if (class_exists('Setting')) {
            $configured = (string) Setting::get('php_cli_path', '');
            if ($configured !== '' && @is_executable($configured)) {
                return $configured;
            }
        }

         
        if (php_sapi_name() === 'cli' && defined('PHP_BINARY') && PHP_BINARY !== ''
            && !str_contains(PHP_BINARY, 'fpm') && @is_executable(PHP_BINARY)) {
            return PHP_BINARY;
        }

        $ver = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;  

         
         
        $compact = PHP_MAJOR_VERSION . PHP_MINOR_VERSION;

        $candidates = [
            '/usr/bin/php' . $ver,
            '/usr/local/bin/php' . $ver,
            '/opt/php' . $ver . '/bin/php',
            '/opt/cpanel/ea-php' . $compact . '/root/usr/bin/php',
            '/opt/alt/php' . $compact . '/usr/bin/php',
            '/usr/bin/php',
            '/usr/local/bin/php',
        ];
        foreach ($candidates as $path) {
            if (@is_file($path) && @is_executable($path)) {
                return $path;
            }
        }

        foreach (['/opt/cpanel/ea-php*/root/usr/bin/php', '/opt/alt/php*/usr/bin/php'] as $pattern) {
            $found = @glob($pattern);
            if (!is_array($found)) {
                continue;
            }
            rsort($found, SORT_NATURAL);
            foreach ($found as $path) {
                if (@is_file($path) && @is_executable($path)) {
                    return $path;
                }
            }
        }

         
        if (function_exists('exec')) {
            foreach (['php' . $ver, 'php'] as $name) {
                $found = @shell_exec('command -v ' . escapeshellarg($name) . ' 2>/dev/null');
                $found = is_string($found) ? trim($found) : '';
                if ($found !== '' && @is_executable($found)) {
                    return $found;
                }
            }
        }

         
        return 'php';
    }

     
    public static function resetPhpCliCache(): void
    {
        self::$phpCli = null;
    }

    

    public static function processAlive(int $pid): bool
    {
        if ($pid <= 0) {
            return false;
        }
        if (@is_dir('/proc')) {
            return @file_exists('/proc/' . $pid);
        }
        if (function_exists('posix_kill')) {
            if (@posix_kill($pid, 0)) {
                return true;
            }
             
            return function_exists('posix_get_last_error') && posix_get_last_error() === 1;
        }
        return true;
    }

    

    public static function canSpawn(): bool
    {
        return function_exists('exec');
    }
}
