innmind/operating-system

Abstraction for the whole system the script is operating in

6.0.0 2025-05-24 12:00 UTC

README

CI codecov Type Coverage

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).

Important

you must use vimeo/psalm to make sure you use this library correctly.

Installation

composer require innmind/operating-system

Documentation

Documentation is located in the documentation/ folder.

Usage

use Innmind\OperatingSystem\Factory;

$os = Factory::build();

Want to access the system clock ?

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

Want to access the filesystem ?

use Innmind\Url\Path;

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

$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\Set<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'))
    ->unwrap();

$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),
    )
    ->unwrap();

$server is an instance of Innmind\IO\Sockets\Servers\Server.

Want to open a local socket ?

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

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

$server is an instance of Innmind\IO\Sockets\Servers\Server.

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

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

$client is an instance of Innmind\IO\Sockets\Clients\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'))
    ->unwrap();

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

Want to do a http call ?

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

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

Want to access current process id ?

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

Want to pause the current process ?

use Innmind\TimeContinuum\Period;

$os->process()->halt(Period::minute(1));

Want to listen for a signal ?

use Innmind\Signals\Signal;

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