PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/psy/psysh/src/ConfigPaths.php

https://bitbucket.org/irahmat/nctauth
PHP | 237 lines | 131 code | 22 blank | 84 comment | 2 complexity | 7b7babc3d6b40de03de4ed374b1d2464 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 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. use XdgBaseDir\Xdg;
  12. /**
  13. * A Psy Shell configuration path helper.
  14. */
  15. class ConfigPaths
  16. {
  17. /**
  18. * Get potential config directory paths.
  19. *
  20. * Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and all
  21. * XDG Base Directory config directories:
  22. *
  23. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  24. *
  25. * @return string[]
  26. */
  27. public static function getConfigDirs()
  28. {
  29. $xdg = new Xdg();
  30. return self::getDirNames($xdg->getConfigDirs());
  31. }
  32. /**
  33. * Get potential home config directory paths.
  34. *
  35. * Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and the
  36. * XDG Base Directory home config directory:
  37. *
  38. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  39. *
  40. * @return string[]
  41. */
  42. public static function getHomeConfigDirs()
  43. {
  44. $xdg = new Xdg();
  45. return self::getDirNames([$xdg->getHomeConfigDir()]);
  46. }
  47. /**
  48. * Get the current home config directory.
  49. *
  50. * Returns the highest precedence home config directory which actually
  51. * exists. If none of them exists, returns the highest precedence home
  52. * config directory (`%APPDATA%/PsySH` on Windows, `~/.config/psysh`
  53. * everywhere else).
  54. *
  55. * @see self::getHomeConfigDirs
  56. *
  57. * @return string
  58. */
  59. public static function getCurrentConfigDir()
  60. {
  61. $configDirs = self::getHomeConfigDirs();
  62. foreach ($configDirs as $configDir) {
  63. if (@is_dir($configDir)) {
  64. return $configDir;
  65. }
  66. }
  67. return $configDirs[0];
  68. }
  69. /**
  70. * Find real config files in config directories.
  71. *
  72. * @param string[] $names Config file names
  73. * @param string $configDir Optionally use a specific config directory
  74. *
  75. * @return string[]
  76. */
  77. public static function getConfigFiles(array $names, $configDir = null)
  78. {
  79. $dirs = ($configDir === null) ? self::getConfigDirs() : [$configDir];
  80. return self::getRealFiles($dirs, $names);
  81. }
  82. /**
  83. * Get potential data directory paths.
  84. *
  85. * If a `dataDir` option was explicitly set, returns an array containing
  86. * just that directory.
  87. *
  88. * Otherwise, it returns `~/.psysh` and all XDG Base Directory data directories:
  89. *
  90. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  91. *
  92. * @return string[]
  93. */
  94. public static function getDataDirs()
  95. {
  96. $xdg = new Xdg();
  97. return self::getDirNames($xdg->getDataDirs());
  98. }
  99. /**
  100. * Find real data files in config directories.
  101. *
  102. * @param string[] $names Config file names
  103. * @param string $dataDir Optionally use a specific config directory
  104. *
  105. * @return string[]
  106. */
  107. public static function getDataFiles(array $names, $dataDir = null)
  108. {
  109. $dirs = ($dataDir === null) ? self::getDataDirs() : [$dataDir];
  110. return self::getRealFiles($dirs, $names);
  111. }
  112. /**
  113. * Get a runtime directory.
  114. *
  115. * Defaults to `/psysh` inside the system's temp dir.
  116. *
  117. * @return string
  118. */
  119. public static function getRuntimeDir()
  120. {
  121. $xdg = new Xdg();
  122. set_error_handler(['Psy\Exception\ErrorException', 'throwException']);
  123. try {
  124. // XDG doesn't really work on Windows, sometimes complains about
  125. // permissions, sometimes tries to remove non-empty directories.
  126. // It's a bit flaky. So we'll give this a shot first...
  127. $runtimeDir = $xdg->getRuntimeDir(false);
  128. } catch (\Exception $e) {
  129. // Well. That didn't work. Fall back to a boring old folder in the
  130. // system temp dir.
  131. $runtimeDir = sys_get_temp_dir();
  132. }
  133. restore_error_handler();
  134. return strtr($runtimeDir, '\\', '/') . '/psysh';
  135. }
  136. private static function getDirNames(array $baseDirs)
  137. {
  138. $dirs = array_map(function ($dir) {
  139. return strtr($dir, '\\', '/') . '/psysh';
  140. }, $baseDirs);
  141. // Add ~/.psysh
  142. if ($home = getenv('HOME')) {
  143. $dirs[] = strtr($home, '\\', '/') . '/.psysh';
  144. }
  145. // Add some Windows specific ones :)
  146. if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
  147. if ($appData = getenv('APPDATA')) {
  148. // AppData gets preference
  149. array_unshift($dirs, strtr($appData, '\\', '/') . '/PsySH');
  150. }
  151. $dir = strtr(getenv('HOMEDRIVE') . '/' . getenv('HOMEPATH'), '\\', '/') . '/.psysh';
  152. if (!in_array($dir, $dirs)) {
  153. $dirs[] = $dir;
  154. }
  155. }
  156. return $dirs;
  157. }
  158. private static function getRealFiles(array $dirNames, array $fileNames)
  159. {
  160. $files = [];
  161. foreach ($dirNames as $dir) {
  162. foreach ($fileNames as $name) {
  163. $file = $dir . '/' . $name;
  164. if (@is_file($file)) {
  165. $files[] = $file;
  166. }
  167. }
  168. }
  169. return $files;
  170. }
  171. /**
  172. * Ensure that $file exists and is writable, make the parent directory if necessary.
  173. *
  174. * Generates E_USER_NOTICE error if either $file or its directory is not writable.
  175. *
  176. * @param string $file
  177. *
  178. * @return string|false Full path to $file, or false if file is not writable
  179. */
  180. public static function touchFileWithMkdir($file)
  181. {
  182. if (file_exists($file)) {
  183. if (is_writable($file)) {
  184. return $file;
  185. }
  186. trigger_error(sprintf('Writing to %s is not allowed.', $file), E_USER_NOTICE);
  187. return false;
  188. }
  189. $dir = dirname($file);
  190. if (!is_dir($dir)) {
  191. // Just try making it and see if it works
  192. @mkdir($dir, 0700, true);
  193. }
  194. if (!is_dir($dir) || !is_writable($dir)) {
  195. trigger_error(sprintf('Writing to %s is not allowed.', $dir), E_USER_NOTICE);
  196. return false;
  197. }
  198. touch($file);
  199. return $file;
  200. }
  201. }