122 lines
2.9 KiB
PHP
122 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Hotfix\Hotfix;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
class Hotfix
|
|
{
|
|
|
|
protected $signer;
|
|
|
|
protected $keyId;
|
|
|
|
protected $header;
|
|
|
|
protected $body;
|
|
|
|
public function __construct($hotfix, $signer)
|
|
{
|
|
$this->load($hotfix);
|
|
$this->signer = $signer[0];
|
|
$this->keyId = $signer[1];
|
|
}
|
|
|
|
protected function load($hotfix)
|
|
{
|
|
if (strpos($hotfix, "\n---\n")===false) {
|
|
throw new \Exception("Hotfix is missing a proper header, is line endings wrong?");
|
|
}
|
|
list ($header, $body) = explode("\n---\n", $hotfix, 2);
|
|
$header = Yaml::parse($header);
|
|
|
|
$this->header = $header;
|
|
$this->body = $body;
|
|
}
|
|
|
|
public function getBody()
|
|
{
|
|
return $this->body;
|
|
}
|
|
|
|
public function apply()
|
|
{
|
|
if (!array_key_exists('lang', $this->header)) {
|
|
$lang = 'bash';
|
|
} else {
|
|
$lang = strtolower($this->header['lang']);
|
|
}
|
|
|
|
$script = null;
|
|
$head = null;
|
|
$foot = null;
|
|
switch ($lang) {
|
|
case 'bash':
|
|
$exec = "/bin/bash";
|
|
$head = file_get_contents(__DIR__."/../stubs/bash.stub");
|
|
break;
|
|
case 'php':
|
|
$exec = "/usr/bin/env php";
|
|
$head = "<?php require_once \"".__DIR__."/../../vendor/autoload.php\"; ".file_get_contents(__DIR__."/../stubs/php.stub");
|
|
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, $head."\n".$this->body."\n".$foot);
|
|
passthru($exec." ".$tmpfile);
|
|
unlink($tmpfile);
|
|
}
|
|
|
|
public function getRequirements()
|
|
{
|
|
if (!array_key_exists('for',$this->header)) {
|
|
return [];
|
|
}
|
|
return $this->header['for'];
|
|
}
|
|
|
|
public function getSignedBy()
|
|
{
|
|
if (!$this->signer) {
|
|
return null;
|
|
}
|
|
return sprintf("%s <%s>",
|
|
$this->signer['uids'][0]['name'],
|
|
$this->signer['uids'][0]['email']
|
|
);
|
|
}
|
|
|
|
public function getKeyId()
|
|
{
|
|
return $this->keyId;
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
if (!array_key_exists('hotfix', $this->header)) {
|
|
return "Untitled hotfix";
|
|
}
|
|
return $this->header['hotfix'];
|
|
}
|
|
|
|
public function getInfo()
|
|
{
|
|
if (!array_key_exists('info', $this->header)) {
|
|
return "No additional information";
|
|
}
|
|
return $this->header['info'];
|
|
}
|
|
|
|
public function getAuthor()
|
|
{
|
|
if (!array_key_exists('author', $this->header)) {
|
|
return "Unknown author";
|
|
}
|
|
return $this->header['author'];
|
|
}
|
|
|
|
}
|