Completed
Push — master ( 5017c0...9386a2 )
by Samuel
11:29
created

Config::getUserHomeDir()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 8
cp 0
rs 9.3222
c 0
b 0
f 0
cc 5
nc 6
nop 0
crap 30
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
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 CacheTool\Console;
13
14
use Symfony\Component\Yaml\Parser;
15
16
class Config implements \ArrayAccess
17
{
18
    private $config = [
19
        'adapter' => 'fastcgi',
20
        'extensions' => ['apcu', 'opcache'],
21
        'fastcgi' => null,
22
        'temp_dir' => null
23
    ];
24
25 22
    public function __construct(array $config = [])
26
    {
27 22
        if (!empty($config)) {
28 17
            $this->config = array_replace($this->config, $config);
29
30 17
            if (!isset($this->config['temp_dir'])) {
31 16
                $this->config['temp_dir'] = sys_get_temp_dir();
32
            }
33
        }
34 22
    }
35
36 22
    public function offsetExists($offset)
37
    {
38 22
        return isset($this->config[$offset]);
39
    }
40
41 21
    public function offsetGet($offset)
42
    {
43 21
        if ($this->offsetExists($offset)) {
44 21
            return $this->config[$offset];
45
        }
46 5
    }
47
48 4
    public function offsetSet($offset, $value)
49
    {
50 4
        $this->config[$offset] = $value;
51 4
    }
52
53 1
    public function offsetUnset($offset)
54
    {
55 1
        unset($this->config[$offset]);
56 1
    }
57
58
    public static function factory()
59
    {
60
        $previous = null;
61
        $path = getcwd();
62
        $paths = [];
63
        $yaml = new Parser();
64
65
        while (($path = realpath($path)) && $path !== $previous) {
66
            $paths[] = "{$path}/.cachetool.yml";
67
            $paths[] = "{$path}/.cachetool.yaml";
68
            $previous = $path;
69
            $path .= '/../';
70
        }
71
72
        if ($home = static::getUserHomeDir()) {
73
            $paths[] = "{$home}/.cachetool.yml";
74
            $paths[] = "{$home}/.cachetool.yaml";
75
        }
76
77
        $paths[] = '/etc/cachetool.yml';
78
        $paths[] = '/etc/cachetool.yaml';
79
80
        foreach ($paths as $path) {
81
            if (is_file($path)) {
82
                $config = $yaml->parse(file_get_contents($path));
83
                return new Config($config);
84
            }
85
        }
86
87
        return new Config();
88
    }
89
90
    /**
91
     * Return the user's home directory.
92
     * From drush
93
     *
94
     * @return string
95
     */
96
    protected static function getUserHomeDir()
97
    {
98
        // Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase
99
        // getenv('HOME') isn't set on Windows and generates a Notice.
100
        $home = getenv('HOME');
101
102
        if (!empty($home)) {
103
            // home should never end with a trailing slash.
104
            $home = rtrim($home, '/');
105
        } elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
106
            // home on windows
107
            $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
108
            // If HOMEPATH is a root directory the path can end with a slash. Make sure
109
            // that doesn't happen.
110
            $home = rtrim($home, '\\/');
111
        }
112
113
        return empty($home) ? null : $home;
114
    }
115
}
116