6 * Simple implementation of the XDG standard http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
8 * Based on the python implementation https://github.com/takluyver/pyxdg/blob/master/xdg/BaseDirectory.py
11 * @package ShopwareCli\Application
15 const S_IFDIR = 040000; // directory
16 const S_IRWXO = 00007; // rwx other
17 const S_IRWXG = 00056; // rwx group
18 const RUNTIME_DIR_FALLBACK = 'php-xdg-runtime-dir-fallback-';
23 public function getHomeDir()
25 return getenv('HOME') ?: (getenv('HOMEDRIVE') . DIRECTORY_SEPARATOR . getenv('HOMEPATH'));
31 public function getHomeConfigDir()
33 $path = getenv('XDG_CONFIG_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.config';
41 public function getHomeDataDir()
43 $path = getenv('XDG_DATA_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.local' . DIRECTORY_SEPARATOR . 'share';
51 public function getConfigDirs()
53 $configDirs = getenv('XDG_CONFIG_DIRS') ? explode(':', getenv('XDG_CONFIG_DIRS')) : array('/etc/xdg');
55 $paths = array_merge(array($this->getHomeConfigDir()), $configDirs);
63 public function getDataDirs()
65 $dataDirs = getenv('XDG_DATA_DIRS') ? explode(':', getenv('XDG_DATA_DIRS')) : array('/usr/local/share', '/usr/share');
67 $paths = array_merge(array($this->getHomeDataDir()), $dataDirs);
75 public function getHomeCacheDir()
77 $path = getenv('XDG_CACHE_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.cache';
83 public function getRuntimeDir($strict=true)
85 if ($runtimeDir = getenv('XDG_RUNTIME_DIR')) {
90 throw new \RuntimeException('XDG_RUNTIME_DIR was not set');
93 $fallback = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::RUNTIME_DIR_FALLBACK . getenv('USER');
97 if (!is_dir($fallback)) {
98 mkdir($fallback, 0700, true);
101 $st = lstat($fallback);
103 # The fallback must be a directory
104 if (!$st['mode'] & self::S_IFDIR) {
107 } elseif ($st['uid'] != getmyuid() ||
108 $st['mode'] & (self::S_IRWXG | self::S_IRWXO)
115 mkdir($fallback, 0700, true);