ConfigPaths.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2015 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(array($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() : array($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() : array($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. return $xdg->getRuntimeDir(false) . '/psysh';
  123. }
  124. private static function getDirNames(array $baseDirs)
  125. {
  126. $dirs = array_map(function ($dir) {
  127. return strtr($dir, '\\', '/') . '/psysh';
  128. }, $baseDirs);
  129. // Add ~/.psysh
  130. if ($home = getenv('HOME')) {
  131. $dirs[] = strtr($home, '\\', '/') . '/.psysh';
  132. }
  133. // Add some Windows specific ones :)
  134. if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
  135. if ($appData = getenv('APPDATA')) {
  136. // AppData gets preference
  137. array_unshift($dirs, strtr($appData, '\\', '/') . '/PsySH');
  138. }
  139. $dir = strtr(getenv('HOMEDRIVE') . '/' . getenv('HOMEPATH'), '\\', '/') . '/.psysh';
  140. if (!in_array($dir, $dirs)) {
  141. $dirs[] = $dir;
  142. }
  143. }
  144. return $dirs;
  145. }
  146. private static function getRealFiles(array $dirNames, array $fileNames)
  147. {
  148. $files = array();
  149. foreach ($dirNames as $dir) {
  150. foreach ($fileNames as $name) {
  151. $file = $dir . '/' . $name;
  152. if (@is_file($file)) {
  153. $files[] = $file;
  154. }
  155. }
  156. }
  157. return $files;
  158. }
  159. }