|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelSynchronize\Listeners; |
|
4
|
|
|
|
|
5
|
|
|
use Schema; |
|
6
|
|
|
use SplFileInfo; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
use Illuminate\Support\Facades\Config; |
|
9
|
|
|
use Illuminate\Database\Events\MigrationStarted; |
|
10
|
|
|
use LaravelSynchronize\Console\Synchronizer\Synchronizer; |
|
11
|
|
|
|
|
12
|
|
|
class MigrationStartedEventListener |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The Synchronizer instance. |
|
16
|
|
|
* |
|
17
|
|
|
* @var \LaravelSynchronize\Console\Synchronizer\Synchronizer |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $synchronizer; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Synchronizer $synchronizer) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->synchronizer = $synchronizer; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Handle the event. |
|
28
|
|
|
* |
|
29
|
|
|
* @todo use MigrationsStarted event to collect synchronization files |
|
30
|
|
|
* |
|
31
|
|
|
* @param MigrationStarted $migrationStarted |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
* |
|
35
|
|
|
* @author Ramon Bakker <[email protected]> |
|
36
|
|
|
* @version 1.0.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
public function handle(MigrationStarted $migrationStarted) |
|
39
|
|
|
{ |
|
40
|
|
|
// Synchronizations should execute before going up |
|
41
|
|
|
if ($migrationStarted->method !== 'up') { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!Schema::hasTable(Config::get('synchronizer.table'))) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$class = get_class($migrationStarted->migration) . 'Synchronization'; |
|
50
|
|
|
$files = $this->synchronizer->getSynchronizations(); |
|
51
|
|
|
|
|
52
|
|
|
$fileNames = $files->map(function ($file) { |
|
53
|
|
|
return $file->getFileName(); |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
$handledFiles = DB::table(Config::get('synchronizer.table'))->pluck('synchronization'); |
|
57
|
|
|
$unHandledFiles = $fileNames->diff($handledFiles); |
|
58
|
|
|
|
|
59
|
|
|
$filesToHandle = $files->filter(function ($file) use ($unHandledFiles, $class) { |
|
60
|
|
|
return $unHandledFiles->contains($file->getFileName()) |
|
61
|
|
|
&& (!$class || ($class === $this->getClassName($file))); |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
if ($filesToHandle->isEmpty()) { |
|
65
|
|
|
echo "No synchronization found for {$class}\n"; |
|
66
|
|
|
|
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$filesToHandle->each(function ($file) { |
|
71
|
|
|
echo 'Synchronizing: ' . $file->getFileName() . "\n"; |
|
72
|
|
|
|
|
73
|
|
|
$this->synchronizer->run($file, 'up'); |
|
74
|
|
|
|
|
75
|
|
|
echo 'Synchronized: ' . $file->getFileName() . "\n"; |
|
76
|
|
|
}); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get class name for file |
|
81
|
|
|
* |
|
82
|
|
|
* @param SplFileInfo $file |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getClassName(SplFileInfo $file): string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->synchronizer->getClassName( |
|
89
|
|
|
$this->synchronizer->getSynchronizationName($file->getFilename()) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|