|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2018 Daniel Popiniuc |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace danielgp\io_operations; |
|
28
|
|
|
|
|
29
|
|
|
trait InputOutputFiles |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
use InputOutputFilePermissions; |
|
33
|
|
|
|
|
34
|
|
|
public function checkFileExistance($strFilePath, $strFileName) |
|
35
|
|
|
{ |
|
36
|
|
|
$fName = $this->gluePathWithFileName($strFilePath, $strFileName); |
|
37
|
|
|
if (!file_exists($fName)) { |
|
38
|
|
|
throw new \RuntimeException(sprintf('File %s does not exists!', $fName)); |
|
39
|
|
|
} |
|
40
|
|
|
return $fName; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* returns an array with non-standard holidays from a JSON file |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $strFileName |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getArrayFromJsonFile($strFilePath, $strFileName) |
|
50
|
|
|
{ |
|
51
|
|
|
$jSonContent = $this->getFileJsonContent($strFilePath, $strFileName); |
|
52
|
|
|
$arrayToReturn = json_decode($jSonContent, true); |
|
53
|
|
|
if (json_last_error() != JSON_ERROR_NONE) { |
|
54
|
|
|
$fName = $this->gluePathWithFileName($strFilePath, $strFileName); |
|
55
|
|
|
throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $fName)); |
|
56
|
|
|
} |
|
57
|
|
|
return $arrayToReturn; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns the details about Communicator (current) file |
|
62
|
|
|
* w/o any kind of verification of file existence |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $fileGiven |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getFileDetailsRaw($fileGiven) |
|
68
|
|
|
{ |
|
69
|
|
|
$info = new \SplFileInfo($fileGiven); |
|
70
|
|
|
$aFileBasicDetails = [ |
|
71
|
|
|
'File Extension' => $info->getExtension(), |
|
72
|
|
|
'File Group' => $info->getGroup(), |
|
73
|
|
|
'File Inode' => $info->getInode(), |
|
74
|
|
|
'File Link Target' => ($info->isLink() ? $info->getLinkTarget() : '-'), |
|
75
|
|
|
'File Name' => $info->getBasename('.' . $info->getExtension()), |
|
76
|
|
|
'File Name w. Extension' => $info->getFilename(), |
|
77
|
|
|
'File Owner' => $info->getOwner(), |
|
78
|
|
|
'File Path' => $info->getPath(), |
|
79
|
|
|
'Name' => $info->getRealPath(), |
|
80
|
|
|
'Type' => $info->getType(), |
|
81
|
|
|
]; |
|
82
|
|
|
$aDetails = array_merge($aFileBasicDetails, $this->getFileDetailsRawStatistic($info, $fileGiven)); |
|
83
|
|
|
ksort($aDetails); |
|
84
|
|
|
return $aDetails; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getFileDetailsRawStatistic(\SplFileInfo $info, $fileGiven) |
|
88
|
|
|
{ |
|
89
|
|
|
return [ |
|
90
|
|
|
'File is Dir' => $info->isDir(), |
|
91
|
|
|
'File is Executable' => $info->isExecutable(), |
|
92
|
|
|
'File is File' => $info->isFile(), |
|
93
|
|
|
'File is Link' => $info->isLink(), |
|
94
|
|
|
'File is Readable' => $info->isReadable(), |
|
95
|
|
|
'File is Writable' => $info->isWritable(), |
|
96
|
|
|
'File Permissions' => $this->explainPerms($info->getPerms()), |
|
97
|
|
|
'Size' => $info->getSize(), |
|
98
|
|
|
'Sha1' => sha1_file($fileGiven), |
|
99
|
|
|
'Timestamp Accessed' => $this->getFileTimes($info->getATime()), |
|
100
|
|
|
'Timestamp Changed' => $this->getFileTimes($info->getCTime()), |
|
101
|
|
|
'Timestamp Modified' => $this->getFileTimes($info->getMTime()), |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getFileEntireContent($strInputFile) |
|
106
|
|
|
{ |
|
107
|
|
|
$contentInputFile = file($strInputFile, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); |
|
108
|
|
|
if ($contentInputFile === false) { |
|
109
|
|
|
throw new \RuntimeException(sprintf('Unable to read file %s...', $strInputFile)); |
|
110
|
|
|
} |
|
111
|
|
|
return $contentInputFile; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function getFileTimes($timeAsPhpNumber) |
|
115
|
|
|
{ |
|
116
|
|
|
return [ |
|
117
|
|
|
'PHP number' => $timeAsPhpNumber, |
|
118
|
|
|
'SQL format' => date('Y-m-d H:i:s', $timeAsPhpNumber), |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getMostRecentFile($strFileFirst, $strFileSecond) |
|
123
|
|
|
{ |
|
124
|
|
|
$infoFirst = new \SplFileInfo($strFileFirst); |
|
125
|
|
|
$infoSecond = new \SplFileInfo($strFileSecond); |
|
126
|
|
|
$sReturn = $strFileFirst; |
|
127
|
|
|
if ($infoFirst->getMTime() <= $infoSecond->getMTime()) { |
|
128
|
|
|
$sReturn = $strFileSecond; |
|
129
|
|
|
} |
|
130
|
|
|
return $sReturn; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function getFileJsonContent($strFilePath, $strFileName) |
|
134
|
|
|
{ |
|
135
|
|
|
$fName = $this->checkFileExistance($strFilePath, $strFileName); |
|
136
|
|
|
$fJson = $this->openFileSafelyAndReturnHandle($fName, 'r', 'read'); |
|
137
|
|
|
$fileContent = fread($fJson, ((int) filesize($fName))); |
|
138
|
|
|
fclose($fJson); |
|
139
|
|
|
return $fileContent; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function gluePathWithFileName($strFilePath, $strFileName) |
|
143
|
|
|
{ |
|
144
|
|
|
return $strFilePath . DIRECTORY_SEPARATOR . $strFileName; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function openFileSafelyAndReturnHandle($strFileName, $strFileOperationChar, $strFileOperationName) |
|
148
|
|
|
{ |
|
149
|
|
|
$fHandle = fopen($strFileName, $strFileOperationChar); |
|
150
|
|
|
if ($fHandle === false) { |
|
151
|
|
|
throw new \RuntimeException(sprintf('Unable to open file %s for %s purposes!' |
|
152
|
|
|
. '', $strFileName, $strFileOperationName)); |
|
153
|
|
|
} |
|
154
|
|
|
return $fHandle; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|