|
1
|
|
|
<?php |
|
2
|
|
|
namespace SmoothPhp\LaravelAdapter\Console; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Console\Command; |
|
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
6
|
|
|
use Illuminate\Database\DatabaseManager; |
|
7
|
|
|
use SmoothPhp\LaravelAdapter\Exception\EventStoreFileNotFound; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ImportEventStore |
|
11
|
|
|
* @package SmoothPhp\LaravelAdapter\Console |
|
12
|
|
|
* @author Simon Bennett <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class ImportEventStore extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The name and signature of the console command. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $signature = 'smoothphp:import {--file= : If specified, import from a JSON file}'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The console command description. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $description = 'Import EventStore'; |
|
29
|
|
|
|
|
30
|
|
|
/** @var DatabaseManager */ |
|
31
|
|
|
private $databaseManager; |
|
32
|
|
|
|
|
33
|
|
|
/** @var Repository */ |
|
34
|
|
|
private $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* ExportEventStore constructor. |
|
38
|
|
|
* @param DatabaseManager $databaseManager |
|
39
|
|
|
* @param Repository $config |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(DatabaseManager $databaseManager, Repository $config) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->databaseManager = $databaseManager; |
|
44
|
|
|
parent::__construct(); |
|
45
|
|
|
$this->config = $config; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Execute the console command. |
|
50
|
|
|
* |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
|
|
public function handle() |
|
54
|
|
|
{ |
|
55
|
|
|
$events = json_decode(!!$this->option('file') ? $this->readFromJsonFile() : $this->readFromStdIn(), true); |
|
56
|
|
|
|
|
57
|
|
|
$this->truncate(); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($events as $event) { |
|
60
|
|
|
$this->databaseManager |
|
61
|
|
|
->connection($this->config->get('cqrses.eventstore_connection')) |
|
62
|
|
|
->table($this->config->get('cqrses.eventstore_table')) |
|
63
|
|
|
->insert($event); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->call('smoothphp:rebuild'); |
|
67
|
|
|
|
|
68
|
|
|
return $this->line('Success'); |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Truncate (empty) event store table |
|
74
|
|
|
*/ |
|
75
|
|
|
private function truncate() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->databaseManager |
|
78
|
|
|
->connection($this->config->get('cqrses.eventstore_connection')) |
|
79
|
|
|
->table($this->config->get('cqrses.eventstore_table')) |
|
80
|
|
|
->truncate(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Read event store JSON from std in |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
private function readFromStdIn() |
|
89
|
|
|
{ |
|
90
|
|
|
$fd = fopen("php://stdin", "r"); |
|
91
|
|
|
$eventsJson = ""; |
|
92
|
|
|
while (!feof($fd)) { |
|
93
|
|
|
$eventsJson .= fread($fd, 1024); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
fclose($fd); |
|
97
|
|
|
|
|
98
|
|
|
return $eventsJson; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Read event store JSON from file |
|
103
|
|
|
* |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
private function readFromJsonFile() |
|
107
|
|
|
{ |
|
108
|
|
|
if (!file_exists($path = $this->option('file'))) { |
|
109
|
|
|
throw new EventStoreFileNotFound(sprintf('%s/%s', __DIR__, $path)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return file_get_contents($path); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|