1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Connector\Auth\Commands; |
4
|
|
|
|
5
|
|
|
use Slides\Connector\Auth\Sync\Syncer; |
6
|
|
|
use Slides\Connector\Auth\Concerns\PassesModes; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SyncImport |
10
|
|
|
* |
11
|
|
|
* @package Slides\Connector\Auth\Commands |
12
|
|
|
*/ |
13
|
|
|
class SyncImport extends \Illuminate\Console\Command |
14
|
|
|
{ |
15
|
|
|
use PassesModes; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'connector:sync-import {filename} |
23
|
|
|
{ --k|key= : Encryption key } |
24
|
|
|
{ --passwords : Allow syncing passwords (can rewrite remotely and locally) } |
25
|
|
|
{ --users= : Sync the specific users } |
26
|
|
|
{ --no-modes : Omit all modes }'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Synchronize remote difference to apply latest changes locally'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function handle() |
41
|
|
|
{ |
42
|
|
|
if(!$key = $this->option('key')) { |
43
|
|
|
throw new \InvalidArgumentException('Encryption key must be passed.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$noModes = $this->option('no-modes'); |
47
|
|
|
|
48
|
|
|
$this->flushListeners(); |
49
|
|
|
|
50
|
|
|
$syncer = new Syncer(null, $noModes ? [] : $this->modes()); |
51
|
|
|
$syncer->setOutputCallback(function(string $message) { |
52
|
|
|
$this->info('[Syncer] ' . $message); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
$this->info('Importing the dump...'); |
57
|
|
|
|
58
|
|
|
$syncer->import($this->argument('filename'), $key, $noModes ? false : !$this->hasModes()); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->displayModes($syncer->getModes()); |
61
|
|
|
|
62
|
|
|
$changes = $syncer->getForeignersCount(); |
63
|
|
|
|
64
|
|
|
if(!$this->confirm('Apply ' . $changes . ' changes?', true)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$duration = $this->measure(function() use ($syncer, $changes) { |
69
|
|
|
$this->info("Applying {$changes} changes..."); |
70
|
|
|
|
71
|
|
|
$syncer->apply(); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
$this->info("Finished in {$duration}s."); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Measure an execution time of the callback. |
79
|
|
|
* |
80
|
|
|
* @param \Closure $callback |
81
|
|
|
* |
82
|
|
|
* @return float |
83
|
|
|
*/ |
84
|
|
|
public function measure(\Closure $callback) |
85
|
|
|
{ |
86
|
|
|
$start = microtime(true); |
87
|
|
|
|
88
|
|
|
$callback(); |
89
|
|
|
|
90
|
|
|
$end = microtime(true); |
91
|
|
|
|
92
|
|
|
return round($end - $start, 2); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Flush models event listeners to speed-up the process. |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function flushListeners() |
101
|
|
|
{ |
102
|
|
|
if(class_exists('\App\Http\Models\User')) { |
103
|
|
|
\App\Http\Models\User::flushEventListeners(); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if(class_exists('App\Http\Models\CustomerProfile')) { |
107
|
|
|
\App\Http\Models\CustomerProfile::flushEventListeners(); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if(class_exists('App\Modules\Billing\Models\Account::class')) { |
111
|
|
|
\App\Modules\Billing\Models\Account::flushEventListeners(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |