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