-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathConfigManager.php
More file actions
220 lines (182 loc) · 5.54 KB
/
ConfigManager.php
File metadata and controls
220 lines (182 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/*
* This file is part of the PHPCR Shell package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PHPCR\Shell\Config;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
/**
* Configuration manager.
*
* @author Daniel Leech <daniel@dantleech.com>
*/
class ConfigManager
{
/**
* Base filenames of all the possible configuration files
* in the users configuration directory.
*
* @var array
*/
protected $configKeys = [
'alias',
'phpcrsh',
];
/**
* Cached configuration.
*
* @var array
*/
protected $cachedConfig = null;
/**
* Filesystem.
*
* @var Filesystem
*/
protected $filesystem;
/**
* @var QuestionHelper|DialogHelper
*/
protected $questionHelper;
/**
* Constuctor - can optionally accept a Filesystem object
* for testing purposes, otherwise one is created.
*
* @param QuestionHelper|DialogHelper $questionHelper
* @param Filesystem $filesystem
*/
public function __construct($questionHelper, ?Filesystem $filesystem = null)
{
if (null === $filesystem) {
$filesystem = new Filesystem();
}
$this->filesystem = $filesystem;
$this->questionHelper = $questionHelper;
}
/**
* Return the configuration directory.
*
* @return string
*/
public function getConfigDir()
{
$home = getenv('PHPCRSH_HOME');
if ($home) {
return $home;
}
// handle windows ..
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
if (!getenv('APPDATA')) {
throw new \RuntimeException(
'The APPDATA or phpcrsh_HOME environment variable must be set for phpcrsh to run correctly'
);
}
$home = strtr(getenv('APPDATA'), '\\', '/').'/phpcrsh';
return $home;
}
if (!getenv('HOME')) {
throw new \RuntimeException(
'The HOME or phpcrsh_HOME environment variable must be set for phpcrsh to run correctly'
);
}
$home = rtrim(getenv('HOME'), '/').'/.phpcrsh';
return $home;
}
private function getDistConfigDir()
{
return __DIR__.'/../Resources/config.dist';
}
/**
* Load the configuration.
*/
public function loadConfig()
{
$config = [];
$configDir = $this->getConfigDir();
$distConfigDir = $this->getDistConfigDir();
foreach ($this->configKeys as $configKey) {
$fullPath = $configDir.'/'.$configKey.'.yml';
$fullDistPath = $distConfigDir.'/'.$configKey.'.yml';
$config[$configKey] = [];
$userConfig = [];
if ($this->filesystem->exists($fullPath)) {
$userConfig = Yaml::parse(file_get_contents($fullPath));
}
if ($this->filesystem->exists($fullDistPath)) {
$distConfig = Yaml::parse(file_get_contents($fullDistPath));
} else {
throw new \RuntimeException(sprintf(
'Could not find dist config at path (%s)',
$fullDistPath
));
}
$config[$configKey] = new Config(array_merge(
$distConfig,
$userConfig
));
}
$this->cachedConfig = $config;
return $config;
}
/**
* Return the configuration.
*
* @return array
*/
public function getConfig($type)
{
if (null !== $this->cachedConfig) {
return $this->cachedConfig[$type];
}
$this->loadConfig();
return $this->cachedConfig[$type];
}
public function getPhpcrshConfig()
{
return $this->getConfig('phpcrsh');
}
/**
* Initialize a configuration files.
*/
public function initConfig(?OutputInterface $output = null)
{
$log = function ($message) use ($output) {
if ($output) {
$output->writeln($message);
}
};
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
throw new \RuntimeException('This feature is currently only supported on Linux and OSX (maybe). Please submit a PR to support it on windows.');
}
$configDir = $this->getConfigDir();
$distDir = $this->getDistConfigDir();
if (!$this->filesystem->exists($configDir)) {
$log('<info>[+] Creating directory:</info> '.$configDir);
$this->filesystem->mkdir($configDir);
}
$configFilenames = [
'alias.yml',
'phpcrsh.yml',
];
foreach ($configFilenames as $configFilename) {
$srcFile = $distDir.'/'.$configFilename;
$destFile = $configDir.'/'.$configFilename;
if (!$this->filesystem->exists($srcFile)) {
throw new \Exception('Dist (source) file "'.$srcFile.'" does not exist.');
}
if ($this->filesystem->exists($destFile)) {
$log(sprintf('<info>File</info> %s <info> already exists, not overwriting.', $destFile));
return;
}
$this->filesystem->copy($srcFile, $destFile);
$log('<info>[+] Creating file:</info> '.$destFile);
}
}
}