|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Set of scripts for Jetpack. |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-scripts |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Scripts; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Fancy logger |
|
12
|
|
|
*/ |
|
13
|
|
|
class Logger { |
|
14
|
|
|
/** |
|
15
|
|
|
* Array of foreground colors |
|
16
|
|
|
* |
|
17
|
|
|
* @var Array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $foreground_colors = array(); |
|
20
|
|
|
/** |
|
21
|
|
|
* Array of background colors |
|
22
|
|
|
* |
|
23
|
|
|
* @var Array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $background_colors = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A generic log command |
|
29
|
|
|
* |
|
30
|
|
|
* @param String $string string to log. |
|
31
|
|
|
* @param String $foreground_color foreground color. |
|
|
|
|
|
|
32
|
|
|
* @param String $background_color background color. |
|
|
|
|
|
|
33
|
|
|
*/ |
|
34
|
|
|
public static function log( $string, $foreground_color = null, $background_color = null ) { |
|
35
|
|
|
$logger = new Logger(); |
|
36
|
|
|
$out = $string; |
|
37
|
|
|
|
|
38
|
|
|
if ( ! is_string( $out ) ) { |
|
39
|
|
|
$out = print_r( $string, 1 ); //phpcs:ignore |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
|
43
|
|
|
error_log( $logger->get_colored_string( $out, $foreground_color, $background_color ) ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Info log command in yellow |
|
48
|
|
|
* |
|
49
|
|
|
* @param String $string string to log. |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function info( $string ) { |
|
52
|
|
|
self::log( $string, 'yellow' ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set up shell colors |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct() { |
|
59
|
|
|
$this->foreground_colors['black'] = '0;30'; |
|
60
|
|
|
$this->foreground_colors['dark_gray'] = '1;30'; |
|
61
|
|
|
$this->foreground_colors['blue'] = '0;34'; |
|
62
|
|
|
$this->foreground_colors['light_blue'] = '1;34'; |
|
63
|
|
|
$this->foreground_colors['green'] = '0;32'; |
|
64
|
|
|
$this->foreground_colors['light_green'] = '1;32'; |
|
65
|
|
|
$this->foreground_colors['cyan'] = '0;36'; |
|
66
|
|
|
$this->foreground_colors['light_cyan'] = '1;36'; |
|
67
|
|
|
$this->foreground_colors['red'] = '0;31'; |
|
68
|
|
|
$this->foreground_colors['light_red'] = '1;31'; |
|
69
|
|
|
$this->foreground_colors['purple'] = '0;35'; |
|
70
|
|
|
$this->foreground_colors['light_purple'] = '1;35'; |
|
71
|
|
|
$this->foreground_colors['brown'] = '0;33'; |
|
72
|
|
|
$this->foreground_colors['yellow'] = '1;33'; |
|
73
|
|
|
$this->foreground_colors['light_gray'] = '0;37'; |
|
74
|
|
|
$this->foreground_colors['white'] = '1;37'; |
|
75
|
|
|
|
|
76
|
|
|
$this->background_colors['black'] = '40'; |
|
77
|
|
|
$this->background_colors['red'] = '41'; |
|
78
|
|
|
$this->background_colors['green'] = '42'; |
|
79
|
|
|
$this->background_colors['yellow'] = '43'; |
|
80
|
|
|
$this->background_colors['blue'] = '44'; |
|
81
|
|
|
$this->background_colors['magenta'] = '45'; |
|
82
|
|
|
$this->background_colors['cyan'] = '46'; |
|
83
|
|
|
$this->background_colors['light_gray'] = '47'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns colored string |
|
88
|
|
|
* |
|
89
|
|
|
* @param String $string string to log. |
|
90
|
|
|
* @param String $foreground_color foreground color. |
|
|
|
|
|
|
91
|
|
|
* @param String $background_color background color. |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
|
|
public function get_colored_string( $string, $foreground_color = null, $background_color = null ) { |
|
94
|
|
|
$colored_string = ''; |
|
95
|
|
|
|
|
96
|
|
|
// Check if given foreground color found. |
|
97
|
|
|
if ( isset( $this->foreground_colors[ $foreground_color ] ) ) { |
|
98
|
|
|
$colored_string .= "\033[" . $this->foreground_colors[ $foreground_color ] . 'm'; |
|
99
|
|
|
} |
|
100
|
|
|
// Check if given background color found. |
|
101
|
|
|
if ( isset( $this->background_colors[ $background_color ] ) ) { |
|
102
|
|
|
$colored_string .= "\033[" . $this->background_colors[ $background_color ] . 'm'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Add string and end coloring. |
|
106
|
|
|
$colored_string .= $string . "\033[0m"; |
|
107
|
|
|
|
|
108
|
|
|
return $colored_string; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns all foreground color names |
|
113
|
|
|
*/ |
|
114
|
|
|
public function get_foreground_colors() { |
|
115
|
|
|
return array_keys( $this->foreground_colors ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns all background color names |
|
120
|
|
|
*/ |
|
121
|
|
|
public function get_background_colors() { |
|
122
|
|
|
return array_keys( $this->background_colors ); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.