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