Xdg.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace XdgBaseDir;
  3. /**
  4. * Simple implementation of the XDG standard http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  5. *
  6. * Based on the python implementation https://github.com/takluyver/pyxdg/blob/master/xdg/BaseDirectory.py
  7. *
  8. * Class Xdg
  9. * @package ShopwareCli\Application
  10. */
  11. class Xdg
  12. {
  13. const S_IFDIR = 040000; // directory
  14. const S_IRWXO = 00007; // rwx other
  15. const S_IRWXG = 00056; // rwx group
  16. const RUNTIME_DIR_FALLBACK = 'php-xdg-runtime-dir-fallback-';
  17. /**
  18. * @return string
  19. */
  20. public function getHomeDir()
  21. {
  22. return getenv('HOME') ?: (getenv('HOMEDRIVE') . DIRECTORY_SEPARATOR . getenv('HOMEPATH'));
  23. }
  24. /**
  25. * @return string
  26. */
  27. public function getHomeConfigDir()
  28. {
  29. $path = getenv('XDG_CONFIG_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.config';
  30. return $path;
  31. }
  32. /**
  33. * @return string
  34. */
  35. public function getHomeDataDir()
  36. {
  37. $path = getenv('XDG_DATA_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.local' . DIRECTORY_SEPARATOR . 'share';
  38. return $path;
  39. }
  40. /**
  41. * @return array
  42. */
  43. public function getConfigDirs()
  44. {
  45. $configDirs = getenv('XDG_CONFIG_DIRS') ? explode(':', getenv('XDG_CONFIG_DIRS')) : array('/etc/xdg');
  46. $paths = array_merge(array($this->getHomeConfigDir()), $configDirs);
  47. return $paths;
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function getDataDirs()
  53. {
  54. $dataDirs = getenv('XDG_DATA_DIRS') ? explode(':', getenv('XDG_DATA_DIRS')) : array('/usr/local/share', '/usr/share');
  55. $paths = array_merge(array($this->getHomeDataDir()), $dataDirs);
  56. return $paths;
  57. }
  58. /**
  59. * @return string
  60. */
  61. public function getHomeCacheDir()
  62. {
  63. $path = getenv('XDG_CACHE_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.cache';
  64. return $path;
  65. }
  66. public function getRuntimeDir($strict=true)
  67. {
  68. if ($runtimeDir = getenv('XDG_RUNTIME_DIR')) {
  69. return $runtimeDir;
  70. }
  71. if ($strict) {
  72. throw new \RuntimeException('XDG_RUNTIME_DIR was not set');
  73. }
  74. $fallback = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::RUNTIME_DIR_FALLBACK . getenv('USER');
  75. $create = false;
  76. if (!is_dir($fallback)) {
  77. mkdir($fallback, 0700, true);
  78. }
  79. $st = lstat($fallback);
  80. # The fallback must be a directory
  81. if (!$st['mode'] & self::S_IFDIR) {
  82. rmdir($fallback);
  83. $create = true;
  84. } elseif ($st['uid'] != getmyuid() ||
  85. $st['mode'] & (self::S_IRWXG | self::S_IRWXO)
  86. ) {
  87. rmdir($fallback);
  88. $create = true;
  89. }
  90. if ($create) {
  91. mkdir($fallback, 0700, true);
  92. }
  93. return $fallback;
  94. }
  95. }