mirror of
https://github.com/kmvan/x-prober.git
synced 2026-04-22 01:08:59 +08:00
62 lines
1.4 KiB
PHP
62 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace InnStudio\Prober\Compiler;
|
|
|
|
final class ScriptGeneration
|
|
{
|
|
private $scriptFilePath = '';
|
|
|
|
private $distFilePath = '';
|
|
|
|
public function __construct(array $args)
|
|
{
|
|
[
|
|
'scriptFilePath' => $this->scriptFilePath,
|
|
'distFilePath' => $this->distFilePath,
|
|
] = $args;
|
|
|
|
if ( ! is_file($this->scriptFilePath)) {
|
|
$this->die("File not found: {$this->scriptFilePath}");
|
|
}
|
|
|
|
if ( ! is_file($this->distFilePath)) {
|
|
$this->die("File not found: {$this->distFilePath}");
|
|
}
|
|
|
|
if ( ! $this->setScript($this->getScript())) {
|
|
$this->die('Error: can not write script content to dist.');
|
|
}
|
|
|
|
$this->die('Script content wrote successful.', false);
|
|
}
|
|
|
|
private function getScript(): string
|
|
{
|
|
return (string) file_get_contents($this->scriptFilePath);
|
|
}
|
|
|
|
private function setScript(string $script): bool
|
|
{
|
|
$dist = (string) file_get_contents($this->distFilePath);
|
|
|
|
if ( ! $dist) {
|
|
return false;
|
|
}
|
|
|
|
$dist = str_replace('{INN_SCRIPT}', $script, $dist);
|
|
|
|
return (bool) file_put_contents($this->distFilePath, $dist);
|
|
}
|
|
|
|
private function die(string $msg, bool $die = true): void
|
|
{
|
|
$msg = "[ScriptGeneration] {$msg}\n";
|
|
|
|
if ($die) {
|
|
exit($msg);
|
|
}
|
|
|
|
echo $msg;
|
|
}
|
|
}
|