A PHP Package to work with OS processes in an OOP way.

Overview

OS Process

This package is a wrapper around the Symfony Process component, and build up an API that is object-oriented and user-friendly.

Installation

composer require juststeveking/os-process

Usage

To start using this package, you first need to create an Abstraction for the process you want to use. I will walk you through this here:

We create an abstraction for Terraform:

use JustSteveKing\OS\Contracts\ProcessContract;

class Terraform implements ProcessContract
{
    //
}

The ProcessContract means that you have to implement a build method that will return a built Symfony Process that we can then run.

use JustSteveKing\OS\Contracts\CommandContract;
use JustSteveKing\OS\Contracts\ProcessContract;
use Symfony\Component\Process\Process;

class Terraform implements ProcessContract
{
    private CommandContract $command;
    
    public function build() : Process
    {
        return new Process(
            command: $this->command->toArgs(),
        );
    }
}

Let's create a Command now:

use JustSteveKing\OS\Contracts\CommandContract;

class TerraformCommand implements CommandContract
{
    public function __construct(
        public readonly array $args = [],
        public readonly null|string $executable = null,
    ) {}
    
    public function toArgs() : array
    {
        $executable = (new ExecutableFinder())->find(
            name: $this->executable ?? 'terraform',
        );

        if (null === $executable) {
            throw new InvalidArgumentException(
                message: "Cannot find executable for [$this->executable].",
            );
        }

        return array_merge(
            [$executable],
            $this->args,
        );
    }
}

Now we just need to build and assign this within our abstraction:

use JustSteveKing\OS\Contracts\CommandContract;
use JustSteveKing\OS\Contracts\ProcessContract;
use Symfony\Component\Process\Process;

class Terraform implements ProcessContract
{
    private CommandContract $command;
    
    public function apply(): Process
    {
        $this->command = new TerraformCommand(
            executable: 'terraform',
        );
        
        return $this->build();
    }
    
    public function build() : Process
    {
        return new Process(
            command: $this->command->toArgs(),
        );
    }
}

Now we are able to work with this process in our code:

$terraform = new Terraform();

$terraform->apply()->run(); // Will run `terraform apply` in an OS process.
You might also like...
PHP 7+ Payment processing library. It offers everything you need to work with payments: Credit card & offsite purchasing, subscriptions, payouts etc. - provided by Forma-Pro

Supporting Payum Payum is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our

Firebird-PHP: A library created to meet a work need when handling a Firebird database
Firebird-PHP: A library created to meet a work need when handling a Firebird database

Firebird-PHP: A library created to meet a work need when handling a Firebird database

PHP shells that work on Linux OS, macOS, and Windows OS.
PHP shells that work on Linux OS, macOS, and Windows OS.

PHP Reverse Shell Just a little refresh on the popular PHP reverse shell script pentestmonkey/php-reverse-shell. Credits to the original author! Works

The swiss army knife for Magento developers, sysadmins and devops. The tool provides a huge set of well tested command line commands which save hours of work time. All commands are extendable by a module API.

netz98 magerun CLI tools for Magento 2 The n98 magerun cli tools provides some handy tools to work with Magento from command line. Build Status Latest

"Whatever you do, work at it with all your heart." - Apostole Paul, probably, maybe.

Apostolos Whatever you do, work at it with all your heart. - Apostole Paul Also remember: Sigmund Freud says in Civilization and Its Discontents that

Make Laravel and Storyblok work together beautifully.

Use Storyblok’s amazing headless CMS in way that feels familiar to Laravel developers This package allows you to use fantastic Storyblok headless CMS

Beanstalk is a simple, fast work queue.

beanstalkd Simple and fast general purpose work queue. https://beanstalkd.github.io/ See doc/protocol.txt for details of the network protocol. Please

When the player is killed and dies, the sound will work

KillDeathSound | v1.0.0 ✔️ When the player is killed and dies, the sound will work ✔️ ☝ Features When the player is killed and dies, the sound will wo

This composer plugin is a temporary implementation of using symbolic links to local packages as dependencies to allow a parallel work process

Composer symlinker A Composer plugin to install packages as local symbolic links. This plugin is a temporary implementation of using symbolic links to

Releases(1.0.0)
Owner
Steve McDougall
🎥 https://bit.ly/3z19Gls 🎥 🖋 https://www.juststeveking.uk/ 🖋
Steve McDougall
This project processes a small database with php all on a web server. This project uses XAMPP to run the web server and the database.

PHP-introduction This project processes a small database with php all on a web server. This project uses XAMPP to run the web server and the database.

Tyler Jacques 1 Jan 6, 2022
A PHP Library To Make Your Work Work Easier/Faster

This Is A Php Library To Make Your Work Easier/Faster,

functionality 2 Dec 30, 2022
This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube features.

Laravel Youtube Client This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube featu

Tilson Mateus 6 May 31, 2023
PHP OOP interface for writing Slack Block Kit messages and modals

Slack Block Kit for PHP ?? For formatting messages and modals for Slack using their Block Kit syntax via an OOP interface ?? By Jeremy Lindblom (@jere

Slack PHP Framework 32 Dec 20, 2022
Demonstration of OOP concepts and usage of Abstract class & Interfaces

Learn OOP Demonstration of OOP concepts and usage of Abstract class & Interfaces Usage clone this repo run composer install run php index.php Code str

M N Islam Shihan 3 Sep 14, 2021
This Kirby V3 Plugin brings snippets and blueprints together in one place. It includes useful tools that completely changing the way you work with Kirby: Fast and well organized.

Kirby Components Overview Do you love to make awesome projects with Kirby CMS? Do you also find it difficult to switch between snippets and blueprints

Roman Gsponer 6 May 31, 2023
MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query and get result in a fastest way

Mysql Optimizer mysql optimizer also known as MOP is a php query handling and manipulation library providing easy and reliable way to manipulate query

null 2 Nov 20, 2021
Laravel Blog Package. Easiest way to add a blog to your Laravel website. A package which adds wordpress functionality to your website and is compatible with laravel 8.

Laravel Blog Have you worked with Wordpress? Developers call this package wordpress-like laravel blog. Give our package a Star to support us ⭐ ?? Inst

Binshops 279 Dec 28, 2022
Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Super Simple DTO Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Mohammed Manssour 8 Jun 8, 2023
Easily create and work with code snippets from PHP

code-snippets Easily create and work with code snippets from source code files of any type in PHP. The original code this package is based on was borr

Permafrost Software 8 Sep 4, 2022