|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace GitWrapper\Event; |
|
4
|
|
|
|
|
5
|
|
|
use GitWrapper\GitException; |
|
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Psr\Log\LogLevel; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class GitLoggerEventSubscriber implements EventSubscriberInterface, LoggerAwareInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Mapping of event to log level. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string[] |
|
17
|
|
|
*/ |
|
18
|
|
|
private $logLevelMappings = [ |
|
19
|
|
|
GitEvents::GIT_PREPARE => LogLevel::INFO, |
|
20
|
|
|
GitEvents::GIT_OUTPUT => LogLevel::DEBUG, |
|
21
|
|
|
GitEvents::GIT_SUCCESS => LogLevel::INFO, |
|
22
|
|
|
GitEvents::GIT_ERROR => LogLevel::ERROR, |
|
23
|
|
|
GitEvents::GIT_BYPASS => LogLevel::INFO, |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var LoggerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $logger; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(LoggerInterface $logger) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->setLogger($logger); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritDoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setLogger(LoggerInterface $logger): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->logger = $logger; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getLogger(): LoggerInterface |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->logger; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function setLogLevelMapping(string $eventName, string $logLevel): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->logLevelMappings[$eventName] = $logLevel; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the log level mapping for an event. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getLogLevelMapping(string $eventName): string |
|
58
|
|
|
{ |
|
59
|
|
|
if (! isset($this->logLevelMappings[$eventName])) { |
|
60
|
|
|
throw new GitException(sprintf('Unknown event "%s"', $eventName)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->logLevelMappings[$eventName]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function getSubscribedEvents() |
|
70
|
|
|
{ |
|
71
|
|
|
return [ |
|
72
|
|
|
GitEvents::GIT_PREPARE => ['onPrepare', 0], |
|
73
|
|
|
GitEvents::GIT_OUTPUT => ['handleOutput', 0], |
|
74
|
|
|
GitEvents::GIT_SUCCESS => ['onSuccess', 0], |
|
75
|
|
|
GitEvents::GIT_ERROR => ['onError', 0], |
|
76
|
|
|
GitEvents::GIT_BYPASS => ['onBypass', 0], |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Adds a log message using the level defined in the mappings. |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed[] $context |
|
84
|
|
|
*/ |
|
85
|
|
|
public function log(GitEvent $gitEvent, string $message, array $context = [], ?string $eventName = null): void |
|
86
|
|
|
{ |
|
87
|
|
|
// Provide backwards compatibility with Symfony 2. |
|
88
|
|
|
if ($eventName === null && method_exists($gitEvent, 'getName')) { |
|
89
|
|
|
$eventName = $gitEvent->getName(); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$method = $this->getLogLevelMapping($eventName); |
|
93
|
|
|
$context += ['command' => $gitEvent->getProcess()->getCommandLine()]; |
|
94
|
|
|
$this->logger->{$method}($message, $context); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function onPrepare(GitEvent $gitEvent, ?string $eventName = null): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->log($gitEvent, 'Git command preparing to run', [], $eventName); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function handleOutput(GitOutputEvent $gitOutputEvent, ?string $eventName = null): void |
|
103
|
|
|
{ |
|
104
|
|
|
$context = ['error' => $gitOutputEvent->isError() ? true : false]; |
|
105
|
|
|
$this->log($gitOutputEvent, $gitOutputEvent->getBuffer(), $context, $eventName); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function onSuccess(GitEvent $gitEvent, ?string $eventName = null): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->log($gitEvent, 'Git command successfully run', [], $eventName); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function onError(GitEvent $gitEvent, ?string $eventName = null): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->log($gitEvent, 'Error running Git command', [], $eventName); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function onBypass(GitEvent $gitEvent, ?string $eventName = null): void |
|
119
|
|
|
{ |
|
120
|
|
|
$this->log($gitEvent, 'Git command bypassed', [], $eventName); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.