Adam Pippin
3 years ago
3 changed files with 119 additions and 0 deletions
@ -0,0 +1,68 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
|
|||
namespace App\Commands\Stack; |
|||
|
|||
use LaravelZero\Framework\Commands\Command; |
|||
|
|||
class Compile extends Command |
|||
{ |
|||
/** |
|||
* The signature of the command. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $signature = 'stack:compile {in_file} {out_file} {--format=Yaml}'; |
|||
|
|||
/** |
|||
* The description of the command. |
|||
* |
|||
* @var string |
|||
*/ |
|||
protected $description = 'Read an input file, process, output result to a file'; |
|||
|
|||
/** |
|||
* Execute the console command. |
|||
* |
|||
* @return mixed |
|||
*/ |
|||
public function handle() |
|||
{ |
|||
$engine = new \App\Engine\Engine(); |
|||
|
|||
$serializer = $this->getSerializer(); |
|||
$engine->setSerializer($serializer)->setUnserializer($serializer)->setCompiler(new \App\Engine\Cfnpp()); |
|||
|
|||
$start_file = $this->argument('in_file'); |
|||
$start_document = $serializer->unserialize(file_get_contents($start_file)); |
|||
|
|||
$files = $start_document->getMeta('stack'); |
|||
$files[] = $start_file; |
|||
|
|||
$base_path = realpath(dirname($start_file)); |
|||
|
|||
$top_file = array_shift($files); |
|||
$this->info($top_file); |
|||
$engine->setInput(file_get_contents($base_path.DIRECTORY_SEPARATOR.$top_file)); |
|||
foreach ($files as $file) |
|||
{ |
|||
$this->info($file); |
|||
$engine->compile(file_get_contents($base_path.DIRECTORY_SEPARATOR.$file), new \App\Engine\CfnppOptions()); |
|||
} |
|||
file_put_contents($this->argument('out_file'), $engine->getOutput()); |
|||
} |
|||
|
|||
protected function getSerializer(): \App\Serialize\IUnserialize |
|||
{ |
|||
$format = $this->option('format'); |
|||
$class = '\\App\\Serialize\\'.$format; |
|||
|
|||
if (!class_exists($class)) |
|||
{ |
|||
throw new \Exception('Unknown formatter: '.$format); |
|||
} |
|||
|
|||
return new $class(); |
|||
} |
|||
} |
Loading…
Reference in new issue