2016-04-19 13:54:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace NoccyLabs\Hotfix\Hotfix;
|
|
|
|
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
class Hotfix
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $signer;
|
|
|
|
|
|
|
|
protected $header;
|
|
|
|
|
|
|
|
protected $body;
|
|
|
|
|
|
|
|
public function __construct($hotfix, $signer)
|
|
|
|
{
|
|
|
|
$this->load($hotfix);
|
|
|
|
$this->signer = $signer;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function load($hotfix)
|
|
|
|
{
|
2016-04-19 14:37:31 +00:00
|
|
|
if (strpos($hotfix, "\n---\n")===false) {
|
|
|
|
throw new \Exception("Hotfix is missing a proper header, is line endings wrong?");
|
|
|
|
}
|
2016-04-19 13:54:03 +00:00
|
|
|
list ($header, $body) = explode("\n---\n", $hotfix, 2);
|
|
|
|
$header = Yaml::parse($header);
|
|
|
|
|
|
|
|
$this->header = $header;
|
|
|
|
$this->body = $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function apply()
|
|
|
|
{
|
|
|
|
if (!array_key_exists('lang', $this->header)) {
|
|
|
|
$lang = 'bash';
|
|
|
|
} else {
|
|
|
|
$lang = strtolower($this->header['lang']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$script = null;
|
|
|
|
switch ($lang) {
|
|
|
|
case 'bash':
|
|
|
|
$exec = "/bin/bash";
|
|
|
|
break;
|
|
|
|
case 'php':
|
|
|
|
$exec = "/usr/bin/env php";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(STDERR, "Error: Unsupported language %s\n", $lang);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$tmpfile = sys_get_temp_dir()."/hotfix_".sha1($this->header['hotfix']);
|
|
|
|
|
|
|
|
file_put_contents($tmpfile, $this->body);
|
|
|
|
passthru($exec." ".$tmpfile);
|
|
|
|
unlink($tmpfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSignedBy()
|
|
|
|
{
|
|
|
|
if (!$this->signer) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return sprintf("%s <%s>",
|
|
|
|
$this->signer['uids'][0]['name'],
|
|
|
|
$this->signer['uids'][0]['email']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->header['hotfix'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInfo()
|
|
|
|
{
|
|
|
|
return $this->header['info'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAuthor()
|
|
|
|
{
|
|
|
|
return $this->header['author'];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|