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
|
|
|
use InputOutputFilePermissions; |
32
|
|
|
|
33
|
|
|
public function checkFileExistance($strFilePath, $strFileName) |
34
|
|
|
{ |
35
|
|
|
$fName = $this->gluePathWithFileName($strFilePath, $strFileName); |
36
|
|
|
if (!file_exists($fName)) { |
37
|
|
|
throw new \RuntimeException(sprintf('File %s does not exists!', $fName)); |
38
|
|
|
} |
39
|
|
|
return $fName; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* returns an array with non-standard holidays from a JSON file |
44
|
|
|
* |
45
|
|
|
* @param string $strFileName |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function getArrayFromJsonFile($strFilePath, $strFileName) |
49
|
|
|
{ |
50
|
|
|
$jSonContent = $this->getFileJsonContent($strFilePath, $strFileName); |
51
|
|
|
$arrayToReturn = json_decode($jSonContent, true); |
52
|
|
|
if (json_last_error() != JSON_ERROR_NONE) { |
53
|
|
|
$fName = $this->gluePathWithFileName($strFilePath, $strFileName); |
54
|
|
|
throw new \RuntimeException(sprintf('Unable to interpret JSON from %s file...', $fName)); |
55
|
|
|
} |
56
|
|
|
return $arrayToReturn; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the details about Communicator (current) file |
61
|
|
|
* w/o any kind of verification of file existence |
62
|
|
|
* |
63
|
|
|
* @param string $fileGiven |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function getFileDetailsRaw($fileGiven) |
67
|
|
|
{ |
68
|
|
|
$info = new \SplFileInfo($fileGiven); |
69
|
|
|
$aFileBasicDetails = [ |
70
|
|
|
'File Extension' => $info->getExtension(), |
71
|
|
|
'File Group' => $info->getGroup(), |
72
|
|
|
'File Inode' => $info->getInode(), |
73
|
|
|
'File Link Target' => ($info->isLink() ? $info->getLinkTarget() : '-'), |
74
|
|
|
'File Name' => $info->getBasename('.' . $info->getExtension()), |
75
|
|
|
'File Name w. Extension' => $info->getFilename(), |
76
|
|
|
'File Owner' => $info->getOwner(), |
77
|
|
|
'File Path' => $info->getPath(), |
78
|
|
|
'Name' => $info->getRealPath(), |
79
|
|
|
'Type' => $info->getType(), |
80
|
|
|
]; |
81
|
|
|
$aDetails = array_merge($aFileBasicDetails, $this->getFileDetailsRawStatistic($info, $fileGiven)); |
82
|
|
|
ksort($aDetails); |
83
|
|
|
return $aDetails; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getFileDetailsRawStatistic(\SplFileInfo $info, $fileGiven) |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
'File is Dir' => $info->isDir(), |
90
|
|
|
'File is Executable' => $info->isExecutable(), |
91
|
|
|
'File is File' => $info->isFile(), |
92
|
|
|
'File is Link' => $info->isLink(), |
93
|
|
|
'File is Readable' => $info->isReadable(), |
94
|
|
|
'File is Writable' => $info->isWritable(), |
95
|
|
|
'File Permissions' => $this->explainPerms($info->getPerms()), |
96
|
|
|
'Size' => $info->getSize(), |
97
|
|
|
'Sha1' => sha1_file($fileGiven), |
98
|
|
|
'Sha256' => hash_file('sha256', $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
|
|
|
$strFileNameFirst = $this->checkFileExistance('', $strFileFirst); |
125
|
|
|
$sReturn = $strFileFirst; |
126
|
|
|
if (file_exists($strFileSecond)) { |
127
|
|
|
$infoFirst = new \SplFileInfo($strFileNameFirst); |
128
|
|
|
$strFileNameSecond = $this->checkFileExistance('', $strFileSecond); |
129
|
|
|
$infoSecond = new \SplFileInfo($strFileNameSecond); |
130
|
|
|
$sReturn = $strFileFirst; |
131
|
|
|
if ($infoFirst->getMTime() <= $infoSecond->getMTime()) { |
132
|
|
|
$sReturn = $strFileSecond; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
return $sReturn; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getFileJsonContent($strFilePath, $strFileName) |
139
|
|
|
{ |
140
|
|
|
$fName = $this->checkFileExistance($strFilePath, $strFileName); |
141
|
|
|
$fJson = $this->openFileSafelyAndReturnHandle($fName, 'r', 'read'); |
142
|
|
|
$fileContent = fread($fJson, ((int) filesize($fName))); |
143
|
|
|
fclose($fJson); |
144
|
|
|
return $fileContent; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function gluePathWithFileName($strFilePath, $strFileName) |
148
|
|
|
{ |
149
|
|
|
$sReturn = $strFileName; |
150
|
|
|
if (strpos($strFileName, DIRECTORY_SEPARATOR) === false) { |
151
|
|
|
$sReturn = $strFilePath . DIRECTORY_SEPARATOR . $strFileName; |
152
|
|
|
} |
153
|
|
|
return $sReturn; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function openFileSafelyAndReturnHandle($strFileName, $strFileOperationChar, $strFileOperationName) |
157
|
|
|
{ |
158
|
|
|
$fHandle = fopen($strFileName, $strFileOperationChar); |
159
|
|
|
if ($fHandle === false) { |
160
|
|
|
throw new \RuntimeException(sprintf('Unable to open file %s for %s purposes!' |
161
|
|
|
. '', $strFileName, $strFileOperationName)); |
162
|
|
|
} |
163
|
|
|
return $fHandle; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|