Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getenv access using constructor #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions src/Xdg.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,51 @@ class Xdg
const S_IRWXG = 00056; // rwx group
const RUNTIME_DIR_FALLBACK = 'php-xdg-runtime-dir-fallback-';

/**
* @var array
*/
private static $envKeys = array(
'USER',
'HOME',
'HOMEDRIVE',
'HOMEPATH',
'XDG_CONFIG_HOME',
'XDG_DATA_HOME',
'XDG_CACHE_HOME',
'XDG_CONFIG_DIRS',
'XDG_DATA_DIRS',
'XDG_RUNTIME_DIR',
);

/**
* @var array
*/
private $env = array();

/**
* @param array $env
*/
public function __construct(array $env = array())
{
foreach (self::$envKeys as $key) {
$this->env[$key] = array_key_exists($key, $env) ? (string)$env[$key] : getenv($key);
}
}

/**
* @return string
*/
public function getHomeDir()
{
return getenv('HOME') ?: (getenv('HOMEDRIVE') . DIRECTORY_SEPARATOR . getenv('HOMEPATH'));
return $this->getEnv('HOME') ?: ($this->getEnv('HOMEDRIVE') . DIRECTORY_SEPARATOR . $this->getEnv('HOMEPATH'));
}

/**
* @return string
*/
public function getHomeConfigDir()
{
$path = getenv('XDG_CONFIG_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.config';
$path = $this->getEnv('XDG_CONFIG_HOME') ? : $this->getHomeDir() . DIRECTORY_SEPARATOR . '.config';

return $path;
}
Expand All @@ -40,7 +71,7 @@ public function getHomeConfigDir()
*/
public function getHomeDataDir()
{
$path = getenv('XDG_DATA_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.local' . DIRECTORY_SEPARATOR . 'share';
$path = $this->getEnv('XDG_DATA_HOME') ? : $this->getHomeDir() . DIRECTORY_SEPARATOR . '.local' . DIRECTORY_SEPARATOR . 'share';

return $path;
}
Expand All @@ -50,7 +81,7 @@ public function getHomeDataDir()
*/
public function getConfigDirs()
{
$configDirs = getenv('XDG_CONFIG_DIRS') ? explode(':', getenv('XDG_CONFIG_DIRS')) : array('/etc/xdg');
$configDirs = $this->getEnv('XDG_CONFIG_DIRS') ? explode(':', $this->getEnv('XDG_CONFIG_DIRS')) : array('/etc/xdg');

$paths = array_merge(array($this->getHomeConfigDir()), $configDirs);

Expand All @@ -62,7 +93,7 @@ public function getConfigDirs()
*/
public function getDataDirs()
{
$dataDirs = getenv('XDG_DATA_DIRS') ? explode(':', getenv('XDG_DATA_DIRS')) : array('/usr/local/share', '/usr/share');
$dataDirs = $this->getEnv('XDG_DATA_DIRS') ? explode(':', $this->getEnv('XDG_DATA_DIRS')) : array('/usr/local/share', '/usr/share');

$paths = array_merge(array($this->getHomeDataDir()), $dataDirs);

Expand All @@ -74,23 +105,36 @@ public function getDataDirs()
*/
public function getHomeCacheDir()
{
$path = getenv('XDG_CACHE_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.cache';
$path = $this->getEnv('XDG_CACHE_HOME') ? : $this->getHomeDir() . DIRECTORY_SEPARATOR . '.cache';

return $path;
}

/**
* @param $name
* @return bool
*/
private function getEnv($name)
{
return isset($this->env[$name]) ? $this->env[$name] : false;
}

/**
* @param bool $strict
* @return string
* @throws \RuntimeException
*/
public function getRuntimeDir($strict=true)
{
if ($runtimeDir = getenv('XDG_RUNTIME_DIR')) {
if ($runtimeDir = $this->getEnv('XDG_RUNTIME_DIR')) {
return $runtimeDir;
}

if ($strict) {
throw new \RuntimeException('XDG_RUNTIME_DIR was not set');
}

$fallback = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::RUNTIME_DIR_FALLBACK . getenv('USER');
$fallback = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::RUNTIME_DIR_FALLBACK . $this->getEnv('USER');

$create = false;

Expand All @@ -117,5 +161,4 @@ public function getRuntimeDir($strict=true)

return $fallback;
}

}
62 changes: 41 additions & 21 deletions tests/XdgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,60 @@ public function testGetFallbackHomeDir()

public function testXdgPutCache()
{
putenv('XDG_DATA_HOME=tmp/');
putenv('XDG_CONFIG_HOME=tmp/');
putenv('XDG_CACHE_HOME=tmp/');
$this->assertEquals('tmp/', $this->getXdg()->getHomeCacheDir());
$env = array(
'XDG_DATA_HOME' => 'tmp/',
'XDG_CONFIG_HOME' => 'tmp/',
'XDG_CACHE_HOME' => 'tmp/',
);
$xdg = new \XdgBaseDir\Xdg($env);

$this->assertEquals('tmp/', $xdg->getHomeCacheDir());
}

public function testXdgPutData()
{
putenv('XDG_DATA_HOME=tmp/');
$this->assertEquals('tmp/', $this->getXdg()->getHomeDataDir());
$env = array('XDG_DATA_HOME' => 'tmp/');
$xdg = new \XdgBaseDir\Xdg($env);

$this->assertEquals('tmp/', $xdg->getHomeDataDir());
}

public function testXdgPutConfig()
{
putenv('XDG_CONFIG_HOME=tmp/');
$this->assertEquals('tmp/', $this->getXdg()->getHomeConfigDir());
$env = array('XDG_CONFIG_HOME' => 'tmp/');
$xdg = new \XdgBaseDir\Xdg($env);

$this->assertEquals('tmp/', $xdg->getHomeConfigDir());
}

public function testXdgDataDirsShouldIncludeHomeDataDir()
{
putenv('XDG_DATA_HOME=tmp/');
putenv('XDG_CONFIG_HOME=tmp/');
$env = array(
'XDG_DATA_HOME' => 'tmp/',
'XDG_CONFIG_HOME' => 'tmp/'
);
$xdg = new \XdgBaseDir\Xdg($env);

$this->assertArrayHasKey('tmp/', array_flip($this->getXdg()->getDataDirs()));
$this->assertArrayHasKey('tmp/', array_flip($xdg->getDataDirs()));
}

public function testXdgConfigDirsShouldIncludeHomeConfigDir()
{
putenv('XDG_CONFIG_HOME=tmp/');
$env = array('XDG_CONFIG_HOME' => 'tmp/');
$xdg = new \XdgBaseDir\Xdg($env);

$this->assertArrayHasKey('tmp/', array_flip($this->getXdg()->getConfigDirs()));
$this->assertArrayHasKey('tmp/', array_flip($xdg->getConfigDirs()));
}

/**
* If XDG_RUNTIME_DIR is set, it should be returned
*/
public function testGetRuntimeDir()
{
putenv('XDG_RUNTIME_DIR=/tmp/');
$runtimeDir = $this->getXdg()->getRuntimeDir();
$env = array('XDG_RUNTIME_DIR' => '/tmp/');
$xdg = new \XdgBaseDir\Xdg($env);

$runtimeDir =$xdg->getRuntimeDir();
$this->assertEquals(is_dir($runtimeDir), true);
}

Expand All @@ -77,23 +90,29 @@ public function testGetRuntimeDir()
*/
public function testGetRuntimeDirShouldThrowException()
{
putenv('XDG_RUNTIME_DIR=');
$this->getXdg()->getRuntimeDir(true);
$env = array('XDG_RUNTIME_DIR' => '');
$xdg = new \XdgBaseDir\Xdg($env);

$xdg->getRuntimeDir(true);
}

/**
* In fallback mode a directory should be created
* @filesystem
*/
public function testGetRuntimeDirShouldCreateDirectory()
{
putenv('XDG_RUNTIME_DIR=');
$dir = $this->getXdg()->getRuntimeDir(false);
$env = array('XDG_RUNTIME_DIR' => '');
$xdg = new \XdgBaseDir\Xdg($env);

$dir = $xdg->getRuntimeDir(false);
$permission = decoct(fileperms($dir) & 0777);
$this->assertEquals(700, $permission);
}

/**
* Ensure, that the fallback directories are created with correct permission
* @filesystem
*/
public function testGetRuntimeShouldDeleteDirsWithWrongPermission()
{
Expand All @@ -106,8 +125,9 @@ public function testGetRuntimeShouldDeleteDirsWithWrongPermission()
$permission = decoct(fileperms($runtimeDir) & 0777);
$this->assertEquals(764, $permission);

putenv('XDG_RUNTIME_DIR=');
$dir = $this->getXdg()->getRuntimeDir(false);
$env = array('XDG_RUNTIME_DIR' => '');
$xdg = new \XdgBaseDir\Xdg($env);
$dir = $xdg->getRuntimeDir(false);

// Permission should be fixed
$permission = decoct(fileperms($dir) & 0777);
Expand Down