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 $fileExtension File extension. Default is '.dat' |
24
|
|
|
* @param string|mixed $delimiter Delimiter. Default is ' ' |
25
|
|
|
*/ |
26
|
|
|
public static function saveArrayToFile($array, $filename, $fileExtension = '.dat', $delimiter = ' ') |
27
|
|
|
{ |
28
|
|
|
$chunkSize = 20; |
29
|
|
|
$chunkedArray = array_chunk($array, $chunkSize); |
30
|
|
|
$chunkedArrayCount = count($chunkedArray); |
31
|
|
|
|
32
|
|
|
Text::message('Saving to file...'); |
33
|
|
|
|
34
|
|
|
$saveStart = microtime(true); |
35
|
|
|
|
36
|
|
|
$file = fopen($filename.$fileExtension, 'w'); |
37
|
|
|
$outputString = null; |
38
|
|
|
$currentArrayItem = 0; |
39
|
|
|
|
40
|
|
|
foreach ($chunkedArray as $chunk) { |
41
|
|
|
foreach ($chunk as $value) { |
42
|
|
|
$outputString .= $value.$delimiter; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Remove last delimiter |
46
|
|
|
if (++$currentArrayItem === $chunkedArrayCount) { |
47
|
|
|
$outputString = rtrim($outputString, $delimiter); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
fwrite($file, $outputString); |
51
|
|
|
$outputString = null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
fclose($file); |
55
|
|
|
|
56
|
|
|
Text::printTimeDuration($saveStart); |
57
|
|
|
Text::message('Output file '.$filename.$fileExtension.' saved with '.filesize($filename.$fileExtension).' bytes.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Warn about overwriting file if exists and empty it. |
62
|
|
|
* |
63
|
|
|
* @param string $filename Filename without extension |
64
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
65
|
|
|
* |
66
|
|
|
* @see Input::dieOnDenyUserConfirm() |
67
|
|
|
*/ |
68
|
|
|
public static function checkIfFileExistsAndDeleteContent($filename, $fileExtension = '.dat') |
69
|
|
|
{ |
70
|
|
|
if (file_exists($filename.$fileExtension)) { |
71
|
|
|
Text::message('File '.$filename.$fileExtension.' exists and it will be overwritten!'); |
72
|
|
|
|
73
|
|
|
Input::dieOnDenyUserConfirm(); |
74
|
|
|
|
75
|
|
|
// Empty the file |
76
|
|
|
file_put_contents($filename.$fileExtension, ''); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create dir from filename if not exists. |
82
|
|
|
* |
83
|
|
|
* @param string $filename Filename |
84
|
|
|
*/ |
85
|
|
|
public static function createMissingDirectory($filename) |
86
|
|
|
{ |
87
|
|
|
if (!is_dir(dirname($filename))) { |
88
|
|
|
Text::debug('Creating missing directory: '.dirname($filename)); |
89
|
|
|
mkdir(dirname($filename), 0777, true); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get array from file content. |
95
|
|
|
* |
96
|
|
|
* @param string $filename Filename without extension |
97
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
98
|
|
|
* @param string|mixed $delimiter Delimiter. Default is ' ' |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public static function getArrayFromFile($filename, $fileExtension = '.dat', $delimiter = ' ') |
103
|
|
|
{ |
104
|
|
|
$array = array(); |
105
|
|
|
|
106
|
|
|
if (file_exists($filename.$fileExtension)) { |
107
|
|
|
$array = explode($delimiter, file_get_contents($filename.$fileExtension)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $array; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|