1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Includes/File.php. |
4
|
|
|
* |
5
|
|
|
* @author Adam "Saibamen" Stachowicz <[email protected]> |
6
|
|
|
* @license MIT |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/Saibamen/Generate-Sort-Numbers |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Includes; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Functions for operating on files. |
15
|
|
|
*/ |
16
|
|
|
class File |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Save array to file |
20
|
|
|
* |
21
|
|
|
* @param array $array Array to sort |
22
|
|
|
* @param string $filename Filename without extension |
23
|
|
|
* @param string|mixed $delimiter Delimiter. Default is ' ' |
24
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
25
|
|
|
*/ |
26
|
|
|
public static function saveArrayToFile($array, $filename, $delimiter = ' ', $fileExtension = '.dat') |
27
|
|
|
{ |
28
|
|
|
$arrayCount = count($array); |
29
|
|
|
|
30
|
|
|
Text::message('Saving to file...'); |
31
|
|
|
|
32
|
|
|
$saveStart = microtime(true); |
33
|
|
|
|
34
|
|
|
$file = fopen($filename.$fileExtension, 'w'); |
35
|
|
|
|
36
|
|
|
$i = 0; |
37
|
|
|
foreach ($array as $value) { |
38
|
|
|
$outputString = $value.$delimiter; |
39
|
|
|
|
40
|
|
|
// Remove last delimiter |
41
|
|
|
if (++$i === $arrayCount) { |
42
|
|
|
$outputString = rtrim($outputString, $delimiter); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
fwrite($file, $outputString); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
fclose($file); |
49
|
|
|
|
50
|
|
|
Text::printTimeDuration($saveStart); |
51
|
|
|
Text::message('Output file '.$filename.$fileExtension.' saved with '.filesize($filename.$fileExtension).' bytes.'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Warn about overwriting file if exists and empty it. |
56
|
|
|
* |
57
|
|
|
* @param string $filename Filename without extension |
58
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
59
|
|
|
* |
60
|
|
|
* @see Input::dieOnDenyUserConfirm() |
61
|
|
|
*/ |
62
|
|
|
public static function checkIfFileExistsAndDeleteContent($filename, $fileExtension = '.dat') |
63
|
|
|
{ |
64
|
|
|
if (file_exists($filename.$fileExtension)) { |
65
|
|
|
Text::message('File '.$filename.$fileExtension.' exists and it will be overwritten!'); |
66
|
|
|
|
67
|
|
|
Input::dieOnDenyUserConfirm(); |
68
|
|
|
|
69
|
|
|
// Empty the file |
70
|
|
|
file_put_contents($filename.$fileExtension, ''); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create dir from filename if not exists. |
76
|
|
|
* |
77
|
|
|
* @param string $filename Filename |
78
|
|
|
*/ |
79
|
|
|
public static function createMissingDirectory($filename) |
80
|
|
|
{ |
81
|
|
|
if (!is_dir(dirname($filename))) { |
82
|
|
|
Text::debug('Creating missing directory: '.dirname($filename)); |
83
|
|
|
mkdir(dirname($filename)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get array from file content. |
89
|
|
|
* |
90
|
|
|
* @param string $filename Filename without extension |
91
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
92
|
|
|
* @param string|mixed $delimiter Delimiter. Default is ' ' |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public static function getArrayFromFile($filename, $fileExtension = '.dat', $delimiter = ' ') |
97
|
|
|
{ |
98
|
|
|
$array = array(); |
99
|
|
|
|
100
|
|
|
if (file_exists($filename.$fileExtension)) { |
101
|
|
|
$array = explode($delimiter, file_get_contents($filename.$fileExtension)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $array; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|