Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Log / Writer / FingersCrossed.php
blob270fa451fde9a77b49e3447bbb2089cb4bc055a7
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9 namespace Zend\Log\Writer;
11 use Traversable;
12 use Zend\Stdlib\ArrayUtils;
13 use Zend\Log\Exception;
14 use Zend\Log\Filter\FilterInterface;
15 use Zend\Log\Filter\Priority as PriorityFilter;
16 use Zend\Log\Formatter\FormatterInterface;
17 use Zend\Log\Logger;
18 use Zend\Log\Writer\AbstractWriter;
19 use Zend\Log\Writer\WriterInterface;
20 use Zend\Log\WriterPluginManager;
22 /**
23 * Buffers all events until the strategy determines to flush them.
25 * @see http://packages.python.org/Logbook/api/handlers.html#logbook.FingersCrossedHandler
27 class FingersCrossed extends AbstractWriter
30 /**
31 * The wrapped writer
33 * @var WriterInterface
35 protected $writer;
37 /**
38 * Writer plugins
40 * @var WriterPluginManager
42 protected $writerPlugins;
44 /**
45 * Flag if buffering is enabled
47 * @var bool
49 protected $buffering = true;
51 /**
52 * Oldest entries are removed from the buffer if bufferSize is reached.
53 * 0 is infinte buffer size.
55 * @var int
57 protected $bufferSize;
59 /**
60 * array of log events
62 * @var array
64 protected $buffer = array();
66 /**
67 * Constructor
69 * @param WriterInterface|string|array|Traversable $writer Wrapped writer or array of configuration options
70 * @param FilterInterface|int $filterOrPriority Filter or log priority which determines buffering of events
71 * @param int $bufferSize Maximum buffer size
73 public function __construct($writer, $filterOrPriority = null, $bufferSize = 0)
75 $this->writer = $writer;
77 if ($writer instanceof Traversable) {
78 $writer = ArrayUtils::iteratorToArray($writer);
81 if (is_array($writer)) {
82 $filterOrPriority = isset($writer['priority']) ? $writer['priority'] : null;
83 $bufferSize = isset($writer['bufferSize']) ? $writer['bufferSize'] : null;
84 $writer = isset($writer['writer']) ? $writer['writer'] : null;
87 if (null === $filterOrPriority) {
88 $filterOrPriority = new PriorityFilter(Logger::WARN);
89 } elseif (!$filterOrPriority instanceof FilterInterface) {
90 $filterOrPriority = new PriorityFilter($filterOrPriority);
93 if (is_array($writer) && isset($writer['name'])) {
94 $this->setWriter($writer['name'], $writer['options']);
95 } else {
96 $this->setWriter($writer);
98 $this->addFilter($filterOrPriority);
99 $this->bufferSize = $bufferSize;
103 * Set a new writer
105 * @param string|WriterInterface $writer
106 * @param array|null $options
107 * @return self
108 * @throws Exception\InvalidArgumentException
110 public function setWriter($writer, array $options = null)
112 if (is_string($writer)) {
113 $writer = $this->writerPlugin($writer, $options);
116 if (!$writer instanceof WriterInterface) {
117 throw new Exception\InvalidArgumentException(sprintf(
118 'Writer must implement %s\WriterInterface; received "%s"',
119 __NAMESPACE__,
120 is_object($writer) ? get_class($writer) : gettype($writer)
124 $this->writer = $writer;
125 return $this;
129 * Get writer plugin manager
131 * @return WriterPluginManager
133 public function getWriterPluginManager()
135 if (null === $this->writerPlugins) {
136 $this->setWriterPluginManager(new WriterPluginManager());
138 return $this->writerPlugins;
142 * Set writer plugin manager
144 * @param string|WriterPluginManager $plugins
145 * @return FingersCrossed
146 * @throws Exception\InvalidArgumentException
148 public function setWriterPluginManager($plugins)
150 if (is_string($plugins)) {
151 $plugins = new $plugins;
153 if (!$plugins instanceof WriterPluginManager) {
154 throw new Exception\InvalidArgumentException(sprintf(
155 'Writer plugin manager must extend %s\WriterPluginManager; received %s',
156 __NAMESPACE__,
157 is_object($plugins) ? get_class($plugins) : gettype($plugins)
161 $this->writerPlugins = $plugins;
162 return $this;
166 * Get writer instance
168 * @param string $name
169 * @param array|null $options
170 * @return WriterInterface
172 public function writerPlugin($name, array $options = null)
174 return $this->getWriterPluginManager()->get($name, $options);
178 * Log a message to this writer.
180 * @param array $event log data event
181 * @return void
183 public function write(array $event)
185 $this->doWrite($event);
189 * Check if buffered data should be flushed
191 * @param array $event event data
192 * @return bool true if buffered data should be flushed
194 protected function isActivated(array $event)
196 foreach ($this->filters as $filter) {
197 if (!$filter->filter($event)) {
198 return false;
201 return true;
205 * Write message to buffer or delegate event data to the wrapped writer
207 * @param array $event event data
208 * @return void
210 protected function doWrite(array $event)
212 if (!$this->buffering) {
213 $this->writer->write($event);
214 return;
217 $this->buffer[] = $event;
219 if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) {
220 array_shift($this->buffer);
223 if (!$this->isActivated($event)) {
224 return;
227 $this->buffering = false;
229 foreach ($this->buffer as $bufferedEvent) {
230 $this->writer->write($bufferedEvent);
235 * Resets the state of the handler.
236 * Stops forwarding records to the wrapped writer
238 public function reset()
240 $this->buffering = true;
244 * Stub in accordance to parent method signature.
245 * Fomatters must be set on the wrapped writer.
247 * @param string|FormatterInterface $formatter
248 * @return WriterInterface
250 public function setFormatter($formatter)
252 return $this->writer;
256 * Record shutdown
258 * @return void
260 public function shutdown()
262 $this->writer->shutdown();
263 $this->buffer = null;