1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chocofamily\LaravelEventSauce\Console; |
4
|
|
|
|
5
|
|
|
use Chocofamily\LaravelEventSauce\Exceptions\CodeGenerationFailed; |
6
|
|
|
use EventSauce\EventSourcing\CodeGeneration\CodeDumper; |
7
|
|
|
use EventSauce\EventSourcing\CodeGeneration\YamlDefinitionLoader; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Filesystem\Filesystem; |
10
|
|
|
|
11
|
|
|
final class GenerateCommand extends Command |
12
|
|
|
{ |
13
|
|
|
protected $signature = 'eventsauce:generate'; |
14
|
|
|
|
15
|
|
|
protected $description = 'Generate commands and events for aggregate roots.'; |
16
|
|
|
|
17
|
|
|
protected $filesystem; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* GenerateCommand constructor. |
21
|
|
|
* @param Filesystem $filesystem |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Filesystem $filesystem) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
|
27
|
|
|
$this->filesystem = $filesystem; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function handle(): void |
31
|
|
|
{ |
32
|
|
|
$this->info('Start generating code...'); |
33
|
|
|
|
34
|
|
|
collect(config('eventsauce.code_generation')) |
35
|
|
|
->reject(function (array $config) { |
36
|
|
|
return is_null($config['input_yaml_file']); |
37
|
|
|
}) |
38
|
|
|
->each(function (array $config) { |
39
|
|
|
$this->generateCode($config['input_yaml_file'], $config['output_file']); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
$this->info('All done!'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $inputFile |
47
|
|
|
* @param string $outputFile |
48
|
|
|
* @throws CodeGenerationFailed |
49
|
|
|
*/ |
50
|
|
|
private function generateCode(string $inputFile, string $outputFile) |
51
|
|
|
{ |
52
|
|
|
if (! file_exists($inputFile)) { |
53
|
|
|
throw CodeGenerationFailed::definitionFileDoesNotExist($inputFile); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$loadedYamlContent = (new YamlDefinitionLoader())->load($inputFile); |
57
|
|
|
$phpCode = (new CodeDumper())->dump($loadedYamlContent); |
58
|
|
|
|
59
|
|
|
$this->filesystem->put($outputFile, $phpCode); |
60
|
|
|
|
61
|
|
|
$this->warn("Written code to `{$outputFile}`"); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|