malc0mn/php-basher

Bash script generator and executor for PHP.

dev-master / 1.0.x-dev 2020-03-16 08:08 UTC

This package is not auto-updated.

Last update: 2024-11-17 10:02:06 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License SensioLabsInsight

What! Why?

This is by no means a(n attempt at a) replacement tool for things like Robo. I just thought it would be fun to create a bash script generator and executor for PHP and it was a nice excuse to create an OO targeted PHP library again...

Install using composer

Open a shell, cd to your poject and type:

composer require malc0mn/php-basher

or edit composer.json and add:

{
    "require": {
        "malc0mn/php-basher": "~1.0"
    }
}

Usage

Generating a bash script

An extremely simple example:

<?php
use Basher\Tools\OSBase;

$base = new OSBase();

$base->set('-e', '-v')
    ->changeDir('/opt/approot')
    ->makeDir('build-new')
    ->delete('previous')
    ->renameIfExists('current', 'previous')
    ->link('build-new', 'current')
    ->set('-o pipefail')
;

echo (string)$base;

Would generate this output:

#!/bin/bash

set -e -v -o pipefail

cd /opt/approot
mkdir -p build-new
rm -f previous
if [ -d current -o -f current -o -L current ]; then mv -f current previous ; fi
ln -s build-new current

Execute commands

Say you would want to execute a command in a linux container to deploy new code, you could do this:

<?php

use Basher\Tools\Vcs\Git;
use Basher\Tools\Lxc\Lxc;

$destination = 'build-' . date('Ymd-His');

// Build the command stack we want to run INSIDE the container.
$git = new Git();
$commands = $git->clone(
      'https://github.com/malc0mn/php-basher',
      "/opt/approot/$destination",
      'master'
    )->changeDir('/opt/approot')
    ->delete('previous', true, true)
    ->renameIfExists('current', 'previous', true)
    ->link($destination, 'current', true)
    ->service('php-fpm', 'reload')
    ->getStacked()
;

// $commands now holds the command stack, joined by double ampersands: '&&' so
// that the stack is aborted immediately when a command fails!

// Attach to the container and run the above command set INSIDE it.
$result = Lxc::attach('my-lxc-container')
    ->execute($commands)
    ->run()
;

// Perform execution result handling.
if (!$result->wasSuccessful()) {
    throw new \RuntimeException($result->getOutput());
}

TODO: complete this readme :/