1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelSynchronize\Console\Commands; |
4
|
|
|
|
5
|
|
|
use SplFileInfo; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
use LaravelSynchronize\Console\Synchronizer\Synchronizer; |
10
|
|
|
|
11
|
|
|
class SynchronizeCommand extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The Synchronizer instance. |
15
|
|
|
* |
16
|
|
|
* @var \LaravelSynchronize\Console\Synchronizer\Synchronizer |
17
|
|
|
*/ |
18
|
|
|
protected $synchronizer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The name and signature of the console command. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $signature = 'synchronize {--class=} {--force}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Execute synchronizations that have not been executed yet'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new controller creator command instance. |
36
|
|
|
* |
37
|
|
|
* @param \LaravelSynchronize\Console\Synchronizer\Synchronizer $synchronizer |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Synchronizer $synchronizer) |
42
|
|
|
{ |
43
|
|
|
parent::__construct(); |
44
|
|
|
|
45
|
|
|
$this->synchronizer = $synchronizer; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Execute the console command. |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function handle() |
54
|
|
|
{ |
55
|
|
|
$forced = $this->option('force'); |
56
|
|
|
$class = $this->option('class'); |
57
|
|
|
$files = $this->synchronizer->getSynchronizations(); |
58
|
|
|
|
59
|
|
|
$fileNames = $files->map(function ($file) { |
60
|
|
|
return $file->getFileName(); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
if ($forced) { |
64
|
|
|
$filesToHandle = $files->filter(function ($file) use ($class) { |
65
|
|
|
return !$class || ($class && $class === $this->getClassName($file)); |
66
|
|
|
}); |
67
|
|
|
} else { |
68
|
|
|
$handledFiles = DB::table(Config::get('synchronizer.table'))->pluck('synchronization'); |
69
|
|
|
$unHandledFiles = $fileNames->diff($handledFiles); |
70
|
|
|
|
71
|
|
|
$filesToHandle = $files->filter(function ($file) use ($unHandledFiles, $class) { |
72
|
|
|
return $unHandledFiles->contains($file->getFileName()) |
73
|
|
|
&& (!$class || ($class === $this->getClassName($file))); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($filesToHandle->isEmpty()) { |
78
|
|
|
$this->info('No synchronizations found.'); |
79
|
|
|
|
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$filesToHandle->each(function ($file) { |
84
|
|
|
$this->info('Synchronizing: ' . $file->getFileName()); |
85
|
|
|
|
86
|
|
|
$this->synchronizer->run($file, 'up'); |
87
|
|
|
|
88
|
|
|
$this->info('Synchronized: ' . $file->getFileName()); |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
$this->info('Synchronizations completed'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get class name for file |
96
|
|
|
* |
97
|
|
|
* @param SplFileInfo $file |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private function getClassName(SplFileInfo $file): string |
102
|
|
|
{ |
103
|
|
|
return $this->synchronizer->getClassName( |
104
|
|
|
$this->synchronizer->getSynchronizationName($file->getFilename()) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|