These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Health monitoring for Yii2 applications |
||
4 | * |
||
5 | * @link https://github.com/hiqdev/yii2-monitoring |
||
6 | * @package yii2-monitoring |
||
7 | * @license BSD-3-Clause |
||
8 | * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
||
9 | */ |
||
10 | |||
11 | namespace hiqdev\yii2\monitoring; |
||
12 | |||
13 | use Yii; |
||
14 | use yii\base\InvalidConfigException; |
||
15 | use yii\helpers\VarDumper; |
||
16 | use yii\log\Logger; |
||
17 | |||
18 | class Module extends \yii\base\Module |
||
19 | { |
||
20 | public $error = []; |
||
21 | |||
22 | public $feedback = []; |
||
23 | |||
24 | public $targets; |
||
25 | |||
26 | public $flag; |
||
27 | |||
28 | public function getTarget($name) |
||
29 | { |
||
30 | if (empty($this->targets[$name])) { |
||
31 | throw new InvalidConfigException("no monitoring target $name defined"); |
||
32 | } |
||
33 | if (!is_object($this->targets[$name])) { |
||
34 | $this->targets[$name] = Yii::createObject($this->targets[$name]); |
||
35 | } |
||
36 | |||
37 | return $this->targets[$name]; |
||
38 | } |
||
39 | |||
40 | protected $_flagText; |
||
41 | |||
42 | public function getFlagText() |
||
43 | { |
||
44 | if ($this->_flagText === null) { |
||
45 | $this->_flagText = $this->detectFlagText($this->flag); |
||
46 | } |
||
47 | |||
48 | return $this->_flagText; |
||
49 | } |
||
50 | |||
51 | public function detectFlagText($flag) |
||
52 | { |
||
53 | if ($flag === 'app') { |
||
54 | return Yii::$app->id; |
||
55 | } elseif ($flag === 'domain') { |
||
56 | return Yii::$app->request->getHostName(); |
||
57 | } else { |
||
58 | return $flag; |
||
59 | } |
||
60 | } |
||
61 | |||
62 | public function flagEmail($email) |
||
63 | { |
||
64 | if (empty($this->flag)) { |
||
65 | return $email; |
||
66 | } |
||
67 | |||
68 | list($nick, $host) = explode('@', $email, 2); |
||
69 | |||
70 | return $nick . '+' . $this->getFlagText() . '@' . $host; |
||
71 | } |
||
72 | |||
73 | public function flagText($text, $delimiter = ' ') |
||
74 | { |
||
75 | if (empty($this->flag)) { |
||
76 | return $text; |
||
77 | } |
||
78 | |||
79 | return '[' . $this->getFlagText() . ']' . $delimiter . $text; |
||
80 | } |
||
81 | |||
82 | public static function getInstance() |
||
83 | { |
||
84 | return Yii::$app->getModule('monitoring'); |
||
85 | } |
||
86 | |||
87 | public function getDebugUrl($sessionTag = null) |
||
88 | { |
||
89 | if (empty($sessionTag)) { |
||
90 | $sessionTag = $this->getDebugTag(); |
||
91 | } |
||
92 | |||
93 | return Yii::$app->getUrlManager()->createAbsoluteUrl([ |
||
94 | '/debug/default/view', |
||
95 | 'panel' => 'log', |
||
96 | 'tag' => $sessionTag, |
||
97 | ]); |
||
98 | } |
||
99 | |||
100 | public function getDebugTag() |
||
101 | { |
||
102 | if (!Yii::$app->hasModule('debug')) { |
||
103 | return null; |
||
104 | } |
||
105 | |||
106 | return Yii::$app->getModule('debug')->logTarget->tag; |
||
107 | } |
||
108 | |||
109 | public function prepareMessageData($message) |
||
110 | { |
||
111 | list($load, $level, $category, $timestamp) = $message; |
||
112 | $dump = $load; |
||
113 | $throwable = null; |
||
114 | if (!is_string($load)) { |
||
115 | // exceptions may not be serializable if in the call stack somewhere is a Closure |
||
116 | if ($load instanceof \Throwable || $load instanceof \Exception) { |
||
117 | $dump = (string) $load; |
||
118 | $throwable = $load; |
||
119 | } else { |
||
120 | $dump = VarDumper::export($load); |
||
121 | } |
||
122 | } |
||
123 | $traces = []; |
||
124 | if (isset($message[4])) { |
||
125 | foreach ($message[4] as $trace) { |
||
126 | $traces[] = "in {$trace['file']}:{$trace['line']}"; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return [ |
||
131 | 'level' => Logger::getLevelName($level), |
||
132 | 'time' => date('c', $timestamp), |
||
133 | 'category' => $category, |
||
134 | // 'prefix' => $target->getMessagePrefix($message), |
||
0 ignored issues
–
show
|
|||
135 | 'traces' => $traces, |
||
136 | 'text' => $dump, |
||
137 | 'throwable' => $throwable, |
||
138 | 'debugUrl' => $this->getDebugUrl(), |
||
139 | ]; |
||
140 | } |
||
141 | |||
142 | public function prepareSubject($message, $subject = null) |
||
143 | { |
||
144 | return $this->flagText($this->rawSubject($message, $subject)); |
||
145 | } |
||
146 | |||
147 | public function rawSubject($message, $subject = null) |
||
148 | { |
||
149 | if ($subject) { |
||
150 | return $subject; |
||
151 | } |
||
152 | |||
153 | $load = reset($message); |
||
154 | |||
155 | if (is_string($load)) { |
||
156 | return $load; |
||
157 | } elseif ($load instanceof \Throwable || $load instanceof \Exception) { |
||
158 | return get_class($load) . ': ' . $load->getMessage(); |
||
159 | } else { |
||
160 | return VarDumper::export($load); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.