|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayObject; |
|
6
|
|
|
use SplObjectStorage; |
|
7
|
|
|
use SplSubject; |
|
8
|
|
|
use SplObserver; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @see https://www.php.net/manual/zh/class.splsubject.php |
|
13
|
|
|
*/ |
|
14
|
|
|
class EventSubject extends ArrayObject implements SplSubject |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \SplObjectStorage |
|
18
|
|
|
*/ |
|
19
|
|
|
private $observers; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Psr\Log\LoggerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $logger; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Construct a new event subject. |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
14 |
|
public function __construct(array $input = [], LoggerInterface $logger) |
|
32
|
|
|
{ |
|
33
|
14 |
|
parent::__construct($input); |
|
34
|
14 |
|
$this->observers = new SplObjectStorage; |
|
35
|
14 |
|
$this->logger = $logger; |
|
36
|
14 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Attach an SplObserver. |
|
40
|
|
|
* |
|
41
|
|
|
* @param \SplObserver $observer |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function attach(SplObserver $observer): void |
|
45
|
|
|
{ |
|
46
|
4 |
|
$this->observers->attach($observer); |
|
47
|
4 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Detach an observer. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \SplObserver $observer |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function detach(SplObserver $observer): void |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->observers->detach($observer); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Notify an observer. |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function notify(): void |
|
66
|
|
|
{ |
|
67
|
4 |
|
foreach ($this->observers as $observer) { |
|
68
|
2 |
|
$observer->update($this); |
|
69
|
2 |
|
$results = $observer->getResults(); |
|
70
|
2 |
|
foreach ($results as $result) { |
|
71
|
2 |
|
$level = array_shift($result); |
|
72
|
2 |
|
call_user_func_array([$this->logger, $level], $result); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get environment variables |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function getEnv(): array |
|
83
|
|
|
{ |
|
84
|
2 |
|
if ($this['object_kind'] == 'push') { |
|
85
|
|
|
return [ |
|
86
|
|
|
// The commit hash being checked out. |
|
87
|
1 |
|
'GIT_COMMIT' => $this['checkout_sha'], |
|
88
|
|
|
// The remote branch name, if any. |
|
89
|
1 |
|
'GIT_BRANCH' => substr($this['ref'], 11), |
|
90
|
|
|
// The remote URL. |
|
91
|
1 |
|
'GIT_URL' => $this['repository']['url'], |
|
92
|
|
|
// Gitlab homepage URL. |
|
93
|
1 |
|
'GIT_HOMEPAGE' => $this['repository']['homepage'], |
|
94
|
|
|
// The configured Git committer name, if any. |
|
95
|
1 |
|
'GIT_COMMITTER_NAME' => $this['user_name'], |
|
96
|
|
|
// The configured Git committer email, if any. |
|
97
|
1 |
|
'GIT_COMMITTER_EMAIL' => $this['user_email'], |
|
98
|
|
|
]; |
|
99
|
|
|
} else { |
|
100
|
1 |
|
return []; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |