innmind/virtual-machine

1.0.0 2021-03-26 16:17 UTC

This package is auto-updated.

Last update: 2024-08-26 23:50:28 UTC


README

Build Status codecov Type Coverage

innmind/cli之上的小抽象层,用于在项目上下文中管理进程和文件系统。

这适用于需要在项目上下文中运行二进制文件的CLI应用程序。

注意:请不要在可以从操作系统的任何位置运行CLI工具时使用此包。

安装

composer require innmind/virtual-machine

使用

# some/binary.php

use Innmind\CLI\{
    Main,
    Environment,
};
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\VirtualMachine\{
    VirtualMachine,
    Command,
};
use Innmind\Server\Status;
use Innmind\Server\Control;
use Innmind\Immutable\Set;

new class extends Main
{
    protected function main(Environment $env, OperatingSystem $os): void
    {
        $vm = VirtualMachine::of($env, $os);

        if ($env->workingDirectory()->toString() !== __DIR__.'/') {
            throw new \Exception('binary.php must me executed from within the "some/" directory')
        }

        // all required paths are resolved from the working directory so you don't
        // have to do the resolution yourself, and it's safe to require a file
        // from anywhere within your app
        $vm->filesystem()->require(Path::of('other_file.php'));

        // this is similar to $os->status()->processes()->all() but here the set
        // will only contain processes that are running whithin the working
        // directory for the same "binary.php" bin
        /** @var Set<Status\Server\Process> */
        $processes = $vm->processes()->all();

        // this is a shortcut to start a new process with the command
        // "php binary.php 'some-command'" started within the same working
        // directory. So you don't have to repeat the the binary name and
        // specify the working directory. The process is started in the foreground.
        /** @var Control\Server\Process */
        $process = $vm->processes()->execute(Command::of('some-command'));

        // Same as the line above except the process is started in the background
        $vm->processes()->daemon(Command::of('some-daemon'));
    }
}

注意:当然,您可以随意命名您的bin文件,而不仅仅是binary.php