|
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
|
19 |
|
public function toJSON() |
|
59
|
|
|
{ |
|
60
|
19 |
|
return json_encode($this->config); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function factory() |
|
64
|
|
|
{ |
|
65
|
|
|
$previous = null; |
|
66
|
|
|
$path = getcwd(); |
|
67
|
|
|
$paths = []; |
|
68
|
|
|
|
|
69
|
|
|
while (($path = realpath($path)) && $path !== $previous) { |
|
70
|
|
|
$paths[] = "{$path}/.cachetool.yml"; |
|
71
|
|
|
$paths[] = "{$path}/.cachetool.yaml"; |
|
72
|
|
|
$previous = $path; |
|
73
|
|
|
$path .= '/../'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($home = static::getUserHomeDir()) { |
|
77
|
|
|
$paths[] = "{$home}/.cachetool.yml"; |
|
78
|
|
|
$paths[] = "{$home}/.cachetool.yaml"; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$paths[] = '/etc/cachetool.yml'; |
|
82
|
|
|
$paths[] = '/etc/cachetool.yaml'; |
|
83
|
|
|
|
|
84
|
|
|
foreach ($paths as $path) { |
|
85
|
|
|
if (is_file($path)) { |
|
86
|
|
|
static::fromFile($path); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return new Config(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public static function fromFile($path) { |
|
94
|
1 |
|
$yaml = new Parser(); |
|
95
|
1 |
|
$config = $yaml->parse(file_get_contents($path)); |
|
96
|
1 |
|
return new Config($config); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Return the user's home directory. |
|
101
|
|
|
* From drush |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected static function getUserHomeDir() |
|
106
|
|
|
{ |
|
107
|
|
|
// Cannot use $_SERVER superglobal since that's empty during UnitUnishTestCase |
|
108
|
|
|
// getenv('HOME') isn't set on Windows and generates a Notice. |
|
109
|
|
|
$home = getenv('HOME'); |
|
110
|
|
|
|
|
111
|
|
|
if (!empty($home)) { |
|
112
|
|
|
// home should never end with a trailing slash. |
|
113
|
|
|
$home = rtrim($home, '/'); |
|
114
|
|
|
} elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { |
|
115
|
|
|
// home on windows |
|
116
|
|
|
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; |
|
117
|
|
|
// If HOMEPATH is a root directory the path can end with a slash. Make sure |
|
118
|
|
|
// that doesn't happen. |
|
119
|
|
|
$home = rtrim($home, '\\/'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return empty($home) ? null : $home; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|