]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/monolog/monolog/src/Monolog/Processor/MercurialProcessor.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / monolog / monolog / src / Monolog / Processor / MercurialProcessor.php
1 <?php
2
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jonathan A. Schweder <jonathanschweder@gmail.com>
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 Hg branch and Hg revision number in all records
18  *
19  * @author Jonathan A. Schweder <jonathanschweder@gmail.com>
20  */
21 class MercurialProcessor
22 {
23     private $level;
24     private static $cache;
25
26     public function __construct($level = Logger::DEBUG)
27     {
28         $this->level = Logger::toMonologLevel($level);
29     }
30
31     /**
32      * @param  array $record
33      * @return array
34      */
35     public function __invoke(array $record)
36     {
37         // return if the level is not high enough
38         if ($record['level'] < $this->level) {
39             return $record;
40         }
41
42         $record['extra']['hg'] = self::getMercurialInfo();
43
44         return $record;
45     }
46
47     private static function getMercurialInfo()
48     {
49         if (self::$cache) {
50             return self::$cache;
51         }
52
53         $result = explode(' ', trim(`hg id -nb`));
54         if (count($result) >= 3) {
55             return self::$cache = array(
56                 'branch' => $result[1],
57                 'revision' => $result[2],
58             );
59         }
60
61         return self::$cache = array();
62     }
63 }