|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wabel\CertainAPI\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class FileChangesHelper |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Create the directory if it doesn't exist. Right:0775 |
|
10
|
|
|
* @param $dirPath |
|
11
|
|
|
* @return bool |
|
12
|
|
|
*/ |
|
13
|
|
|
public static function createDirectory($dirPath){ |
|
14
|
|
|
if(!file_exists($dirPath)){ |
|
15
|
|
|
return mkdir($dirPath, 0775, true, null); |
|
16
|
|
|
} |
|
17
|
|
|
return false; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Write the file. The content is already in json. |
|
22
|
|
|
* @param string $path |
|
23
|
|
|
* @param string $msg |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function writeFile($path,$msg){ |
|
27
|
|
|
$f = fopen($path, "w+"); |
|
28
|
|
|
fwrite($f, $msg); |
|
29
|
|
|
fclose($f); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Remove the file |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function removeFile(string $path): void{ |
|
36
|
|
|
unlink($path); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Save appointments with the timestamp in filename. |
|
41
|
|
|
* @param string $filePath |
|
42
|
|
|
* @param string $contents |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function saveAppointmentsFileByHistory($filePath,$contents){ |
|
46
|
|
|
$time = time(); |
|
47
|
|
|
$file = pathinfo($filePath); |
|
48
|
|
|
$fileNamebase = $file['filename']; |
|
49
|
|
|
$filePath = $file['dirname'].'/'.$fileNamebase.'_'.$time.'.json'; |
|
50
|
|
|
if(!file_exists($filePath)){ |
|
51
|
|
|
self::writeFile($filePath,''); |
|
52
|
|
|
} |
|
53
|
|
|
self::writeFile($filePath,$contents); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the content from a file. |
|
58
|
|
|
* @param $path |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getContentFile($path){ |
|
62
|
|
|
if(!file_exists($path)){ |
|
63
|
|
|
self::writeFile($path,''); |
|
64
|
|
|
} |
|
65
|
|
|
$handle = fopen($path, "rb"); |
|
66
|
|
|
if (FALSE === $handle) { |
|
67
|
|
|
$handle = fopen($path, "rb"); |
|
68
|
|
|
} |
|
69
|
|
|
$contents = ''; |
|
70
|
|
|
while (!feof($handle)) { |
|
71
|
|
|
$contents .= fread($handle, 8192); |
|
72
|
|
|
} |
|
73
|
|
|
fclose($handle); |
|
74
|
|
|
return $contents; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the content from a json file by decoding. |
|
79
|
|
|
* @param string $path |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function getJsonContentFromFile($path){ |
|
83
|
|
|
$contents = self::getContentFile($path); |
|
84
|
|
|
return json_decode($contents, true); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get an file list of appointments by eventCode |
|
89
|
|
|
* @param string $eventCode |
|
90
|
|
|
* @param string $pathDir |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function getFilesListHistoryAppointmentsByEventCode($eventCode,$pathDir){ |
|
94
|
|
|
$fileList = array(); |
|
95
|
|
|
$files = glob($pathDir.'/appointments_'.$eventCode.'_*.json'); |
|
96
|
|
|
foreach ($files as $file) { |
|
97
|
|
|
$fileList[filemtime($file)] = $file; |
|
98
|
|
|
} |
|
99
|
|
|
ksort($fileList); |
|
100
|
|
|
$fileList = array_reverse($fileList, TRUE); |
|
101
|
|
|
return $fileList; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the last list of appointments. |
|
106
|
|
|
* @param string $eventCode |
|
107
|
|
|
* @param string $pathDir |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function getTheLastAppointmentsSaved($eventCode,$pathDir){ |
|
111
|
|
|
$fileList = self::getFilesListHistoryAppointmentsByEventCode($eventCode,$pathDir); |
|
112
|
|
|
return array_shift($fileList); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public static function checkDirectory(string $pathDir): string |
|
116
|
|
|
{ |
|
117
|
|
|
if (!is_dir($pathDir)) { |
|
118
|
|
|
return 'no_directory'; |
|
119
|
|
|
} |
|
120
|
|
|
if (!is_readable($pathDir)) { |
|
121
|
|
|
return 'not_readable'; |
|
122
|
|
|
} |
|
123
|
|
|
if (!is_writable($pathDir)) { |
|
124
|
|
|
return 'not_writable'; |
|
125
|
|
|
} |
|
126
|
|
|
return 'ok'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public static function commandIsLocked(string $pathDir): bool |
|
130
|
|
|
{ |
|
131
|
|
|
return file_exists($pathDir. '/command.lock'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public static function lockCommand(string $pathDir): void |
|
135
|
|
|
{ |
|
136
|
|
|
if (!self::commandIsLocked($pathDir)) { |
|
137
|
|
|
self::writeFile($pathDir. '/command.lock','1'); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function unlockCommand(string $pathDir): void |
|
142
|
|
|
{ |
|
143
|
|
|
if (self::commandIsLocked($pathDir)) { |
|
144
|
|
|
self::removeFile($pathDir. '/command.lock'); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |