PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/nmhieucoder/laravel_tintuc
PHP | 413 lines | 324 code | 27 blank | 62 comment | 16 complexity | d1f12f1b81c42474c5259dad7a776228 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2020 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($overrides = [], $env = null)
  31. {
  32. $this->overrideDirs($overrides);
  33. $this->env = $env ?: 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($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()
  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()
  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()
  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. * @return string
  139. */
  140. public function currentConfigDir()
  141. {
  142. if ($this->configDir !== null) {
  143. return $this->configDir;
  144. }
  145. $configDirs = $this->allDirNames([$this->homeConfigDir()]);
  146. foreach ($configDirs as $configDir) {
  147. if (@\is_dir($configDir)) {
  148. return $configDir;
  149. }
  150. }
  151. return $configDirs[0];
  152. }
  153. /**
  154. * @deprecated
  155. */
  156. public static function getCurrentConfigDir()
  157. {
  158. return (new self())->currentConfigDir();
  159. }
  160. /**
  161. * Find real config files in config directories.
  162. *
  163. * @param string[] $names Config file names
  164. *
  165. * @return string[]
  166. */
  167. public function configFiles(array $names)
  168. {
  169. return $this->allRealFiles($this->configDirs(), $names);
  170. }
  171. /**
  172. * @deprecated
  173. */
  174. public static function getConfigFiles(array $names, $configDir = null)
  175. {
  176. return (new self(['configDir' => $configDir]))->configFiles($names);
  177. }
  178. /**
  179. * Get potential data directory paths.
  180. *
  181. * If a `dataDir` option was explicitly set, returns an array containing
  182. * just that directory.
  183. *
  184. * Otherwise, it returns `~/.psysh` and all XDG Base Directory data directories:
  185. *
  186. * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  187. *
  188. * @return string[]
  189. */
  190. public function dataDirs()
  191. {
  192. if ($this->dataDir !== null) {
  193. return [$this->dataDir];
  194. }
  195. $homeDataDir = $this->getEnv('XDG_DATA_HOME') ?: $this->homeDir().'/.local/share';
  196. $dataDirs = $this->getEnvArray('XDG_DATA_DIRS') ?: ['/usr/local/share', '/usr/share'];
  197. return $this->allDirNames(\array_merge([$homeDataDir], $dataDirs));
  198. }
  199. /**
  200. * @deprecated
  201. */
  202. public static function getDataDirs()
  203. {
  204. return (new self())->dataDirs();
  205. }
  206. /**
  207. * Find real data files in config directories.
  208. *
  209. * @param string[] $names Config file names
  210. *
  211. * @return string[]
  212. */
  213. public function dataFiles(array $names)
  214. {
  215. return $this->allRealFiles($this->dataDirs(), $names);
  216. }
  217. /**
  218. * @deprecated
  219. */
  220. public static function getDataFiles(array $names, $dataDir = null)
  221. {
  222. return (new self(['dataDir' => $dataDir]))->dataFiles($names);
  223. }
  224. /**
  225. * Get a runtime directory.
  226. *
  227. * Defaults to `/psysh` inside the system's temp dir.
  228. *
  229. * @return string
  230. */
  231. public function runtimeDir()
  232. {
  233. if ($this->runtimeDir !== null) {
  234. return $this->runtimeDir;
  235. }
  236. // Fallback to a boring old folder in the system temp dir.
  237. $runtimeDir = $this->getEnv('XDG_RUNTIME_DIR') ?: \sys_get_temp_dir();
  238. return \strtr($runtimeDir, '\\', '/').'/psysh';
  239. }
  240. /**
  241. * @deprecated
  242. */
  243. public static function getRuntimeDir()
  244. {
  245. return (new self())->runtimeDir();
  246. }
  247. /**
  248. * Get all PsySH directory name candidates given a list of base directories.
  249. *
  250. * This expects that XDG-compatible directory paths will be passed in.
  251. * `psysh` will be added to each of $baseDirs, and we'll throw in `~/.psysh`
  252. * and a couple of Windows-friendly paths as well.
  253. *
  254. * @param string[] $baseDirs base directory paths
  255. *
  256. * @return string[]
  257. */
  258. private function allDirNames(array $baseDirs)
  259. {
  260. $dirs = \array_map(function ($dir) {
  261. return \strtr($dir, '\\', '/').'/psysh';
  262. }, $baseDirs);
  263. // Add ~/.psysh
  264. if ($home = $this->getEnv('HOME')) {
  265. $dirs[] = \strtr($home, '\\', '/').'/.psysh';
  266. }
  267. // Add some Windows specific ones :)
  268. if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
  269. if ($appData = $this->getEnv('APPDATA')) {
  270. // AppData gets preference
  271. \array_unshift($dirs, \strtr($appData, '\\', '/').'/PsySH');
  272. }
  273. if ($windowsHomeDir = $this->windowsHomeDir()) {
  274. $dir = \strtr($windowsHomeDir, '\\', '/').'/.psysh';
  275. if (!\in_array($dir, $dirs)) {
  276. $dirs[] = $dir;
  277. }
  278. }
  279. }
  280. return $dirs;
  281. }
  282. /**
  283. * Given a list of directories, and a list of filenames, find the ones that
  284. * are real files.
  285. *
  286. * @return string[]
  287. */
  288. private function allRealFiles(array $dirNames, array $fileNames)
  289. {
  290. $files = [];
  291. foreach ($dirNames as $dir) {
  292. foreach ($fileNames as $name) {
  293. $file = $dir.'/'.$name;
  294. if (@\is_file($file)) {
  295. $files[] = $file;
  296. }
  297. }
  298. }
  299. return $files;
  300. }
  301. /**
  302. * Ensure that $dir exists and is writable.
  303. *
  304. * Generates E_USER_NOTICE error if the directory is not writable or creatable.
  305. *
  306. * @param string $dir
  307. *
  308. * @return bool False if directory exists but is not writeable, or cannot be created
  309. */
  310. public static function ensureDir($dir)
  311. {
  312. if (!\is_dir($dir)) {
  313. // Just try making it and see if it works
  314. @\mkdir($dir, 0700, true);
  315. }
  316. if (!\is_dir($dir) || !\is_writable($dir)) {
  317. \trigger_error(\sprintf('Writing to directory %s is not allowed.', $dir), \E_USER_NOTICE);
  318. return false;
  319. }
  320. return true;
  321. }
  322. /**
  323. * Ensure that $file exists and is writable, make the parent directory if necessary.
  324. *
  325. * Generates E_USER_NOTICE error if either $file or its directory is not writable.
  326. *
  327. * @param string $file
  328. *
  329. * @return string|false Full path to $file, or false if file is not writable
  330. */
  331. public static function touchFileWithMkdir($file)
  332. {
  333. if (\file_exists($file)) {
  334. if (\is_writable($file)) {
  335. return $file;
  336. }
  337. \trigger_error(\sprintf('Writing to %s is not allowed.', $file), \E_USER_NOTICE);
  338. return false;
  339. }
  340. if (!self::ensureDir(\dirname($file))) {
  341. return false;
  342. }
  343. \touch($file);
  344. return $file;
  345. }
  346. private function getEnv($key)
  347. {
  348. return $this->env->get($key);
  349. }
  350. private function getEnvArray($key)
  351. {
  352. if ($value = $this->getEnv($key)) {
  353. return \explode(':', $value);
  354. }
  355. return null;
  356. }
  357. }