Completed
Push — master ( 09a2d3...1a84d1 )
by Samuel
16:54 queued 10:40
created

Config::fromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 23
    public function __construct(array $config = [])
26
    {
27 23
        if (!empty($config)) {
28 18
            $this->config = array_replace($this->config, $config);
29
30 18
            if (!isset($this->config['temp_dir'])) {
31 17
                $this->config['temp_dir'] = sys_get_temp_dir();
32
            }
33
        }
34 23
    }
35
36 23
    public function offsetExists($offset)
37
    {
38 23
        return isset($this->config[$offset]);
39
    }
40
41 22
    public function offsetGet($offset)
42
    {
43 22
        if ($this->offsetExists($offset)) {
44 22
            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
64
        while (($path = realpath($path)) && $path !== $previous) {
65
            $paths[] = "{$path}/.cachetool.yml";
66
            $paths[] = "{$path}/.cachetool.yaml";
67
            $previous = $path;
68
            $path .= '/../';
69
        }
70
71
        if ($home = static::getUserHomeDir()) {
72
            $paths[] = "{$home}/.cachetool.yml";
73
            $paths[] = "{$home}/.cachetool.yaml";
74
        }
75
76
        $paths[] = '/etc/cachetool.yml';
77
        $paths[] = '/etc/cachetool.yaml';
78
79
        foreach ($paths as $path) {
80
            if (is_file($path)) {
81
                static::fromFile($path);
82
            }
83
        }
84
85
        return new Config();
86
    }
87
88 1
    public static function fromFile($path) {
89 1
        $yaml = new Parser();
90 1
        $config = $yaml->parse(file_get_contents($path));
91 1
        return new Config($config);
92
    }
93
94
    /**
95
     * Return the user's home directory.
96
     * From drush
97
     *
98
     * @return string
99
     */
100
    protected static function getUserHomeDir()
101
    {
102
        // Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase
103
        // getenv('HOME') isn't set on Windows and generates a Notice.
104
        $home = getenv('HOME');
105
106
        if (!empty($home)) {
107
            // home should never end with a trailing slash.
108
            $home = rtrim($home, '/');
109
        } elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
110
            // home on windows
111
            $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
112
            // If HOMEPATH is a root directory the path can end with a slash. Make sure
113
            // that doesn't happen.
114
            $home = rtrim($home, '\\/');
115
        }
116
117
        return empty($home) ? null : $home;
118
    }
119
}
120