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 / MongoDB.php
blob1725ce53dc31f04d5e2d08e96146b00124246734
1 <?php
3 /**
4 * Zend Framework (http://framework.zend.com/)
6 * @link http://github.com/zendframework/zf2 for the canonical source repository
7 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
8 * @license http://framework.zend.com/license/new-bsd New BSD License
9 */
11 namespace Zend\Log\Writer;
13 use DateTime;
14 use Mongo;
15 use MongoClient;
16 use MongoDate;
17 use Traversable;
18 use Zend\Log\Exception;
19 use Zend\Log\Formatter\FormatterInterface;
20 use Zend\Stdlib\ArrayUtils;
22 /**
23 * MongoDB log writer.
25 class MongoDB extends AbstractWriter
27 /**
28 * MongoCollection instance
30 * @var MongoCollection
32 protected $mongoCollection;
34 /**
35 * Options used for MongoCollection::save()
37 * @var array
39 protected $saveOptions;
41 /**
42 * Constructor
44 * @param Mongo|MongoClient|array|Traversable $mongo
45 * @param string|MongoDB $database
46 * @param string $collection
47 * @param array $saveOptions
48 * @throws Exception\InvalidArgumentException
50 public function __construct($mongo, $database = null, $collection = null, array $saveOptions = array())
52 if ($mongo instanceof Traversable) {
53 // Configuration may be multi-dimensional due to save options
54 $mongo = ArrayUtils::iteratorToArray($mongo);
56 if (is_array($mongo)) {
57 parent::__construct($mongo);
58 $saveOptions = isset($mongo['save_options']) ? $mongo['save_options'] : array();
59 $collection = isset($mongo['collection']) ? $mongo['collection'] : null;
60 $database = isset($mongo['database']) ? $mongo['database'] : null;
61 $mongo = isset($mongo['mongo']) ? $mongo['mongo'] : null;
64 if (null === $collection) {
65 throw new Exception\InvalidArgumentException(
66 'The collection parameter cannot be empty'
70 if (null === $database) {
71 throw new Exception\InvalidArgumentException(
72 'The database parameter cannot be empty'
76 if (!($mongo instanceof MongoClient || $mongo instanceof Mongo)) {
77 throw new Exception\InvalidArgumentException(sprintf(
78 'Parameter of type %s is invalid; must be MongoClient or Mongo',
79 (is_object($mongo) ? get_class($mongo) : gettype($mongo))
80 ));
83 $this->mongoCollection = $mongo->selectCollection($database, $collection);
84 $this->saveOptions = $saveOptions;
87 /**
88 * This writer does not support formatting.
90 * @param string|FormatterInterface $formatter
91 * @return WriterInterface
93 public function setFormatter($formatter)
95 return $this;
98 /**
99 * Write a message to the log.
101 * @param array $event Event data
102 * @return void
103 * @throws Exception\RuntimeException
105 protected function doWrite(array $event)
107 if (null === $this->mongoCollection) {
108 throw new Exception\RuntimeException('MongoCollection must be defined');
111 if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTime) {
112 $event['timestamp'] = new MongoDate($event['timestamp']->getTimestamp());
115 $this->mongoCollection->save($event, $this->saveOptions);