|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Functions for console scripts. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Adam "Saibamen" Stachowicz <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Saves string to file. |
|
10
|
|
|
* |
|
11
|
|
|
* @param string $string String to save |
|
12
|
|
|
* @param string $filename Output filename without extension |
|
13
|
|
|
* @param string $fileExtension File extension. Default is '.dat' |
|
14
|
|
|
* |
|
15
|
|
|
* @see generateRandomNumbers() |
|
16
|
|
|
*/ |
|
17
|
|
|
function saveStringToFile($string, $filename, $fileExtension = '.dat') |
|
18
|
|
|
{ |
|
19
|
|
|
// Create dir if not exists |
|
20
|
|
|
if (!is_dir(dirname($filename))) { |
|
21
|
|
|
debug('Creating missing directory: '.dirname($filename)); |
|
22
|
|
|
mkdir(dirname($filename)); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// Warn about overwriting file |
|
26
|
|
|
if (file_exists($filename.$fileExtension)) { |
|
27
|
|
|
text('File '.$filename.$fileExtension.' exists and it will be overwritten!'); |
|
28
|
|
|
getUserConfirm(true); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
text('Saving to file...'); |
|
32
|
|
|
|
|
33
|
|
|
$outputFileBytes = file_put_contents($filename.$fileExtension, $string, LOCK_EX); |
|
34
|
|
|
|
|
35
|
|
|
text('Output file '.$filename.$fileExtension.' generated with '.$outputFileBytes.' bytes.'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get filename from User. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $message Message for User what he must type |
|
42
|
|
|
* |
|
43
|
|
|
* @return string Inserted filename |
|
44
|
|
|
*/ |
|
45
|
|
|
function getFilenameInput($message) |
|
46
|
|
|
{ |
|
47
|
|
|
echo $message.': '; |
|
48
|
|
|
|
|
49
|
|
|
do { |
|
50
|
|
|
$input = trim(fgets(STDIN)); |
|
|
|
|
|
|
51
|
|
|
debug('User input: '.$input); |
|
52
|
|
|
|
|
53
|
|
|
/* |
|
54
|
|
|
* TODO: Invalid characters: |
|
55
|
|
|
* (Windows) \/:*?"<>| (check \ and / only at the end of string - need to test) |
|
56
|
|
|
* (Linux) / |
|
57
|
|
|
*/ |
|
58
|
|
|
$isInputWrong = is_null($input) || empty($input); |
|
59
|
|
|
|
|
60
|
|
|
if ($isInputWrong) { |
|
61
|
|
|
echo 'Please input filename: '; |
|
62
|
|
|
} |
|
63
|
|
|
} while ($isInputWrong); |
|
64
|
|
|
|
|
65
|
|
|
return $input; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get User confirmation. Default is YES. |
|
70
|
|
|
* |
|
71
|
|
|
* @param bool $die If true - script dies if User denial |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool Confirmation result |
|
74
|
|
|
*/ |
|
75
|
|
|
function getUserConfirm($die = false) |
|
76
|
|
|
{ |
|
77
|
|
|
while (1) { |
|
78
|
|
|
echo 'Do you really want to continue? [Y/n]: '; |
|
79
|
|
|
|
|
80
|
|
|
$input = trim(fgets(STDIN)); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// Default is YES |
|
83
|
|
|
if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') { |
|
84
|
|
|
return true; |
|
85
|
|
|
} elseif (strtolower($input) == 'n' || strtolower($input) == 'no') { |
|
86
|
|
|
if ($die) { |
|
87
|
|
|
die('Script terminated by user.'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Fix missing return statement warning. Return true... |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Prints how much time took some action in milliseconds. |
|
100
|
|
|
* |
|
101
|
|
|
* @param float $startTime Time when action started |
|
102
|
|
|
*/ |
|
103
|
|
|
function printEndTime($startTime) |
|
104
|
|
|
{ |
|
105
|
|
|
$endTime = microtime(true) - (float) $startTime; |
|
106
|
|
|
$endTime = number_format((float) $endTime, 4, '.', ''); |
|
107
|
|
|
|
|
108
|
|
|
text('It was done in '.$endTime.' ms.'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Execute text() function if DEBUG is set to true. |
|
113
|
|
|
* |
|
114
|
|
|
* @global bool $DEBUG |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $message Message to print if in DEBUG mode |
|
117
|
|
|
* |
|
118
|
|
|
* @see text() |
|
119
|
|
|
*/ |
|
120
|
|
|
function debug($message) |
|
121
|
|
|
{ |
|
122
|
|
|
if (DEBUG) { |
|
|
|
|
|
|
123
|
|
|
text($message); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Prints message with new lines. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $message Message to print |
|
131
|
|
|
*/ |
|
132
|
|
|
function text($message) |
|
133
|
|
|
{ |
|
134
|
|
|
echo "\n".$message."\n"; |
|
135
|
|
|
} |
|
136
|
|
|
|