ConfigPaths.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy;
  11. /**
  12. * A Psy Shell configuration path helper.
  13. */
  14. class ConfigPaths
  15. {
  16. private $configDir;
  17. private $dataDir;
  18. private $runtimeDir;
  19. private $env;
  20. /**
  21. * ConfigPaths constructor.
  22. *
  23. * Optionally provide `configDir`, `dataDir` and `runtimeDir` overrides.
  24. *
  25. * @see self::overrideDirs
  26. *
  27. * @param string[] $overrides Directory overrides
  28. * @param EnvInterface $env
  29. */
  30. public function __construct(array $overrides = [], EnvInterface $env = null)
  31. {
  32. $this->overrideDirs($overrides);
  33. $this->env = $env ?: (\PHP_SAPI === 'cli-server' ? new SystemEnv() : new SuperglobalsEnv());
  34. }
  35. /**
  36. * Provide `configDir`, `dataDir` and `runtimeDir` overrides.
  37. *
  38. * If a key is set but empty, the override will be removed. If it is not set
  39. * at all, any existing override will persist.
  40. *
  41. * @param string[] $overrides Directory overrides
  42. */
  43. public function overrideDirs(array $overrides)
  44. {
  45. if (\array_key_exists('configDir', $overrides)) {
  46. $this->configDir = $overrides['configDir'] ?: null;
  47. }
  48. if (\array_key_exists('dataDir', $overrides)) {
  49. $this->dataDir = $overrides['dataDir'] ?: null;
  50. }
  51. if (\array_key_exists('runtimeDir', $overrides)) {
  52. $this->runtimeDir = $overrides['runtimeDir'] ?: null;
  53. }
  54. }
  55. /**
  56. * Get the current home directory.
  57. *
  58. * @return string|null
  59. */
  60. public function homeDir()
  61. {
  62. if ($homeDir = $this->getEnv('HOME') ?: $this->windowsHomeDir()) {
  63. return \strtr($homeDir, '\\', '/');
  64. }
  65. return null;
  66. }
  67. private function windowsHomeDir()
  68. {
  69. if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
  70. $homeDrive = $this->getEnv('HOMEDRIVE');
  71. $homePath = $this->getEnv('HOMEPATH');
  72. if ($homeDrive && $homePath) {
  73. return $homeDrive.'/'.$homePath;
  74. }
  75. }
  76. return null;
  77. }
  78. private function homeConfigDir()
  79. {
  80. if ($homeConfigDir = $this->getEnv('XDG_CONFIG_HOME')) {
  81. return $homeConfigDir;
  82. }
  83. $homeDir = $this->homeDir();
  84. return $homeDir === '/' ? $homeDir.'.config' : $homeDir.'/.config';
  85. }
  86. /**
  87. * Get potential config directory paths.
  88. *
  89. * Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and all
  90. * XDG Base Directory config directories:
  91. *
  92. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  93. *
  94. * @return string[]
  95. */
  96. public function configDirs(): array
  97. {
  98. if ($this->configDir !== null) {
  99. return [$this->configDir];
  100. }
  101. $configDirs = $this->getEnvArray('XDG_CONFIG_DIRS') ?: ['/etc/xdg'];
  102. return $this->allDirNames(\array_merge([$this->homeConfigDir()], $configDirs));
  103. }
  104. /**
  105. * @deprecated
  106. */
  107. public static function getConfigDirs(): array
  108. {
  109. return (new self())->configDirs();
  110. }
  111. /**
  112. * Get potential home config directory paths.
  113. *
  114. * Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and the
  115. * XDG Base Directory home config directory:
  116. *
  117. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  118. *
  119. * @deprecated
  120. *
  121. * @return string[]
  122. */
  123. public static function getHomeConfigDirs(): array
  124. {
  125. // Not quite the same, but this is deprecated anyway /shrug
  126. return self::getConfigDirs();
  127. }
  128. /**
  129. * Get the current home config directory.
  130. *
  131. * Returns the highest precedence home config directory which actually
  132. * exists. If none of them exists, returns the highest precedence home
  133. * config directory (`%APPDATA%/PsySH` on Windows, `~/.config/psysh`
  134. * everywhere else).
  135. *
  136. * @see self::homeConfigDir
  137. */
  138. public function currentConfigDir(): string
  139. {
  140. if ($this->configDir !== null) {
  141. return $this->configDir;
  142. }
  143. $configDirs = $this->allDirNames([$this->homeConfigDir()]);
  144. foreach ($configDirs as $configDir) {
  145. if (@\is_dir($configDir)) {
  146. return $configDir;
  147. }
  148. }
  149. return $configDirs[0];
  150. }
  151. /**
  152. * @deprecated
  153. */
  154. public static function getCurrentConfigDir(): string
  155. {
  156. return (new self())->currentConfigDir();
  157. }
  158. /**
  159. * Find real config files in config directories.
  160. *
  161. * @param string[] $names Config file names
  162. *
  163. * @return string[]
  164. */
  165. public function configFiles(array $names): array
  166. {
  167. return $this->allRealFiles($this->configDirs(), $names);
  168. }
  169. /**
  170. * @deprecated
  171. */
  172. public static function getConfigFiles(array $names, $configDir = null): array
  173. {
  174. return (new self(['configDir' => $configDir]))->configFiles($names);
  175. }
  176. /**
  177. * Get potential data directory paths.
  178. *
  179. * If a `dataDir` option was explicitly set, returns an array containing
  180. * just that directory.
  181. *
  182. * Otherwise, it returns `~/.psysh` and all XDG Base Directory data directories:
  183. *
  184. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  185. *
  186. * @return string[]
  187. */
  188. public function dataDirs(): array
  189. {
  190. if ($this->dataDir !== null) {
  191. return [$this->dataDir];
  192. }
  193. $homeDataDir = $this->getEnv('XDG_DATA_HOME') ?: $this->homeDir().'/.local/share';
  194. $dataDirs = $this->getEnvArray('XDG_DATA_DIRS') ?: ['/usr/local/share', '/usr/share'];
  195. return $this->allDirNames(\array_merge([$homeDataDir], $dataDirs));
  196. }
  197. /**
  198. * @deprecated
  199. */
  200. public static function getDataDirs(): array
  201. {
  202. return (new self())->dataDirs();
  203. }
  204. /**
  205. * Find real data files in config directories.
  206. *
  207. * @param string[] $names Config file names
  208. *
  209. * @return string[]
  210. */
  211. public function dataFiles(array $names): array
  212. {
  213. return $this->allRealFiles($this->dataDirs(), $names);
  214. }
  215. /**
  216. * @deprecated
  217. */
  218. public static function getDataFiles(array $names, $dataDir = null): array
  219. {
  220. return (new self(['dataDir' => $dataDir]))->dataFiles($names);
  221. }
  222. /**
  223. * Get a runtime directory.
  224. *
  225. * Defaults to `/psysh` inside the system's temp dir.
  226. */
  227. public function runtimeDir(): string
  228. {
  229. if ($this->runtimeDir !== null) {
  230. return $this->runtimeDir;
  231. }
  232. // Fallback to a boring old folder in the system temp dir.
  233. $runtimeDir = $this->getEnv('XDG_RUNTIME_DIR') ?: \sys_get_temp_dir();
  234. return \strtr($runtimeDir, '\\', '/').'/psysh';
  235. }
  236. /**
  237. * @deprecated
  238. */
  239. public static function getRuntimeDir(): string
  240. {
  241. return (new self())->runtimeDir();
  242. }
  243. /**
  244. * Get a list of directories in PATH.
  245. *
  246. * If $PATH is unset/empty it defaults to '/usr/sbin:/usr/bin:/sbin:/bin'.
  247. *
  248. * @return string[]
  249. */
  250. public function pathDirs(): array
  251. {
  252. return $this->getEnvArray('PATH') ?: ['/usr/sbin', '/usr/bin', '/sbin', '/bin'];
  253. }
  254. /**
  255. * Locate a command (an executable) in $PATH.
  256. *
  257. * Behaves like 'command -v COMMAND' or 'which COMMAND'.
  258. * If $PATH is unset/empty it defaults to '/usr/sbin:/usr/bin:/sbin:/bin'.
  259. *
  260. * @param string $command the executable to locate
  261. *
  262. * @return string
  263. */
  264. public function which($command)
  265. {
  266. foreach ($this->pathDirs() as $path) {
  267. $fullpath = $path.\DIRECTORY_SEPARATOR.$command;
  268. if (@\is_file($fullpath) && @\is_executable($fullpath)) {
  269. return $fullpath;
  270. }
  271. }
  272. return null;
  273. }
  274. /**
  275. * Get all PsySH directory name candidates given a list of base directories.
  276. *
  277. * This expects that XDG-compatible directory paths will be passed in.
  278. * `psysh` will be added to each of $baseDirs, and we'll throw in `~/.psysh`
  279. * and a couple of Windows-friendly paths as well.
  280. *
  281. * @param string[] $baseDirs base directory paths
  282. *
  283. * @return string[]
  284. */
  285. private function allDirNames(array $baseDirs): array
  286. {
  287. $dirs = \array_map(function ($dir) {
  288. return \strtr($dir, '\\', '/').'/psysh';
  289. }, $baseDirs);
  290. // Add ~/.psysh
  291. if ($home = $this->getEnv('HOME')) {
  292. $dirs[] = \strtr($home, '\\', '/').'/.psysh';
  293. }
  294. // Add some Windows specific ones :)
  295. if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
  296. if ($appData = $this->getEnv('APPDATA')) {
  297. // AppData gets preference
  298. \array_unshift($dirs, \strtr($appData, '\\', '/').'/PsySH');
  299. }
  300. if ($windowsHomeDir = $this->windowsHomeDir()) {
  301. $dir = \strtr($windowsHomeDir, '\\', '/').'/.psysh';
  302. if (!\in_array($dir, $dirs)) {
  303. $dirs[] = $dir;
  304. }
  305. }
  306. }
  307. return $dirs;
  308. }
  309. /**
  310. * Given a list of directories, and a list of filenames, find the ones that
  311. * are real files.
  312. *
  313. * @return string[]
  314. */
  315. private function allRealFiles(array $dirNames, array $fileNames): array
  316. {
  317. $files = [];
  318. foreach ($dirNames as $dir) {
  319. foreach ($fileNames as $name) {
  320. $file = $dir.'/'.$name;
  321. if (@\is_file($file)) {
  322. $files[] = $file;
  323. }
  324. }
  325. }
  326. return $files;
  327. }
  328. /**
  329. * Ensure that $dir exists and is writable.
  330. *
  331. * Generates E_USER_NOTICE error if the directory is not writable or creatable.
  332. *
  333. * @param string $dir
  334. *
  335. * @return bool False if directory exists but is not writeable, or cannot be created
  336. */
  337. public static function ensureDir(string $dir): bool
  338. {
  339. if (!\is_dir($dir)) {
  340. // Just try making it and see if it works
  341. @\mkdir($dir, 0700, true);
  342. }
  343. if (!\is_dir($dir) || !\is_writable($dir)) {
  344. \trigger_error(\sprintf('Writing to directory %s is not allowed.', $dir), \E_USER_NOTICE);
  345. return false;
  346. }
  347. return true;
  348. }
  349. /**
  350. * Ensure that $file exists and is writable, make the parent directory if necessary.
  351. *
  352. * Generates E_USER_NOTICE error if either $file or its directory is not writable.
  353. *
  354. * @param string $file
  355. *
  356. * @return string|false Full path to $file, or false if file is not writable
  357. */
  358. public static function touchFileWithMkdir(string $file)
  359. {
  360. if (\file_exists($file)) {
  361. if (\is_writable($file)) {
  362. return $file;
  363. }
  364. \trigger_error(\sprintf('Writing to %s is not allowed.', $file), \E_USER_NOTICE);
  365. return false;
  366. }
  367. if (!self::ensureDir(\dirname($file))) {
  368. return false;
  369. }
  370. \touch($file);
  371. return $file;
  372. }
  373. private function getEnv($key)
  374. {
  375. return $this->env->get($key);
  376. }
  377. private function getEnvArray($key)
  378. {
  379. if ($value = $this->getEnv($key)) {
  380. return \explode(\PATH_SEPARATOR, $value);
  381. }
  382. return null;
  383. }
  384. }