|
| 1 | +<?php namespace Norm\Connection; |
| 2 | + |
| 3 | + |
| 4 | +use DateTime; |
| 5 | +use Exception; |
| 6 | +use Norm\Model; |
| 7 | +use MongoDB\Driver\Manager as MongoClient; |
| 8 | +use MongoDB\BSON\UTCDateTime as MongoDate; |
| 9 | +use MongoDB\BSON\ObjectId as MongoId; |
| 10 | +use Norm\Connection; |
| 11 | +use Norm\Collection; |
| 12 | +use Norm\Type\NObject; |
| 13 | +use Norm\Type\NDateTime as NormDateTime; |
| 14 | +use Norm\Type\NormArray; |
| 15 | +use Norm\Cursor\MongoCursor; |
| 16 | + |
| 17 | +/** |
| 18 | + * Mongo Connection. |
| 19 | + * |
| 20 | + * @author Ganesha <reekoheek@gmail.com> |
| 21 | + * @copyright 2013 PT Sagara Xinix Solusitama |
| 22 | + * @link http://xinix.co.id/products/norm Norm |
| 23 | + * @license https://raw.github.com/xinix-technology/norm/master/LICENSE |
| 24 | + */ |
| 25 | +class MongoDBConnection extends Connection |
| 26 | +{ |
| 27 | + /** |
| 28 | + * MongoDB client object |
| 29 | + * |
| 30 | + * @var \MongoClient |
| 31 | + */ |
| 32 | + protected $client; |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritDoc} |
| 36 | + */ |
| 37 | + public function __construct($options) |
| 38 | + { |
| 39 | + parent::__construct($options); |
| 40 | + |
| 41 | + $defaultOptions = array( |
| 42 | + 'hostname' => '127.0.0.1', |
| 43 | + 'port' => '27017', |
| 44 | + ); |
| 45 | + |
| 46 | + $this->options = $options + $defaultOptions; |
| 47 | + |
| 48 | + if (isset($this->options['connectionString'])) { |
| 49 | + $connectionString = $this->options['connectionString']; |
| 50 | + } else { |
| 51 | + $hostname = $this->options['hostname']; |
| 52 | + $port = $this->options['port']; |
| 53 | + |
| 54 | + if (isset($this->options['database'])) { |
| 55 | + $database = $this->options['database']; |
| 56 | + } else { |
| 57 | + throw new Exception('[Norm/MongoConnection] Missing database name, check your configuration!'); |
| 58 | + } |
| 59 | + |
| 60 | + $prefix = ''; |
| 61 | + |
| 62 | + if (isset($this->options['username'])) { |
| 63 | + $prefix = $this->options['username'].':'.$this->options['password'].'@'; |
| 64 | + } |
| 65 | + |
| 66 | + $connectionString = "mongodb://$prefix$hostname:$port/$database"; |
| 67 | + } |
| 68 | + |
| 69 | + $this->client = new MongoClient($connectionString); |
| 70 | + $this->raw = $this->client->$database; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * {@inheritDoc} |
| 75 | + */ |
| 76 | + public function query($collection, array $criteria = array()) |
| 77 | + { |
| 78 | + return new MongoCursor($this->factory($collection), $criteria); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * {@inheritDoc} |
| 83 | + */ |
| 84 | + public function persist($collection, array $document) |
| 85 | + { |
| 86 | + if ($collection instanceof Collection) { |
| 87 | + $collection = $collection->getName(); |
| 88 | + } |
| 89 | + |
| 90 | + $marshalledDocument = $this->marshall($document); |
| 91 | + |
| 92 | + $result = false; |
| 93 | + |
| 94 | + if (isset($document['$id'])) { |
| 95 | + $criteria = array( |
| 96 | + '_id' => new MongoId($document['$id']), |
| 97 | + ); |
| 98 | + |
| 99 | + $marshalledDocument = $this->raw->$collection->findAndModify( |
| 100 | + $criteria, |
| 101 | + array('$set' => $marshalledDocument), |
| 102 | + null, |
| 103 | + array('new' => true) |
| 104 | + ); |
| 105 | + } else { |
| 106 | + $retval = $this->raw->$collection->insert($marshalledDocument); |
| 107 | + |
| 108 | + if (!$retval['ok']) { |
| 109 | + throw new Exception($retval['errmsg']); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return $this->unmarshall($marshalledDocument); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * {@inheritDoc} |
| 118 | + */ |
| 119 | + public function remove($collection, $criteria = null) |
| 120 | + { |
| 121 | + if ($collection instanceof Collection) { |
| 122 | + $collection = $collection->getName(); |
| 123 | + } |
| 124 | + |
| 125 | + if (func_num_args() === 1) { |
| 126 | + $result = $this->raw->$collection->remove(); |
| 127 | + } else { |
| 128 | + if ($criteria instanceof Model) { |
| 129 | + $criteria = $criteria->getId(); |
| 130 | + } |
| 131 | + |
| 132 | + if (is_string($criteria)) { |
| 133 | + $criteria = array( |
| 134 | + '_id' => new MongoId($criteria), |
| 135 | + ); |
| 136 | + } elseif (! is_array($criteria)) { |
| 137 | + throw new Exception('[Norm/Connection] Cannot remove with specified criteria. Criteria must be array, string, or model'); |
| 138 | + } |
| 139 | + |
| 140 | + $result = $this->raw->$collection->remove($criteria); |
| 141 | + } |
| 142 | + |
| 143 | + if ((int) $result['ok'] !== 1) { |
| 144 | + throw new Exception($result['errmsg']); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Get MongoDB client |
| 150 | + * |
| 151 | + * @return MongoClient MongoDB client |
| 152 | + */ |
| 153 | + public function getClient() |
| 154 | + { |
| 155 | + return $this->client; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * {@inheritDoc} |
| 160 | + */ |
| 161 | + public function unmarshall($object) |
| 162 | + { |
| 163 | + if (isset($object['_id'])) { |
| 164 | + $object['$id'] = (string) $object['_id']; |
| 165 | + unset($object['_id']); |
| 166 | + } |
| 167 | + |
| 168 | + foreach ($object as $key => &$value) { |
| 169 | + if ($value instanceof MongoDate) { |
| 170 | + $value = new DateTime('@'.$value->sec); |
| 171 | + } elseif ($value instanceof MongoId) { |
| 172 | + $value = (string) $value; |
| 173 | + } |
| 174 | + |
| 175 | + if ($key[0] === '_') { |
| 176 | + unset($object[$key]); |
| 177 | + $key[0] = '$'; |
| 178 | + $object[$key] = $value; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return $object; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * {@inheritDoc} |
| 187 | + */ |
| 188 | + public function marshall($object) |
| 189 | + { |
| 190 | + if ($object instanceof NormDateTime) { |
| 191 | + return new MongoDate($object->getTimestamp()); |
| 192 | + } elseif ($object instanceof NormArray) { |
| 193 | + return $object->toArray(); |
| 194 | + } elseif ($object instanceof NObject) { |
| 195 | + return $object->toObject(); |
| 196 | + } else { |
| 197 | + return parent::marshall($object); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments