scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
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 Monolog\Processor;
13  
14 use Monolog\Logger;
15  
16 /**
17 * Injects Git branch and Git commit SHA in all records
18 *
19 * @author Nick Otter
20 * @author Jordi Boggiano <j.boggiano@seld.be>
21 */
22 class GitProcessor
23 {
24 private $level;
25 private static $cache;
26  
27 public function __construct($level = Logger::DEBUG)
28 {
29 $this->level = Logger::toMonologLevel($level);
30 }
31  
32 /**
33 * @param array $record
34 * @return array
35 */
36 public function __invoke(array $record)
37 {
38 // return if the level is not high enough
39 if ($record['level'] < $this->level) {
40 return $record;
41 }
42  
43 $record['extra']['git'] = self::getGitInfo();
44  
45 return $record;
46 }
47  
48 private static function getGitInfo()
49 {
50 if (self::$cache) {
51 return self::$cache;
52 }
53  
54 $branches = `git branch -v --no-abbrev`;
55 if (preg_match('{^\* (.+?)\s+([a-f0-9]{40})(?:\s|$)}m', $branches, $matches)) {
56 return self::$cache = array(
57 'branch' => $matches[1],
58 'commit' => $matches[2],
59 );
60 }
61  
62 return self::$cache = array();
63 }
64 }