forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
165 lines (141 loc) · 3.86 KB
/
Config.php
File metadata and controls
165 lines (141 loc) · 3.86 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
<?php
namespace kriskbx\gtt\Config;
use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Symfony\Component\Yaml\Yaml;
/**
* Class Config
* @package kriskbx\gtt\Config
*/
class Config implements \ArrayAccess, Arrayable
{
/**
* @var string
*/
static $DIR_NAME = '.gtt';
/**
* @var string
*/
static $FILE_NAME = 'config.yml';
/**
* @var string
*/
static $TIMES_DIR = 'times';
/**
* @var string
*/
static $DEPRECATED_FILE = '.gitlab-time.yml';
/**
* @var array
*/
protected $config = [];
/**
* Config constructor.
*/
public function __construct()
{
$this->preFlight();
$this->parse();
$this->defaults();
}
/**
* @return string
*/
public static function getDir()
{
return $_SERVER['HOME'] . DIRECTORY_SEPARATOR . static::$DIR_NAME;
}
/**
* @return string
*/
public static function getFile()
{
return self::getDir() . DIRECTORY_SEPARATOR . static::$FILE_NAME;
}
/**
* @return string
*/
public static function getDeprecatedFile()
{
return $_SERVER['HOME'] . DIRECTORY_SEPARATOR . static::$DEPRECATED_FILE;
}
/**
* @return string
*/
public static function getTimesDir()
{
return self::getDir() . DIRECTORY_SEPARATOR . static::$TIMES_DIR;
}
/**
* Ensure dir and file exist
*/
protected function preFlight()
{
if ( ! file_exists(self::getDir())) {
mkdir(self::getDir(), 0755, true);
}
if ( ! file_exists(self::getTimesDir())) {
mkdir(self::getTimesDir(), 0755, true);
}
if (file_exists(self::getDeprecatedFile())) {
copy(self::getDeprecatedFile(), self::getFile());
unlink(self::getDeprecatedFile());
} elseif ( ! file_exists(self::getFile())) {
touch(self::getFile());
}
}
/**
* Parse config.
*/
protected function parse()
{
try {
$this->config = Yaml::parse(file_get_contents(static::getFile())) ?: [];
} catch (Exception $e) {
echo "Invalid config file '" . static::getFile() . "'.\n";
exit(1);
}
}
/**
* Set default values.
*/
protected function defaults()
{
$this->config = array_merge($this->config, [
'configFile' => static::getFile(),
'url' => @$this->config['url'] ?: 'http://gitlab.com/api/v4/',
'token' => @$this->config['token'] ?: false,
'project' => @$this->config['project'] ?: false,
'closed' => @$this->config['closed'] ?: false,
'milestone' => @$this->config['milestone'] ?: null,
'output' => @$this->config['output'] ?: null,
'hoursPerDay' => @$this->config['hoursPerDay'] ?: 8,
'columns' => @$this->config['columns'] ?: ['iid', 'title', 'estimation'],
'dateFormat' => @$this->config['dateFormat'] ?: 'd.m.Y H:i',
'excludeByLabels' => @$this->config['excludeByLabels'] ?: [],
'includeByLabels' => @$this->config['includeByLabels'] ?: [],
'excludeLabels' => @$this->config['excludeLabels'] ?: [],
'includeLabels' => @$this->config['includeLabels'] ?: []
]);
}
public function offsetExists($offset)
{
return (bool)@$this->config[$offset];
}
public function offsetGet($offset)
{
return $this->config[$offset];
}
public function offsetSet($offset, $value)
{
$this->config[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->config[$offset]);
}
public function toArray()
{
return $this->config;
}
}