ConfigPaths.php
10.0 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2023 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy;
/**
* A Psy Shell configuration path helper.
*/
class ConfigPaths
{
private $configDir;
private $dataDir;
private $runtimeDir;
private $env;
/**
* ConfigPaths constructor.
*
* Optionally provide `configDir`, `dataDir` and `runtimeDir` overrides.
*
* @see self::overrideDirs
*
* @param string[] $overrides Directory overrides
* @param EnvInterface $env
*/
public function __construct(array $overrides = [], ?EnvInterface $env = null)
{
$this->overrideDirs($overrides);
$this->env = $env ?: (\PHP_SAPI === 'cli-server' ? new SystemEnv() : new SuperglobalsEnv());
}
/**
* Provide `configDir`, `dataDir` and `runtimeDir` overrides.
*
* If a key is set but empty, the override will be removed. If it is not set
* at all, any existing override will persist.
*
* @param string[] $overrides Directory overrides
*/
public function overrideDirs(array $overrides)
{
if (\array_key_exists('configDir', $overrides)) {
$this->configDir = $overrides['configDir'] ?: null;
}
if (\array_key_exists('dataDir', $overrides)) {
$this->dataDir = $overrides['dataDir'] ?: null;
}
if (\array_key_exists('runtimeDir', $overrides)) {
$this->runtimeDir = $overrides['runtimeDir'] ?: null;
}
}
/**
* Get the current home directory.
*
* @return string|null
*/
public function homeDir()
{
if ($homeDir = $this->getEnv('HOME') ?: $this->windowsHomeDir()) {
return \strtr($homeDir, '\\', '/');
}
return null;
}
private function windowsHomeDir()
{
if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
$homeDrive = $this->getEnv('HOMEDRIVE');
$homePath = $this->getEnv('HOMEPATH');
if ($homeDrive && $homePath) {
return $homeDrive.'/'.$homePath;
}
}
return null;
}
private function homeConfigDir()
{
if ($homeConfigDir = $this->getEnv('XDG_CONFIG_HOME')) {
return $homeConfigDir;
}
$homeDir = $this->homeDir();
return $homeDir === '/' ? $homeDir.'.config' : $homeDir.'/.config';
}
/**
* Get potential config directory paths.
*
* Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and all
* XDG Base Directory config directories:
*
* http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
*
* @return string[]
*/
public function configDirs(): array
{
if ($this->configDir !== null) {
return [$this->configDir];
}
$configDirs = $this->getEnvArray('XDG_CONFIG_DIRS') ?: ['/etc/xdg'];
return $this->allDirNames(\array_merge([$this->homeConfigDir()], $configDirs));
}
/**
* Get the current home config directory.
*
* Returns the highest precedence home config directory which actually
* exists. If none of them exists, returns the highest precedence home
* config directory (`%APPDATA%/PsySH` on Windows, `~/.config/psysh`
* everywhere else).
*
* @see self::homeConfigDir
*/
public function currentConfigDir(): string
{
if ($this->configDir !== null) {
return $this->configDir;
}
$configDirs = $this->allDirNames([$this->homeConfigDir()]);
foreach ($configDirs as $configDir) {
if (@\is_dir($configDir)) {
return $configDir;
}
}
return $configDirs[0];
}
/**
* Find real config files in config directories.
*
* @param string[] $names Config file names
*
* @return string[]
*/
public function configFiles(array $names): array
{
return $this->allRealFiles($this->configDirs(), $names);
}
/**
* Get potential data directory paths.
*
* If a `dataDir` option was explicitly set, returns an array containing
* just that directory.
*
* Otherwise, it returns `~/.psysh` and all XDG Base Directory data directories:
*
* http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
*
* @return string[]
*/
public function dataDirs(): array
{
if ($this->dataDir !== null) {
return [$this->dataDir];
}
$homeDataDir = $this->getEnv('XDG_DATA_HOME') ?: $this->homeDir().'/.local/share';
$dataDirs = $this->getEnvArray('XDG_DATA_DIRS') ?: ['/usr/local/share', '/usr/share'];
return $this->allDirNames(\array_merge([$homeDataDir], $dataDirs));
}
/**
* Find real data files in config directories.
*
* @param string[] $names Config file names
*
* @return string[]
*/
public function dataFiles(array $names): array
{
return $this->allRealFiles($this->dataDirs(), $names);
}
/**
* Get a runtime directory.
*
* Defaults to `/psysh` inside the system's temp dir.
*/
public function runtimeDir(): string
{
if ($this->runtimeDir !== null) {
return $this->runtimeDir;
}
// Fallback to a boring old folder in the system temp dir.
$runtimeDir = $this->getEnv('XDG_RUNTIME_DIR') ?: \sys_get_temp_dir();
return \strtr($runtimeDir, '\\', '/').'/psysh';
}
/**
* Get a list of directories in PATH.
*
* If $PATH is unset/empty it defaults to '/usr/sbin:/usr/bin:/sbin:/bin'.
*
* @return string[]
*/
public function pathDirs(): array
{
return $this->getEnvArray('PATH') ?: ['/usr/sbin', '/usr/bin', '/sbin', '/bin'];
}
/**
* Locate a command (an executable) in $PATH.
*
* Behaves like 'command -v COMMAND' or 'which COMMAND'.
* If $PATH is unset/empty it defaults to '/usr/sbin:/usr/bin:/sbin:/bin'.
*
* @param string $command the executable to locate
*
* @return string
*/
public function which($command)
{
foreach ($this->pathDirs() as $path) {
$fullpath = $path.\DIRECTORY_SEPARATOR.$command;
if (@\is_file($fullpath) && @\is_executable($fullpath)) {
return $fullpath;
}
}
return null;
}
/**
* Get all PsySH directory name candidates given a list of base directories.
*
* This expects that XDG-compatible directory paths will be passed in.
* `psysh` will be added to each of $baseDirs, and we'll throw in `~/.psysh`
* and a couple of Windows-friendly paths as well.
*
* @param string[] $baseDirs base directory paths
*
* @return string[]
*/
private function allDirNames(array $baseDirs): array
{
$dirs = \array_map(function ($dir) {
return \strtr($dir, '\\', '/').'/psysh';
}, $baseDirs);
// Add ~/.psysh
if ($home = $this->getEnv('HOME')) {
$dirs[] = \strtr($home, '\\', '/').'/.psysh';
}
// Add some Windows specific ones :)
if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
if ($appData = $this->getEnv('APPDATA')) {
// AppData gets preference
\array_unshift($dirs, \strtr($appData, '\\', '/').'/PsySH');
}
if ($windowsHomeDir = $this->windowsHomeDir()) {
$dir = \strtr($windowsHomeDir, '\\', '/').'/.psysh';
if (!\in_array($dir, $dirs)) {
$dirs[] = $dir;
}
}
}
return $dirs;
}
/**
* Given a list of directories, and a list of filenames, find the ones that
* are real files.
*
* @return string[]
*/
private function allRealFiles(array $dirNames, array $fileNames): array
{
$files = [];
foreach ($dirNames as $dir) {
foreach ($fileNames as $name) {
$file = $dir.'/'.$name;
if (@\is_file($file)) {
$files[] = $file;
}
}
}
return $files;
}
/**
* Ensure that $dir exists and is writable.
*
* Generates E_USER_NOTICE error if the directory is not writable or creatable.
*
* @param string $dir
*
* @return bool False if directory exists but is not writeable, or cannot be created
*/
public static function ensureDir(string $dir): bool
{
if (!\is_dir($dir)) {
// Just try making it and see if it works
@\mkdir($dir, 0700, true);
}
if (!\is_dir($dir) || !\is_writable($dir)) {
\trigger_error(\sprintf('Writing to directory %s is not allowed.', $dir), \E_USER_NOTICE);
return false;
}
return true;
}
/**
* Ensure that $file exists and is writable, make the parent directory if necessary.
*
* Generates E_USER_NOTICE error if either $file or its directory is not writable.
*
* @param string $file
*
* @return string|false Full path to $file, or false if file is not writable
*/
public static function touchFileWithMkdir(string $file)
{
if (\file_exists($file)) {
if (\is_writable($file)) {
return $file;
}
\trigger_error(\sprintf('Writing to %s is not allowed.', $file), \E_USER_NOTICE);
return false;
}
if (!self::ensureDir(\dirname($file))) {
return false;
}
\touch($file);
return $file;
}
private function getEnv($key)
{
return $this->env->get($key);
}
private function getEnvArray($key)
{
if ($value = $this->getEnv($key)) {
return \explode(\PATH_SEPARATOR, $value);
}
return null;
}
}