1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class to Log messages to Console. |
4
|
|
|
* |
5
|
|
|
* @author Vitex <[email protected]> |
6
|
|
|
* @copyright 2016 [email protected] (G) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ease\Logger; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Description of ToConsole |
13
|
|
|
* |
14
|
|
|
* @author vitex |
15
|
|
|
*/ |
16
|
|
|
class ToConsole extends ToMemory implements Loggingable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Saves obejct instace (singleton...). |
20
|
|
|
*/ |
21
|
|
|
private static $instance = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Standard Output handle |
25
|
|
|
* |
26
|
|
|
* @var resource|false |
27
|
|
|
*/ |
28
|
|
|
public $stdout = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Standard error handle |
32
|
|
|
* |
33
|
|
|
* @var resource |
34
|
|
|
*/ |
35
|
|
|
public $stderr = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Ansi Codes |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected static $ANSI_CODES = array( |
43
|
|
|
"off" => 0, |
44
|
|
|
"bold" => 1, |
45
|
|
|
"italic" => 3, |
46
|
|
|
"underline" => 4, |
47
|
|
|
"blink" => 5, |
48
|
|
|
"inverse" => 7, |
49
|
|
|
"hidden" => 8, |
50
|
|
|
"black" => 30, |
51
|
|
|
"red" => 31, |
52
|
|
|
"green" => 32, |
53
|
|
|
"yellow" => 33, |
54
|
|
|
"blue" => 34, |
55
|
|
|
"magenta" => 35, |
56
|
|
|
"cyan" => 36, |
57
|
|
|
"white" => 37, |
58
|
|
|
"black_bg" => 40, |
59
|
|
|
"red_bg" => 41, |
60
|
|
|
"green_bg" => 42, |
61
|
|
|
"yellow_bg" => 43, |
62
|
|
|
"blue_bg" => 44, |
63
|
|
|
"magenta_bg" => 45, |
64
|
|
|
"cyan_bg" => 46, |
65
|
|
|
"white_bg" => 47 |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Log Status messages to console |
70
|
|
|
*/ |
71
|
|
|
public function __construct() |
72
|
|
|
{ |
73
|
|
|
$this->stdout = fopen('php://stdout', 'w'); |
74
|
|
|
$this->stderr = fopen('php://stderr', 'w'); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set Ansi Color |
79
|
|
|
* |
80
|
|
|
* @param string $str |
81
|
|
|
* @param string $color |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public static function set($str, $color) |
86
|
|
|
{ |
87
|
|
|
$color_attrs = explode("+", $color); |
88
|
|
|
$ansi_str = ""; |
89
|
|
|
foreach ($color_attrs as $attr) { |
90
|
|
|
$ansi_str .= "\033[".self::$ANSI_CODES[$attr]."m"; |
91
|
|
|
} |
92
|
|
|
$ansi_str .= $str."\033[".self::$ANSI_CODES["off"]."m"; |
93
|
|
|
return $ansi_str; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Zapise zapravu do logu. |
98
|
|
|
* |
99
|
|
|
* @param string $caller název volajícího objektu |
100
|
|
|
* @param string $message zpráva |
101
|
|
|
* @param string $type typ zprávy (success|info|error|warning|*) |
102
|
|
|
* |
103
|
|
|
* @return boolean|null byl report zapsán ? |
104
|
|
|
*/ |
105
|
|
|
public function addToLog($caller, $message, $type = 'message') |
106
|
|
|
{ |
107
|
|
|
$message = $this->set( |
108
|
|
|
' '.Message::getTypeUnicodeSymbol($type).' '.strip_tags($message), |
109
|
|
|
self::getTypeColor($type) |
110
|
|
|
); |
111
|
|
|
$logLine = strftime("%D %T").' `'.$caller.'` '.$message; |
112
|
|
|
|
113
|
|
|
switch ($type) { |
114
|
|
|
case 'error': |
115
|
|
|
fputs($this->stderr, $logLine."\n"); |
116
|
|
|
break; |
117
|
|
|
default: |
118
|
|
|
fputs($this->stdout, $logLine."\n"); |
|
|
|
|
119
|
|
|
break; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get color code for given message |
125
|
|
|
* |
126
|
|
|
* @param string $type |
127
|
|
|
*/ |
128
|
|
|
public static function getTypeColor($type) |
129
|
|
|
{ |
130
|
|
|
switch ($type) { |
131
|
|
|
case 'mail': // Envelope |
132
|
|
|
$color = 'blue'; |
133
|
|
|
break; |
134
|
|
|
case 'warning': // Vykřičník v trojůhelníku |
135
|
|
|
$color = 'yellow'; |
136
|
|
|
break; |
137
|
|
|
case 'error': // Lebka |
138
|
|
|
$color = 'red'; |
139
|
|
|
break; |
140
|
|
|
case 'debug': // Kytička |
141
|
|
|
$color = 'magenta'; |
142
|
|
|
break; |
143
|
|
|
case 'success': // Kytička |
144
|
|
|
$color = 'green'; |
145
|
|
|
break; |
146
|
|
|
default: // i v kroužku |
147
|
|
|
$color = 'white'; |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
return $color; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako |
155
|
|
|
* konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho |
156
|
|
|
* instance (ta prvni). |
157
|
|
|
* |
158
|
|
|
* @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a |
159
|
|
|
* priklad |
160
|
|
|
*/ |
161
|
|
|
public static function singleton() |
162
|
|
|
{ |
163
|
|
|
if (!isset(self::$instance)) { |
164
|
|
|
self::$instance = new self(); |
165
|
|
|
} |
166
|
|
|
return self::$instance; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.