4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
17 * A Psy Shell configuration path helper.
22 * Get potential config directory paths.
24 * Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and all
25 * XDG Base Directory config directories:
27 * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
31 public static function getConfigDirs()
35 return self::getDirNames($xdg->getConfigDirs());
39 * Get potential home config directory paths.
41 * Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and the
42 * XDG Base Directory home config directory:
44 * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
48 public static function getHomeConfigDirs()
52 return self::getDirNames(array($xdg->getHomeConfigDir()));
56 * Get the current home config directory.
58 * Returns the highest precedence home config directory which actually
59 * exists. If none of them exists, returns the highest precedence home
60 * config directory (`%APPDATA%/PsySH` on Windows, `~/.config/psysh`
63 * @see self::getHomeConfigDirs
67 public static function getCurrentConfigDir()
69 $configDirs = self::getHomeConfigDirs();
70 foreach ($configDirs as $configDir) {
71 if (@is_dir($configDir)) {
76 return $configDirs[0];
80 * Find real config files in config directories.
82 * @param string[] $names Config file names
83 * @param string $configDir Optionally use a specific config directory
87 public static function getConfigFiles(array $names, $configDir = null)
89 $dirs = ($configDir === null) ? self::getConfigDirs() : array($configDir);
91 return self::getRealFiles($dirs, $names);
95 * Get potential data directory paths.
97 * If a `dataDir` option was explicitly set, returns an array containing
98 * just that directory.
100 * Otherwise, it returns `~/.psysh` and all XDG Base Directory data directories:
102 * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
106 public static function getDataDirs()
110 return self::getDirNames($xdg->getDataDirs());
114 * Find real data files in config directories.
116 * @param string[] $names Config file names
117 * @param string $dataDir Optionally use a specific config directory
121 public static function getDataFiles(array $names, $dataDir = null)
123 $dirs = ($dataDir === null) ? self::getDataDirs() : array($dataDir);
125 return self::getRealFiles($dirs, $names);
129 * Get a runtime directory.
131 * Defaults to `/psysh` inside the system's temp dir.
135 public static function getRuntimeDir()
139 return $xdg->getRuntimeDir(false) . '/psysh';
142 private static function getDirNames(array $baseDirs)
144 $dirs = array_map(function ($dir) {
145 return strtr($dir, '\\', '/') . '/psysh';
149 if ($home = getenv('HOME')) {
150 $dirs[] = strtr($home, '\\', '/') . '/.psysh';
153 // Add some Windows specific ones :)
154 if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
155 if ($appData = getenv('APPDATA')) {
156 // AppData gets preference
157 array_unshift($dirs, strtr($appData, '\\', '/') . '/PsySH');
160 $dir = strtr(getenv('HOMEDRIVE') . '/' . getenv('HOMEPATH'), '\\', '/') . '/.psysh';
161 if (!in_array($dir, $dirs)) {
169 private static function getRealFiles(array $dirNames, array $fileNames)
172 foreach ($dirNames as $dir) {
173 foreach ($fileNames as $name) {
174 $file = $dir . '/' . $name;
175 if (@is_file($file)) {
185 * Ensure that $file exists and is writable, make the parent directory if necessary.
187 * Generates E_USER_NOTICE error if either $file or its directory is not writable.
189 * @param string $file
191 * @return string|false Full path to $file, or false if file is not writable
193 public static function touchFileWithMkdir($file)
195 if (file_exists($file)) {
196 if (is_writable($file)) {
200 trigger_error(sprintf('Writing to %s is not allowed.', $file), E_USER_NOTICE);
205 $dir = dirname($file);
208 // Just try making it and see if it works
209 @mkdir($dir, 0700, true);
212 if (!is_dir($dir) || !is_writable($dir)) {
213 trigger_error(sprintf('Writing to %s is not allowed.', $dir), E_USER_NOTICE);