php-hotfix/src/Runner/BashRunner.php

52 lines
1.2 KiB
PHP

<?php
namespace NoccyLabs\Hotfix\Runner;
use NoccyLabs\Hotfix\System\Facts;
use NoccyLabs\Hotfix\Hotfix\Hotfix;
/**
* Run hotfixes written in BASH/DASH script.
*
*
*/
class BashRunner implements RunnerInterface
{
const BASH_STUB = "/../stubs/bash.stub";
protected $hotfix;
protected $facts;
public function prepare(Hotfix $hotfix, Facts $facts)
{
$this->hotfix = $hotfix;
$this->facts = $facts;
}
public function apply()
{
$head = file_get_contents(__DIR__.self::BASH_STUB);
$body = $this->hotfix->getBody();
$hash = $this->hotfix->getHash();
$facts = $this->facts->getFlat();
$head .= 'function fact {'.PHP_EOL;
$head .= ' case "$1" in'.PHP_EOL;
foreach ($facts as $key=>$fact) {
$head .= ' "'.$key.'") echo "'.$fact.'";;'.PHP_EOL;
}
$head .= ' esac'.PHP_EOL;
$head .= '}'.PHP_EOL;
// Create temporary filename based on the hash of the hotfix
$tmpfile = sys_get_temp_dir()."/hotfix_".$hash;
file_put_contents($tmpfile, $head."\n".$body."\n");
// Execute the hotfix and clean up
passthru("bash {$tmpfile}");
unlink($tmpfile);
}
}