-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
MessageCollector.php
132 lines (121 loc) · 3.4 KB
/
MessageCollector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Neos\Flow\ResourceManagement\Publishing;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Error\Messages\Error;
use Neos\Error\Messages\Message;
use Neos\Error\Messages\Notice;
use Neos\Error\Messages\Warning;
use Neos\Flow\Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* Message Collector
*
* @Flow\Scope("singleton")
*/
class MessageCollector
{
const LOGLEVEL_MAPPING = [
Error::SEVERITY_ERROR => LogLevel::ERROR,
Error::SEVERITY_NOTICE => LogLevel::NOTICE,
Error::SEVERITY_OK => LogLevel::INFO,
Error::SEVERITY_WARNING => LogLevel::WARNING
];
/**
* @var \SplObjectStorage
*/
protected $messages;
/**
* @Flow\Inject(name="Neos.Flow:SystemLogger")
* @var LoggerInterface
*/
protected $logger;
/**
* Injects the (system) logger based on PSR-3.
*
* @param LoggerInterface $logger
* @return void
*/
public function injectLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Message Collector Constructor
*/
public function __construct()
{
$this->messages = new \SplObjectStorage();
}
/**
* @param string $message The message to log
* @param string $severity An integer value, one of the Error::SEVERITY_* constants
* @param integer|null $code A unique error code
* @return void
* @throws Exception
* @api
*/
public function append(string $message, string $severity = Error::SEVERITY_ERROR, ?int $code = null): void
{
switch ($severity) {
case Error::SEVERITY_ERROR:
$notification = new Error($message, $code);
break;
case Error::SEVERITY_WARNING:
$notification = new Warning($message, $code);
break;
case Error::SEVERITY_NOTICE:
$notification = new Notice($message, $code);
break;
case Error::SEVERITY_OK:
$notification = new Message($message, $code);
break;
default:
throw new Exception('Invalid severity', 1455819761);
}
$this->messages->attach($notification);
}
/**
* @return boolean
* @api
*/
public function hasMessages(): bool
{
return $this->messages->count() > 0;
}
/**
* @param callable $callback a callback function to process every notification
* @return void
* @api
*/
public function flush(callable $callback = null): void
{
foreach ($this->messages as $message) {
/** @var Message $message */
$this->messages->detach($message);
$severity = self::LOGLEVEL_MAPPING[$message->getSeverity()];
$this->logger->log($severity, 'ResourcePublishingMessage: ' . $message->getMessage());
if ($callback !== null) {
$callback($message);
}
}
}
/**
* Flush all notification during the object lifecycle
*
* @return void
*/
public function __destruct()
{
$this->flush();
}
}