1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Deployer; |
4
|
|
|
|
5
|
|
|
function sendHttpRequest($url, $formParams) |
6
|
|
|
{ |
7
|
|
|
$formParams = json_encode($formParams); |
8
|
|
|
|
9
|
|
|
$ch = curl_init($url); |
10
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
11
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $formParams); |
12
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
13
|
|
|
curl_setopt( |
14
|
|
|
$ch, |
15
|
|
|
CURLOPT_HTTPHEADER, |
16
|
|
|
[ |
17
|
|
|
'Content-Type: application/json', |
18
|
|
|
'Content-Length: '.strlen($formParams), |
19
|
|
|
] |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
return curl_exec($ch); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function sendGroupMessage($subject) |
26
|
|
|
{ |
27
|
|
|
$url = get('notify_channel_url'); |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
if (! $url) { |
31
|
|
|
throw new \InvalidArgumentException('[Laravel-Deployer]Notification is on but channel url is not set!'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$notifyBy = get('notify_by', 'webhook'); |
35
|
|
|
|
36
|
|
|
switch ($notifyBy) { |
37
|
|
|
case 'wechat_bot': |
38
|
|
|
$formParams = [ |
39
|
|
|
'msgtype' => 'news', |
40
|
|
|
'news' => [ |
41
|
|
|
'articles' => [ |
42
|
|
|
[ |
43
|
|
|
'title' => get('user') . ' ' . $subject, |
44
|
|
|
'description' => '在 ' . get('environment') . ' 环境更新 ' . get('branch') . ' 分支 ', |
45
|
|
|
'url' => get('app_repo_url', 'https://github.com'), |
46
|
|
|
'picurl' => get('pic_url', 'https://picsum.photos/id/' . rand(1, 1000) . '/800/600') |
47
|
|
|
] |
48
|
|
|
] |
49
|
|
|
] |
50
|
|
|
]; |
51
|
|
|
break; |
52
|
|
|
default: |
53
|
|
|
$content = implode("\n", [ |
54
|
|
|
$subject, |
55
|
|
|
'应用名称: '.get('application'), |
56
|
|
|
'发布者: '.get('user'), |
57
|
|
|
'分支名: '.get('branch'), |
58
|
|
|
'环境: '.get('environment'), |
59
|
|
|
]); |
60
|
|
|
$formParams = ['text' => $content]; |
61
|
|
|
break; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return get('group_notify') ? sendHttpRequest($url, $formParams) : writeln($content); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
task('success:notify', function () { |
68
|
|
|
return sendGroupMessage('成功发布新版本!'); |
69
|
|
|
})->local(); |
70
|
|
|
|
71
|
|
|
task('failed:notify', function () { |
72
|
|
|
return sendGroupMessage('发布新版本失败!'); |
73
|
|
|
})->local(); |
74
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: