|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HughCube\ACMClient\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use HughCube\ACMClient\ACM; |
|
6
|
|
|
use HughCube\ACMClient\Client; |
|
7
|
|
|
use Illuminate\Console\Command; |
|
8
|
|
|
|
|
9
|
|
|
class SyncConfig extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The name and signature of the console command. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $signature = 'acm:sync-config |
|
17
|
|
|
{dataId : Configured ID } |
|
18
|
|
|
{file : Configure saved file path } |
|
19
|
|
|
{--name=default : Name of the ACM client, default "default" } |
|
20
|
|
|
{--repeat=-1 : Number of repeated checks, -1 means unlimited, default "-1" } |
|
21
|
|
|
{--interval=1000 : Every check interval MS, default 1000 ms }'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The console command description. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $description = 'Save the configuration of pull ACM to a file'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Execute the console command. |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
* @throws \Exception |
|
35
|
|
|
*/ |
|
36
|
|
|
public function handle() |
|
37
|
|
|
{ |
|
38
|
|
|
$repeat = $this->option("repeat"); |
|
39
|
|
|
$interval = $this->option("interval"); |
|
40
|
|
|
|
|
41
|
|
|
$runCount = 0; |
|
42
|
|
|
do { |
|
43
|
|
|
|
|
44
|
|
|
$this->syncConfig(); |
|
45
|
|
|
$runCount++; |
|
46
|
|
|
usleep($interval * 1000); |
|
47
|
|
|
|
|
48
|
|
|
} while (-1 == $repeat || $runCount < $repeat); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return bool |
|
53
|
|
|
* @throws \Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function syncConfig() |
|
56
|
|
|
{ |
|
57
|
|
|
$dataId = $this->argument("dataId"); |
|
58
|
|
|
$file = $this->argument('file'); |
|
59
|
|
|
|
|
60
|
|
|
$connection = $this->option('name'); |
|
61
|
|
|
|
|
62
|
|
|
/** @var Client $client */ |
|
63
|
|
|
$client = ACM::connection($connection); |
|
64
|
|
|
|
|
65
|
|
|
$localContents = null; |
|
66
|
|
|
if (is_file($file)) { |
|
|
|
|
|
|
67
|
|
|
$localContents = file_get_contents($file); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
$localHash = $this->hashContents($localContents); |
|
70
|
|
|
|
|
71
|
|
|
$remoteContents = $client->read($dataId); |
|
|
|
|
|
|
72
|
|
|
$remoteHash = $this->hashContents($remoteContents); |
|
73
|
|
|
|
|
74
|
|
|
if ($localHash === $remoteHash) { |
|
75
|
|
|
$this->line(sprintf("%s <info>文件无需同步, hash: %s</info>", date('Y-m-d H:i:s'), $remoteHash)); |
|
76
|
|
|
} else { |
|
77
|
|
|
file_put_contents($file, $remoteContents, LOCK_EX); |
|
|
|
|
|
|
78
|
|
|
$this->line(sprintf("%s <info>文件同步成功, hash: %s</info>", date('Y-m-d H:i:s'), $remoteHash)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function hashContents($contents) |
|
85
|
|
|
{ |
|
86
|
|
|
if (null === $contents) { |
|
87
|
|
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return md5($contents) . '-' . crc32($contents);; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|