Skip to content

Commit 7d28e75

Browse files
Use XDG in the same way that Composer does.
1 parent 3a857cc commit 7d28e75

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Option | Environment Variable | Description
4242
--base-dir | CGR_BASE_DIR | Where to store "global" projects.
4343
--bin-dir | CGR_BIN_DIR | Where to install project binaries.
4444

45+
If these variables are not defined, then cgr uses the value of the `COMPOSER_HOME` environment variable as the base directory to use as described in the [Composer documentation on environment variables](https://getcomposer.org/doc/03-cli.md#composer-home).
46+
4547
To configure cgr to install binaries to ~/bin, add the following to your ~/.bashrc file:
4648

4749
`export CGR_BIN_DIR=$HOME/bin`

bin/cgr.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
function main($argv)
44
{
5-
$home = getenv("COMPOSER_HOME");
6-
if (empty($home)) {
7-
$home = getenv("HOME") . '/.composer';
8-
}
95
$appRoot = dirname(__DIR__);
106

117
if (file_exists($appRoot.'/vendor/autoload.php')) {
@@ -17,6 +13,9 @@ function main($argv)
1713
exit(1);
1814
}
1915

16+
// Find the home directory
17+
$home = \Consolidation\Cgr\SystemInformation::getHomeDir();
18+
2019
$app = new \Consolidation\Cgr\Application();
2120
return $app->run($argv, $home);
2221
}

src/SystemInformation.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Consolidation\Cgr;
4+
5+
/**
6+
* Determine information about the system
7+
*/
8+
class SystemInformation
9+
{
10+
/**
11+
* @throws \RuntimeException
12+
* @return string
13+
*/
14+
public static function getHomeDir()
15+
{
16+
$home = getenv('COMPOSER_HOME');
17+
if ($home) {
18+
return $home;
19+
}
20+
if (self::isWindows()) {
21+
if (!getenv('APPDATA')) {
22+
throw new \RuntimeException('The APPDATA or COMPOSER_HOME environment variable must be set for composer to run correctly');
23+
}
24+
return rtrim(strtr(getenv('APPDATA'), '\\', '/'), '/') . '/Composer';
25+
}
26+
$userDir = self::getUserDir();
27+
if (is_dir($userDir . '/.composer')) {
28+
return $userDir . '/.composer';
29+
}
30+
if (self::useXdg()) {
31+
// XDG Base Directory Specifications
32+
$xdgConfig = getenv('XDG_CONFIG_HOME') ?: $userDir . '/.config';
33+
return $xdgConfig . '/composer';
34+
}
35+
return $userDir . '/.composer';
36+
}
37+
38+
/**
39+
* @return bool Whether the host machine is running a Windows OS
40+
*/
41+
public static function isWindows()
42+
{
43+
return defined('PHP_WINDOWS_VERSION_BUILD');
44+
}
45+
46+
/**
47+
* @return bool
48+
*/
49+
private static function useXdg()
50+
{
51+
foreach (array_keys($_SERVER) as $key) {
52+
if (substr($key, 0, 4) === 'XDG_') {
53+
return true;
54+
}
55+
}
56+
return false;
57+
}
58+
59+
/**
60+
* @throws \RuntimeException
61+
* @return string
62+
*/
63+
private static function getUserDir()
64+
{
65+
$home = getenv('HOME');
66+
if (!$home) {
67+
throw new \RuntimeException('The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly');
68+
}
69+
return rtrim(strtr($home, '\\', '/'), '/');
70+
}
71+
}

0 commit comments

Comments
 (0)