Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / ConfigPaths.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy;
13
14 use XdgBaseDir\Xdg;
15
16 /**
17  * A Psy Shell configuration path helper.
18  */
19 class ConfigPaths
20 {
21     /**
22      * Get potential config directory paths.
23      *
24      * Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and all
25      * XDG Base Directory config directories:
26      *
27      *     http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
28      *
29      * @return string[]
30      */
31     public static function getConfigDirs()
32     {
33         $xdg = new Xdg();
34
35         return self::getDirNames($xdg->getConfigDirs());
36     }
37
38     /**
39      * Get potential home config directory paths.
40      *
41      * Returns `~/.psysh`, `%APPDATA%/PsySH` (when on Windows), and the
42      * XDG Base Directory home config directory:
43      *
44      *     http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
45      *
46      * @return string[]
47      */
48     public static function getHomeConfigDirs()
49     {
50         $xdg = new Xdg();
51
52         return self::getDirNames(array($xdg->getHomeConfigDir()));
53     }
54
55     /**
56      * Get the current home config directory.
57      *
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`
61      * everywhere else).
62      *
63      * @see self::getHomeConfigDirs
64      *
65      * @return string
66      */
67     public static function getCurrentConfigDir()
68     {
69         $configDirs = self::getHomeConfigDirs();
70         foreach ($configDirs as $configDir) {
71             if (@is_dir($configDir)) {
72                 return $configDir;
73             }
74         }
75
76         return $configDirs[0];
77     }
78
79     /**
80      * Find real config files in config directories.
81      *
82      * @param string[] $names     Config file names
83      * @param string   $configDir Optionally use a specific config directory
84      *
85      * @return string[]
86      */
87     public static function getConfigFiles(array $names, $configDir = null)
88     {
89         $dirs = ($configDir === null) ? self::getConfigDirs() : array($configDir);
90
91         return self::getRealFiles($dirs, $names);
92     }
93
94     /**
95      * Get potential data directory paths.
96      *
97      * If a `dataDir` option was explicitly set, returns an array containing
98      * just that directory.
99      *
100      * Otherwise, it returns `~/.psysh` and all XDG Base Directory data directories:
101      *
102      *     http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
103      *
104      * @return string[]
105      */
106     public static function getDataDirs()
107     {
108         $xdg = new Xdg();
109
110         return self::getDirNames($xdg->getDataDirs());
111     }
112
113     /**
114      * Find real data files in config directories.
115      *
116      * @param string[] $names   Config file names
117      * @param string   $dataDir Optionally use a specific config directory
118      *
119      * @return string[]
120      */
121     public static function getDataFiles(array $names, $dataDir = null)
122     {
123         $dirs = ($dataDir === null) ? self::getDataDirs() : array($dataDir);
124
125         return self::getRealFiles($dirs, $names);
126     }
127
128     /**
129      * Get a runtime directory.
130      *
131      * Defaults to  `/psysh` inside the system's temp dir.
132      *
133      * @return string
134      */
135     public static function getRuntimeDir()
136     {
137         $xdg = new Xdg();
138
139         return $xdg->getRuntimeDir(false) . '/psysh';
140     }
141
142     private static function getDirNames(array $baseDirs)
143     {
144         $dirs = array_map(function ($dir) {
145             return strtr($dir, '\\', '/') . '/psysh';
146         }, $baseDirs);
147
148         // Add ~/.psysh
149         if ($home = getenv('HOME')) {
150             $dirs[] = strtr($home, '\\', '/') . '/.psysh';
151         }
152
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');
158             }
159
160             $dir = strtr(getenv('HOMEDRIVE') . '/' . getenv('HOMEPATH'), '\\', '/') . '/.psysh';
161             if (!in_array($dir, $dirs)) {
162                 $dirs[] = $dir;
163             }
164         }
165
166         return $dirs;
167     }
168
169     private static function getRealFiles(array $dirNames, array $fileNames)
170     {
171         $files = array();
172         foreach ($dirNames as $dir) {
173             foreach ($fileNames as $name) {
174                 $file = $dir . '/' . $name;
175                 if (@is_file($file)) {
176                     $files[] = $file;
177                 }
178             }
179         }
180
181         return $files;
182     }
183
184     /**
185      * Ensure that $file exists and is writable, make the parent directory if necessary.
186      *
187      * Generates E_USER_NOTICE error if either $file or its directory is not writable.
188      *
189      * @param string $file
190      *
191      * @return string|false Full path to $file, or false if file is not writable
192      */
193     public static function touchFileWithMkdir($file)
194     {
195         if (file_exists($file)) {
196             if (is_writable($file)) {
197                 return $file;
198             }
199
200             trigger_error(sprintf('Writing to %s is not allowed.', $file), E_USER_NOTICE);
201
202             return false;
203         }
204
205         $dir = dirname($file);
206
207         if (!is_dir($dir)) {
208             // Just try making it and see if it works
209             @mkdir($dir, 0700, true);
210         }
211
212         if (!is_dir($dir) || !is_writable($dir)) {
213             trigger_error(sprintf('Writing to %s is not allowed.', $dir), E_USER_NOTICE);
214
215             return false;
216         }
217
218         touch($file);
219
220         return $file;
221     }
222 }