php-hotfix/src/Runner/PhpRunner.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 PHP
*
*
*/
class PhpRunner implements RunnerInterface
{
const PHP_STUB = "/../stubs/php.stub";
protected $hotfix;
protected $facts;
public function prepare(Hotfix $hotfix, Facts $facts)
{
$this->hotfix = $hotfix;
$this->facts = $facts;
}
public function apply()
{
$stub = file_get_contents(__DIR__.self::PHP_STUB);
$body = $this->hotfix->getBody();
$hash = $this->hotfix->getHash();
$stub.= 'function fact($key) { ';
$stub.= 'switch ($key) {';
$facts = $this->facts->getFlat();
foreach ($facts as $key=>$value) {
$stub.= 'case "'.$key.'": return "'.$value.'";';
}
$stub.= '}}'.PHP_EOL;
// Create temporary filename based on the hash of the hotfix filename
$tmpfile = sys_get_temp_dir()."/hotfix_".$hash;
file_put_contents($tmpfile, '<?php '.$stub."\n".$body."\n");
// Execute the hotfix and clean up
passthru("php {$tmpfile}");
unlink($tmpfile);
}
}