malc0mn/php-basher

PHP 的 Bash 脚本生成器和执行器。

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

This package is not auto-updated.

Last update: 2024-09-22 09:08:18 UTC


README

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

什么!为什么?

这绝对不是要取代像 Robo 这样的工具。我只是觉得为 PHP 创建一个 bash 脚本 生成器和执行器很有趣,这又是一个创建面向对象 PHP 库的好借口...

使用 composer 安装

打开 shell,cd 到你的项目目录,然后输入

composer require malc0mn/php-basher

或者编辑 composer.json 并添加

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

用法

生成 bash 脚本

一个非常简单的示例

<?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;

将生成以下输出

#!/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

执行命令

比如说,你想要在一个 linux 容器 中执行命令以部署新代码,你可以这样做

<?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());
}

待办:完成这个 README :/