innmind/operating-system

Abstraction for the whole system the script is operating in

2.1.0 2020-05-17 12:37 UTC

This package is auto-updated.

Last update: 2020-07-17 13:03:37 UTC


README

develop
codecov
Build Status

Abstraction for most of the operating system the PHP code run on.

The goal is to deal with the operating system in a more abstract way (instead of dealing with concrete, low level, details).

Installation

composer require innmind/operating-system

Usage

use Innmind\OperatingSystem\Factory;

$os = Factory::build();

Want to access the system clock ?

$os->clock() will return an instance of Innmind\TimeContinuum\TimeContinuumInterface.

Want to access the filesystem ?

use Innmind\Url\Path;

$adapter = $os->filesystem()->mount(Path::of('/var/data'));

$adater is an instance of Innmind\Filesystem\Adapter.

Want to list processes running on the system ?

$os->status()->processes()->all() will return a map of Inmmind\Immutable\Map<int, Innmind\Server\Status\Server\Process>.

Want to run a command on the system ?

use Innmind\Server\Control\Server\Command;

$process = $os
    ->control()
    ->processes()
    ->execute(Command::foreground('echo foo'));

$process is an instance of Innmind\Server\Control\Server\Process.

Want to open a port to the outside world ?

use Innmind\Socket\Internet\Transport;
use Innmind\IP\IPv4;
use Innmind\Url\Authority\Port;

$server = $os
    ->ports()
    ->open(
        Transport::tcp(),
        IPv4::localhost(),
        Port::of(1337),
    );

$server is an instance of Innmind\Socket\Server.

Want to open a local socket ?

# process A
use Innmind\Socket\Address\Unix;

$server = $os->sockets()->open(Unix::of('/tmp/foo.sock'));

$server is an instance of Innmind\Socket\Server.

# process B
use Innmind\Socket\Address\Unix;

$client = $os->sockets()->connectTo(Unix::of('/tmp/foo.sock'));

$client is an instance of Innmind\Socket\Client.

Want to execute commands on a remote server ?

use Innmind\Url\Url;
use Innmind\Server\Control\Server\Command;

$process = $os
    ->remote()
    ->ssh(Url::of('ssh://user@server-address:1337'))
    ->processes()
    ->execute(Command::foreground('ls'));

$process is an instance of Innmind\Server\Control\Server\Process.

Want to do a http call ?

use Innmind\Http\{
    Message\Request\Request,
    Message\Method,
    ProtocolVersion,
};
use Innmind\Url\Url;

$response = $os
    ->remote()
    ->http()
    ->fulfill(new Request(
        Url::of('http://example.com'),
        Method::get(),
        new ProtocolVersion(2, 0),
    ));

Want to access current process id ?

$os->process()->id();

Want to fork the current process ?

use Innmind\OperatingSystem\Exception\ForkFailed;

try {
    $side = $os->process()->fork();

    if ($side->parent()) {
        $childPid = $side->child();
    } else {
        try {
            // do something in the child process
            exit(0);
        } catch (\Throwable $e) {
            exit(1);
        }
    }

} catch (ForkFailed $e) {
    // handle the exception
}

Want to wait for child process to finish ?

$side = $os->process()->fork();

if ($side->parent()) {
    // do some thing
    $os->process()->children()->wait();
}

Want to pause the current process ?

use Innmind\TimeContinuum\Earth\Period\Minute;

$os->process()->halt(new Minute(1));

Want to listen for a signal ?

use Innmind\Signals\Signal;

$os->process()->signals()->listen(Signal::terminate(), function() {
    // handle the signal here
});

Note: when forking the process the child will have all listeners resetted to avoid having the listener called twice (in the parent and the child).

Important: beware when sending a signal right after a fork, there is a case where the listeners can still be called in the child.