-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathname-generator.php
More file actions
executable file
·192 lines (164 loc) · 5.3 KB
/
name-generator.php
File metadata and controls
executable file
·192 lines (164 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env php
<?php
/**
* PHP implementation of the original `name-generator.sh`.
*
* Behaviour:
* * Uses the environment variables SEPARATOR, NOUN_FILE, ADJ_FILE,
* NOUN_FOLDER, ADJ_FOLDER and counto (the number of lines to emit).
* * If NOUN_FILE / ADJ_FILE are not set, a random regular file is chosen
* from the respective folder.
* * If a folder variable is not set, it defaults to "<cwd>/nouns" and
* "<cwd>/adjectives".
* * The noun is lower‑cased, the adjective keeps its original case.
* * When DEBUG=true a small debug dump is written to STDERR.
* * No greeting is added – the output matches the original shell script
* (e.g., "test_test").
*/
declare(strict_types=1);
/* --------------------------------------------------- *
* Helper utilities
* --------------------------------------------------- */
/**
* Return true if $path points to a regular file.
*/
function is_regular_file(string $path): bool
{
return is_file($path) && is_readable($path);
}
/**
* Pick a random regular file from $folder.
*
* @throws RuntimeException if the folder contains no regular files.
*/
function pick_random_file(string $folder): string
{
$entries = scandir($folder, SCANDIR_SORT_NONE);
if ($entries === false) {
throw new RuntimeException("Cannot read directory: $folder");
}
$files = [];
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$full = $folder . DIRECTORY_SEPARATOR . $entry;
if (is_regular_file($full)) {
$files[] = $full;
}
}
if (count($files) === 0) {
throw new RuntimeException("Folder $folder does not contain any regular files.");
}
return $files[random_int(0, count($files) - 1)];
}
/**
* Resolve a file path from an environment variable or, if not set,
* pick a random file from $folder.
*
* @throws RuntimeException if the env‑var points to a non‑regular file.
*/
function resolve_file(string $envVar, string $folder): string
{
$val = getenv($envVar);
if ($val !== false && trim($val) !== '') {
$path = realpath($val);
if ($path === false || !is_regular_file($path)) {
throw new RuntimeException(
"Environment variable $envVar points to a non‑regular file: $val"
);
}
return $path;
}
return pick_random_file($folder);
}
/**
* Read all non‑empty, trimmed lines from $file.
*
* @return list<string>
*/
function read_nonempty_lines(string $file): array
{
$raw = file($file, FILE_IGNORE_NEW_LINES);
if ($raw === false) {
throw new RuntimeException("Cannot read file: $file");
}
$lines = [];
foreach ($raw as $line) {
$trim = trim($line);
if ($trim !== '') {
$lines[] = $trim;
}
}
return $lines;
}
/**
* Return the number of lines to emit.
*
* 1. Environment variable `counto` (if numeric).
* 2. `tput lines` command (if available).
* 3. Fallback to 24.
*/
function get_count_o(): int
{
$env = getenv('counto');
if ($env !== false && is_numeric($env)) {
return (int)$env;
}
// Try `tput lines`
$output = null;
$returnVar = null;
@exec('tput lines 2>/dev/null', $output, $returnVar);
if ($returnVar === 0 && isset($output[0]) && is_numeric(trim($output[0]))) {
return (int)trim($output[0]);
}
return 24;
}
/* --------------------------------------------------- *
* Configuration (environment → defaults)
* --------------------------------------------------- */
$HERE = getcwd();
$SEPARATOR = getenv('SEPARATOR') ?: '-';
$COUNT_O = get_count_o();
$NOUN_FOLDER = getenv('NOUN_FOLDER')
?: $HERE . DIRECTORY_SEPARATOR . 'nouns';
$ADJ_FOLDER = getenv('ADJ_FOLDER')
?: $HERE . DIRECTORY_SEPARATOR . 'adjectives';
$NOUN_FILE = resolve_file('NOUN_FILE', $NOUN_FOLDER);
$ADJ_FILE = resolve_file('ADJ_FILE', $ADJ_FOLDER);
/* --------------------------------------------------- *
* Load the word lists
* --------------------------------------------------- */
$nounLines = read_nonempty_lines($NOUN_FILE);
$adjLines = read_nonempty_lines($ADJ_FILE);
if (count($nounLines) === 0) {
fwrite(STDERR, "Error: noun list is empty.\n");
exit(1);
}
if (count($adjLines) === 0) {
fwrite(STDERR, "Error: adjective list is empty.\n");
exit(1);
}
/* --------------------------------------------------- *
* Main generation loop
* --------------------------------------------------- */
$debug = getenv('DEBUG') === 'true';
for ($i = 0; $i < $COUNT_O; $i++) {
// Pick random entries
$nounRaw = $nounLines[random_int(0, count($nounLines) - 1)];
$adjRaw = $adjLines[random_int(0, count($adjLines) - 1)];
$noun = strtolower($nounRaw);
$adj = $adjRaw; // keep original case
if ($debug) {
fwrite(STDERR, "DEBUG:\n");
fwrite(STDERR, " adjective : $adj\n");
fwrite(STDERR, " noun : $noun\n");
fwrite(STDERR, " ADJ_FILE : $ADJ_FILE\n");
fwrite(STDERR, " ADJ_FOLDER: $ADJ_FOLDER\n");
fwrite(STDERR, " NOUN_FILE : $NOUN_FILE\n");
fwrite(STDERR, " NOUN_FOLDER: $NOUN_FOLDER\n");
fwrite(STDERR, " $i > $COUNT_O\n");
}
// Output without any greeting, matching the original script
echo $adj . $SEPARATOR . $noun . PHP_EOL;
}