easysoft / phpmicro

micro sfx SAPI for php
Apache License 2.0
251 stars 30 forks source link

Ability to access phpmicro from PHP file #19

Closed saundefined closed 1 hour ago

saundefined commented 1 day ago

Hi,

Example:

<?php

// test.php
var_dump(phpversion());
var_dump(exec('php --version'));

Downloaded phpmicro 8.3.6:

cat /path/to/micro.sfx test.php > myawesomeapp
chmod 0755 ./myawesomeapp
./myawesomeapp

//string(5) "8.3.6"
//string(66) "    with Zend OPcache v8.3.11, Copyright (c), by Zend Technologies"

So.. 8.3.6 is phpmicro PHP version, and 8.3.11 is local PHP version.

Is any chance to run phpmicro from PHP file?

dixyes commented 3 hours ago

I'm not sure what is "run phpmicro from PHP file"

If you want to use micro like cli (something_like_php_command_but_micro /path/to/some.php), you may look at fakecmd.php

If you want to run another php process in micro'd php codes, you may use command argument with proc_open

caller.php:

<?php

function runAnother(string $another, ?string $stdin = null): array {
    $self = micro_get_self_filename();
    $proc = proc_open([
        $self,
        "runsub",
        $another,
    ], [
        0 => ["pipe", "r"],
        1 => ["pipe", "w"],
        2 => ["pipe", "w"],
    ], $pipes);

    if ($stdin !== null) {
        fwrite($pipes[0], $stdin);
    }
    fclose($pipes[0]);
    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);

    pcntl_wait($status);

    return [
        'stdout' => $stdout,
        'stderr' => $stderr,
        'rc' => pcntl_wexitstatus($status),
    ];
}

// assume this is a argument parser
// you may use https://www.php.net/manual/en/function.getopt.php to implement this
if ($argv[1]??null === "runsub") {
    require_once($argv[2]);
} else {
    $ret = runAnother("callee.php");
    var_dump($ret);
}