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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zend-log for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Log\Writer;
use Traversable;
use Zend\Log\Exception;
use Zend\Log\Filter\FilterInterface;
use Zend\Log\Filter\Priority as PriorityFilter;
use Zend\Log\Formatter\FormatterInterface;
use Zend\Log\Logger;
use Zend\Log\WriterPluginManager;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
/**
* Buffers all events until the strategy determines to flush them.
*
* @see http://packages.python.org/Logbook/api/handlers.html#logbook.FingersCrossedHandler
*/
class FingersCrossed extends AbstractWriter
{
/**
* The wrapped writer
*
* @var WriterInterface
*/
protected $writer;
/**
* Writer plugins
*
* @var WriterPluginManager
*/
protected $writerPlugins;
/**
* Flag if buffering is enabled
*
* @var bool
*/
protected $buffering = true;
/**
* Oldest entries are removed from the buffer if bufferSize is reached.
* 0 is infinte buffer size.
*
* @var int
*/
protected $bufferSize;
/**
* array of log events
*
* @var array
*/
protected $buffer = [];
/**
* Constructor
*
* @param WriterInterface|string|array|Traversable $writer Wrapped writer or array of configuration options
* @param FilterInterface|int $filterOrPriority Filter or log priority which determines buffering of events
* @param int $bufferSize Maximum buffer size
*/
public function __construct($writer, $filterOrPriority = null, $bufferSize = 0)
{
$this->writer = $writer;
if ($writer instanceof Traversable) {
$writer = ArrayUtils::iteratorToArray($writer);
}
if (is_array($writer)) {
$filterOrPriority = isset($writer['priority']) ? $writer['priority'] : null;
$bufferSize = isset($writer['bufferSize']) ? $writer['bufferSize'] : null;
$writer = isset($writer['writer']) ? $writer['writer'] : null;
}
if (null === $filterOrPriority) {
$filterOrPriority = new PriorityFilter(Logger::WARN);
} elseif (! $filterOrPriority instanceof FilterInterface) {
$filterOrPriority = new PriorityFilter($filterOrPriority);
}
if (is_array($writer) && isset($writer['name'])) {
$this->setWriter($writer['name'], $writer['options']);
} else {
$this->setWriter($writer);
}
$this->addFilter($filterOrPriority);
$this->bufferSize = $bufferSize;
}
/**
* Set a new writer
*
* @param string|WriterInterface $writer
* @param array|null $options
* @return self
* @throws Exception\InvalidArgumentException
*/
public function setWriter($writer, array $options = null)
{
if (is_string($writer)) {
$writer = $this->writerPlugin($writer, $options);
}
if (! $writer instanceof WriterInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'Writer must implement %s\WriterInterface; received "%s"',
__NAMESPACE__,
is_object($writer) ? get_class($writer) : gettype($writer)
));
}
$this->writer = $writer;
return $this;
}
/**
* Get writer plugin manager
*
* @return WriterPluginManager
*/
public function getWriterPluginManager()
{
if (null === $this->writerPlugins) {
$this->setWriterPluginManager(new WriterPluginManager(new ServiceManager()));
}
return $this->writerPlugins;
}
/**
* Set writer plugin manager
*
* @param string|WriterPluginManager $plugins
* @return FingersCrossed
* @throws Exception\InvalidArgumentException
*/
public function setWriterPluginManager($plugins)
{
if (is_string($plugins)) {
$plugins = new $plugins;
}
if (! $plugins instanceof WriterPluginManager) {
throw new Exception\InvalidArgumentException(sprintf(
'Writer plugin manager must extend %s\WriterPluginManager; received %s',
__NAMESPACE__,
is_object($plugins) ? get_class($plugins) : gettype($plugins)
));
}
$this->writerPlugins = $plugins;
return $this;
}
/**
* Get writer instance
*
* @param string $name
* @param array|null $options
* @return WriterInterface
*/
public function writerPlugin($name, array $options = null)
{
return $this->getWriterPluginManager()->get($name, $options);
}
/**
* Log a message to this writer.
*
* @param array $event log data event
* @return void
*/
public function write(array $event)
{
$this->doWrite($event);
}
/**
* Check if buffered data should be flushed
*
* @param array $event event data
* @return bool true if buffered data should be flushed
*/
protected function isActivated(array $event)
{
foreach ($this->filters as $filter) {
if (! $filter->filter($event)) {
return false;
}
}
return true;
}
/**
* Write message to buffer or delegate event data to the wrapped writer
*
* @param array $event event data
* @return void
*/
protected function doWrite(array $event)
{
if (! $this->buffering) {
$this->writer->write($event);
return;
}
$this->buffer[] = $event;
if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) {
array_shift($this->buffer);
}
if (! $this->isActivated($event)) {
return;
}
$this->buffering = false;
foreach ($this->buffer as $bufferedEvent) {
$this->writer->write($bufferedEvent);
}
}
/**
* Resets the state of the handler.
* Stops forwarding records to the wrapped writer
*/
public function reset()
{
$this->buffering = true;
}
/**
* Stub in accordance to parent method signature.
* Fomatters must be set on the wrapped writer.
*
* @param string|FormatterInterface $formatter
* @param array|null $options (unused)
* @return WriterInterface
*/
public function setFormatter($formatter, array $options = null)
{
return $this->writer;
}
/**
* Record shutdown
*
* @return void
*/
public function shutdown()
{
$this->writer->shutdown();
$this->buffer = null;
}
}