1
|
|
|
<?php |
2
|
|
|
namespace Kendrick\SymfonyDebugToolbarGit\DataCollector; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class GitDataCollector |
11
|
|
|
* @package Kendrick\SymfonyDebugToolbarGit\DataCollector |
12
|
|
|
*/ |
13
|
|
|
class GitDataCollector extends DataCollector |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param $repositoryCommitUrl |
18
|
|
|
*/ |
19
|
|
|
public function __construct($repositoryCommitUrl) |
20
|
|
|
{ |
21
|
|
|
$this->data['repositoryCommitUrl'] = $repositoryCommitUrl; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function reset() |
28
|
|
|
{ |
29
|
|
|
$this->data = []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Collect Git data for DebugBar (branch,commit,author,email,merge,date,message) |
34
|
|
|
* |
35
|
|
|
* @param Request $request |
36
|
|
|
* @param Response $response |
37
|
|
|
* @param \Exception $exception |
38
|
|
|
*/ |
39
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
$fs = new Filesystem(); |
43
|
|
|
|
44
|
|
|
// is there a web directory ? |
45
|
|
|
|
46
|
|
|
if ($fs->exists('../web')) { |
47
|
|
|
$gitPath = '../.git'; |
48
|
|
|
} else { |
49
|
|
|
// unit tests |
50
|
|
|
$gitPath = '.git'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// if there is no .git directory |
54
|
|
|
if (!$fs->exists($gitPath)) { |
55
|
|
|
$this->data['gitData'] = false; |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// get latest commit information |
60
|
|
|
exec("git log -1", $data); |
61
|
|
|
|
62
|
|
|
if (isset($data) && !empty($data)) { |
63
|
|
|
|
64
|
|
|
// there is some information |
65
|
|
|
$this->data['gitData'] = true; |
66
|
|
|
|
67
|
|
|
foreach ($data as $d) { |
68
|
|
|
|
69
|
|
|
if (strpos($d, 'commit') === 0) { |
70
|
|
|
|
71
|
|
|
// commit Id |
72
|
|
|
|
73
|
|
|
$this->data['commit'] = substr($d, 7); |
74
|
|
|
|
75
|
|
|
} elseif (strpos($d, 'Author') === 0) { |
76
|
|
|
|
77
|
|
|
// author and email |
78
|
|
|
|
79
|
|
|
preg_match('$Author: ([^<]+)<(.+)>$', $d, $author); |
80
|
|
|
|
81
|
|
|
if (isset($author[1])) { |
82
|
|
|
$this->data['author'] = trim($author[1]); |
83
|
|
|
} |
84
|
|
|
if (isset($author[2])) { |
85
|
|
|
$this->data['email'] = $author[2]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} elseif (strpos($d, 'Date') === 0) { |
89
|
|
|
|
90
|
|
|
$date = trim(substr($d, 5)); // ddd mmm n hh:mm:ss yyyy +gmt |
91
|
|
|
|
92
|
|
|
// date of commit |
93
|
|
|
$dateCommit = date_create($date); |
94
|
|
|
|
95
|
|
|
// actual date at runtime |
96
|
|
|
$dateRuntime = new \DateTime(); |
97
|
|
|
$dateNow = date_create($dateRuntime->format('Y-m-d H:i:s')); |
98
|
|
|
|
99
|
|
|
// difference |
100
|
|
|
$time = date_diff($dateCommit, $dateNow); |
101
|
|
|
|
102
|
|
|
// static time difference : minutes and seconds |
103
|
|
|
$this->data['timeCommitIntervalMinutes'] = $time->format('%y')*365*24*60+$time->format('%m')*30*24*60+$time->format('%d')*24*60+$time->format('%h')*60+$time->format('%i'); |
104
|
|
|
$this->data['timeCommitIntervalSeconds'] = $time->format('%s'); |
105
|
|
|
|
106
|
|
|
// full readable date |
107
|
|
|
$this->data['date'] = $date; |
108
|
|
|
|
109
|
|
|
} elseif (strpos($d, 'Merge') === 0) { |
110
|
|
|
|
111
|
|
|
// merge information |
112
|
|
|
|
113
|
|
|
$this->data['merge'] = trim(substr($d, 6)); |
114
|
|
|
|
115
|
|
|
} else { |
116
|
|
|
|
117
|
|
|
// commit message |
118
|
|
|
|
119
|
|
|
$this->data['message'] = trim($d); |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
unset($data); |
126
|
|
|
|
127
|
|
|
exec("git status", $data); |
128
|
|
|
|
129
|
|
|
if (isset($data[0])) { |
130
|
|
|
|
131
|
|
|
if (strstr($data[0], 'On branch')) { |
132
|
|
|
|
133
|
|
|
// branch name |
134
|
|
|
|
135
|
|
|
$this->data['branch'] = trim(substr($data[0], strpos($data[0], 'On branch')+9)); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} else { |
141
|
|
|
|
142
|
|
|
// no git data |
143
|
|
|
|
144
|
|
|
$this->data['gitData'] = false; |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* true if there is some data : used by the view |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function getGitData() |
157
|
|
|
{ |
158
|
|
|
|
159
|
|
|
return $this->getData('gitData'); |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Actual branch name |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getBranch() |
169
|
|
|
{ |
170
|
|
|
|
171
|
|
|
return $this->getData('branch'); |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Commit ID |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getCommit() |
181
|
|
|
{ |
182
|
|
|
|
183
|
|
|
return $this->getData('commit'); |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Merge information |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function getMerge() |
193
|
|
|
{ |
194
|
|
|
|
195
|
|
|
return $this->getData('merge'); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Author |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public function getAuthor() |
205
|
|
|
{ |
206
|
|
|
|
207
|
|
|
return $this->getData('author'); |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Author's email |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
public function getEmail() |
217
|
|
|
{ |
218
|
|
|
|
219
|
|
|
return $this->getData('email'); |
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Commit date |
225
|
|
|
* |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public function getDate() |
229
|
|
|
{ |
230
|
|
|
|
231
|
|
|
return $this->getData('date'); |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Minutes since last commit |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
public function getTimeCommitIntervalMinutes() |
241
|
|
|
{ |
242
|
|
|
|
243
|
|
|
return $this->getData('timeCommitIntervalMinutes'); |
244
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Seconds since latest commit |
249
|
|
|
* |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
|
|
public function getTimeCommitIntervalSeconds() |
253
|
|
|
{ |
254
|
|
|
|
255
|
|
|
return $this->getData('timeCommitIntervalSeconds'); |
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Commit message |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
public function getMessage() |
265
|
|
|
{ |
266
|
|
|
|
267
|
|
|
return $this->getData('message'); |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Commit URL |
273
|
|
|
* |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
|
|
public function getCommitUrl() |
277
|
|
|
{ |
278
|
|
|
|
279
|
|
|
return $this->data['repositoryCommitUrl']; |
280
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Checks and returns the data |
285
|
|
|
* |
286
|
|
|
* @param string $data |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
private function getData($data) |
290
|
|
|
{ |
291
|
|
|
|
292
|
|
|
return (isset($this->data[$data])) ? $this->data[$data] : ''; |
293
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* DataCollector name : used by service declaration into container.yml |
298
|
|
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
public function getName() |
302
|
|
|
{ |
303
|
|
|
|
304
|
|
|
return 'datacollector_git'; |
305
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* change the icon color depending on the kernel version |
310
|
|
|
* |
311
|
|
|
* #3f3f3f < 2.8 |
312
|
|
|
* #AAAAAA >= 2.8 |
313
|
|
|
* |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
|
|
final public function getIconColor() |
317
|
|
|
{ |
318
|
|
|
if ((float) $this->getSymfonyVersion() >= 2.8) { |
319
|
|
|
return $this->data['iconColor'] = '#AAAAAA'; |
320
|
|
|
} |
321
|
|
|
return $this->data['iconColor'] = '#3F3F3F'; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return string |
326
|
|
|
*/ |
327
|
|
|
private function getSymfonyVersion() |
328
|
|
|
{ |
329
|
|
|
$symfonyVersion = \Symfony\Component\HttpKernel\Kernel::VERSION; |
330
|
|
|
$symfonyVersion = explode('.', $symfonyVersion, -1); |
331
|
|
|
$symfonyMajorMinorVersion = implode('.', $symfonyVersion); |
332
|
|
|
return $symfonyMajorMinorVersion; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
} |
336
|
|
|
|