|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* The MIT License (MIT) |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
11
|
|
|
* in the Software without restriction, including without limitation the rights |
|
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
17
|
|
|
* copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
25
|
|
|
* SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace danielgp\common_lib; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* usefull functions to get quick results |
|
33
|
|
|
* |
|
34
|
|
|
* @author Daniel Popiniuc |
|
35
|
|
|
*/ |
|
36
|
|
|
trait CommonBasic |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns the details about Communicator (current) file |
|
41
|
|
|
* w/o any kind of verification of file existance |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $fileGiven |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getFileDetailsRaw($fileGiven) |
|
47
|
|
|
{ |
|
48
|
|
|
$info = new \SplFileInfo($fileGiven); |
|
49
|
|
|
$aFileBasicDetails = [ |
|
50
|
|
|
'File Extension' => $info->getExtension(), |
|
51
|
|
|
'File Group' => $info->getGroup(), |
|
52
|
|
|
'File Inode' => $info->getInode(), |
|
53
|
|
|
'File Link Target' => ($info->isLink() ? $info->getLinkTarget() : '-'), |
|
54
|
|
|
'File Name' => $info->getBasename('.' . $info->getExtension()), |
|
55
|
|
|
'File Name w. Extension' => $info->getFilename(), |
|
56
|
|
|
'File Owner' => $info->getOwner(), |
|
57
|
|
|
'File Path' => $info->getPath(), |
|
58
|
|
|
'Name' => $info->getRealPath(), |
|
59
|
|
|
'Type' => $info->getType(), |
|
60
|
|
|
]; |
|
61
|
|
|
$aDetails = array_merge($aFileBasicDetails, $this->getFileDetailsRawStatistic($info, $fileGiven)); |
|
62
|
|
|
ksort($aDetails); |
|
63
|
|
|
return $aDetails; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function getFileDetailsRawStatistic(\SplFileInfo $info, $fileGiven) |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
'File is Dir' => $info->isDir(), |
|
70
|
|
|
'File is Executable' => $info->isExecutable(), |
|
71
|
|
|
'File is File' => $info->isFile(), |
|
72
|
|
|
'File is Link' => $info->isLink(), |
|
73
|
|
|
'File is Readable' => $info->isReadable(), |
|
74
|
|
|
'File is Writable' => $info->isWritable(), |
|
75
|
|
|
'File Permissions' => $this->explainPerms($info->getPerms()), |
|
|
|
|
|
|
76
|
|
|
'Size' => $info->getSize(), |
|
77
|
|
|
'Sha1' => sha1_file($fileGiven), |
|
78
|
|
|
'Timestamp Accessed' => $this->getFileTimes($info->getATime()), |
|
79
|
|
|
'Timestamp Changed' => $this->getFileTimes($info->getCTime()), |
|
80
|
|
|
'Timestamp Modified' => $this->getFileTimes($info->getMTime()), |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function getFileTimes($timeAsPhpNumber) |
|
85
|
|
|
{ |
|
86
|
|
|
return [ |
|
87
|
|
|
'PHP number' => $timeAsPhpNumber, |
|
88
|
|
|
'SQL format' => date('Y-m-d H:i:s', $timeAsPhpNumber), |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Moves files into another folder |
|
94
|
|
|
* |
|
95
|
|
|
* @param type $sourcePath |
|
96
|
|
|
* @param type $targetPath |
|
97
|
|
|
* @return type |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function moveFilesIntoTargetFolder($sourcePath, $targetPath) |
|
100
|
|
|
{ |
|
101
|
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem(); |
|
102
|
|
|
$filesystem->mirror($sourcePath, $targetPath); |
|
103
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
|
104
|
|
|
$iterator = $finder->files()->ignoreUnreadableDirs(true)->followLinks()->in($sourcePath); |
|
105
|
|
|
$sFiles = []; |
|
106
|
|
|
foreach ($iterator as $file) { |
|
107
|
|
|
$relativePathFile = str_replace($sourcePath, '', $file->getRealPath()); |
|
108
|
|
|
if (!file_exists($targetPath . $relativePathFile)) { |
|
109
|
|
|
$sFiles[$relativePathFile] = $targetPath . $relativePathFile; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
return $this->setArrayToJson($sFiles); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function removeFilesDecision($inputArray) |
|
116
|
|
|
{ |
|
117
|
|
|
$proceedWithDeletion = false; |
|
118
|
|
|
if (is_array($inputArray)) { |
|
119
|
|
|
if (!isset($inputArray['path'])) { |
|
120
|
|
|
return '`path` has not been provided'; |
|
121
|
|
|
} elseif (!isset($inputArray['dateRule'])) { |
|
122
|
|
|
return '`dateRule` has not been provided'; |
|
123
|
|
|
} |
|
124
|
|
|
$proceedWithDeletion = true; |
|
125
|
|
|
} |
|
126
|
|
|
return $proceedWithDeletion; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Remove files older than given rule |
|
131
|
|
|
* (both Access time and Modified time will be checked |
|
132
|
|
|
* and only if both matches removal will take place) |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $inputArray |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function removeFilesOlderThanGivenRule($inputArray) |
|
138
|
|
|
{ |
|
139
|
|
|
$aFiles = $this->retrieveFilesOlderThanGivenRule($inputArray); |
|
|
|
|
|
|
140
|
|
|
if (is_null($aFiles)) { |
|
141
|
|
|
return $aFiles; |
|
142
|
|
|
} |
|
143
|
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem(); |
|
144
|
|
|
$filesystem->remove($aFiles); |
|
145
|
|
|
return $this->setArrayToJson($aFiles); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function retrieveFilesOlderThanGivenRule($inputArray) |
|
149
|
|
|
{ |
|
150
|
|
|
$proceedWithRetrieving = $this->removeFilesDecision($inputArray); |
|
151
|
|
|
if ($proceedWithRetrieving) { |
|
152
|
|
|
$finder = new \Symfony\Component\Finder\Finder(); |
|
153
|
|
|
$iterator = $finder->files()->ignoreUnreadableDirs(true)->followLinks()->in($inputArray['path']); |
|
154
|
|
|
$aFiles = null; |
|
155
|
|
|
foreach ($iterator as $file) { |
|
156
|
|
|
if ($file->getATime() < strtotime($inputArray['dateRule'])) { |
|
157
|
|
|
$aFiles[] = $file->getRealPath(); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
return $aFiles; |
|
161
|
|
|
} |
|
162
|
|
|
return null; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Replace space with break line for each key element |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $aElements |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function setArrayToArrayKbr(array $aElements) |
|
172
|
|
|
{ |
|
173
|
|
|
$aReturn = []; |
|
174
|
|
|
foreach ($aElements as $key => $value) { |
|
175
|
|
|
$aReturn[str_replace(' ', '<br/>', $key)] = $value; |
|
176
|
|
|
} |
|
177
|
|
|
return $aReturn; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Converts a single-child array into an parent-child one |
|
182
|
|
|
* |
|
183
|
|
|
* @param type $inArray |
|
184
|
|
|
* @return type |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function setArrayValuesAsKey(array $inArray) |
|
187
|
|
|
{ |
|
188
|
|
|
$outArray = array_combine($inArray, $inArray); |
|
189
|
|
|
ksort($outArray); |
|
190
|
|
|
return $outArray; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Converts an array into JSON string |
|
195
|
|
|
* |
|
196
|
|
|
* @param array $inArray |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function setArrayToJson(array $inArray) |
|
200
|
|
|
{ |
|
201
|
|
|
$rtrn = utf8_encode(json_encode($inArray, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); |
|
202
|
|
|
$jsonError = $this->setJsonErrorInPlainEnglish(); |
|
203
|
|
|
if (is_null($jsonError)) { |
|
204
|
|
|
return $rtrn; |
|
205
|
|
|
} else { |
|
206
|
|
|
return $jsonError; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Provides a list of all known JSON errors and their description |
|
212
|
|
|
* |
|
213
|
|
|
* @return type |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function setJsonErrorInPlainEnglish() |
|
216
|
|
|
{ |
|
217
|
|
|
$knownErrors = [ |
|
218
|
|
|
JSON_ERROR_NONE => null, |
|
219
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
|
220
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
|
221
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
|
222
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
|
223
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
|
224
|
|
|
]; |
|
225
|
|
|
$currentError = json_last_error(); |
|
226
|
|
|
$sReturn = null; |
|
227
|
|
|
if (in_array($currentError, $knownErrors)) { |
|
228
|
|
|
$sReturn = $knownErrors[$currentError]; |
|
229
|
|
|
} |
|
230
|
|
|
return $sReturn; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.