PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Doctrine/MongoDB/Traits/LoggableCollectionTrait.php

http://github.com/doctrine/mongodb
PHP | 348 lines | 214 code | 50 blank | 84 comment | 1 complexity | 4b68225d8e3a34462c9273b4d3950554 MD5 | raw file
  1. <?php
  2. namespace Doctrine\MongoDB\Traits;
  3. /**
  4. * {@internal It's used by loggable collection classes }}
  5. */
  6. trait LoggableCollectionTrait
  7. {
  8. /**
  9. * The logger callable.
  10. *
  11. * @var callable
  12. */
  13. protected $loggerCallable;
  14. /**
  15. * Log something using the configured logger callable.
  16. *
  17. * @see Loggable::log()
  18. * @param array $log
  19. */
  20. public function log(array $log)
  21. {
  22. $log['db'] = $this->database->getName();
  23. $log['collection'] = $this->getName();
  24. call_user_func($this->loggerCallable, $log);
  25. }
  26. /**
  27. * @see Collection::aggregate()
  28. */
  29. public function aggregate(array $pipeline, array $options = [] /* , array $op, ... */)
  30. {
  31. if ( ! array_key_exists(0, $pipeline)) {
  32. $pipeline = func_get_args();
  33. $options = [];
  34. }
  35. $this->log([
  36. 'aggregate' => true,
  37. 'pipeline' => $pipeline,
  38. 'options' => $options,
  39. ]);
  40. return parent::aggregate($pipeline, $options);
  41. }
  42. /**
  43. * @see Collection::batchInsert()
  44. */
  45. public function batchInsert(array &$a, array $options = [])
  46. {
  47. $this->log([
  48. 'batchInsert' => true,
  49. 'num' => count($a),
  50. 'data' => $a,
  51. 'options' => $options,
  52. ]);
  53. return parent::batchInsert($a, $options);
  54. }
  55. /**
  56. * @see Collection::count()
  57. */
  58. public function count(array $query = [], $limitOrOptions = 0, $skip = 0)
  59. {
  60. $options = is_array($limitOrOptions)
  61. ? array_merge(['limit' => 0, 'skip' => 0], $limitOrOptions)
  62. : ['limit' => $limitOrOptions, 'skip' => $skip];
  63. $this->log([
  64. 'count' => true,
  65. 'query' => $query,
  66. 'options' => $options,
  67. /* @deprecated 1.2 Replaced by options; will be removed for 2.0 */
  68. 'limit' => $options['limit'],
  69. 'skip' => $options['skip'],
  70. ]);
  71. return parent::count($query, $options);
  72. }
  73. /* Collection::createDBRef() is intentionally omitted because it does not
  74. * interact with the server.
  75. */
  76. /**
  77. * @see Collection::deleteIndex()
  78. */
  79. public function deleteIndex($keys)
  80. {
  81. $this->log([
  82. 'deleteIndex' => true,
  83. 'keys' => $keys,
  84. ]);
  85. return parent::deleteIndex($keys);
  86. }
  87. /**
  88. * @see Collection::deleteIndexes()
  89. */
  90. public function deleteIndexes()
  91. {
  92. $this->log(['deleteIndexes' => true]);
  93. return parent::deleteIndexes();
  94. }
  95. /**
  96. * @see Collection::distinct()
  97. */
  98. public function distinct($field, array $query = [], array $options = [])
  99. {
  100. $this->log([
  101. 'distinct' => true,
  102. 'field' => $field,
  103. 'query' => $query,
  104. 'options' => $options,
  105. ]);
  106. return parent::distinct($field, $query, $options);
  107. }
  108. /**
  109. * @see Collection::drop()
  110. */
  111. public function drop()
  112. {
  113. $this->log(['drop' => true]);
  114. return parent::drop();
  115. }
  116. /**
  117. * @see Collection::ensureIndex()
  118. */
  119. public function ensureIndex(array $keys, array $options = [])
  120. {
  121. $this->log([
  122. 'ensureIndex' => true,
  123. 'keys' => $keys,
  124. 'options' => $options,
  125. ]);
  126. return parent::ensureIndex($keys, $options);
  127. }
  128. /**
  129. * @see Collection::find()
  130. */
  131. public function find(array $query = [], array $fields = [])
  132. {
  133. $this->log([
  134. 'find' => true,
  135. 'query' => $query,
  136. 'fields' => $fields,
  137. ]);
  138. return parent::find($query, $fields);
  139. }
  140. /**
  141. * @see Collection::findAndRemove()
  142. */
  143. public function findAndRemove(array $query, array $options = [])
  144. {
  145. $this->log([
  146. 'findAndRemove' => true,
  147. 'query' => $query,
  148. 'options' => $options,
  149. ]);
  150. return parent::findAndRemove($query, $options);
  151. }
  152. /**
  153. * @see Collection::findAndUpdate()
  154. */
  155. public function findAndUpdate(array $query, array $newObj, array $options = [])
  156. {
  157. $this->log([
  158. 'findAndUpdate' => true,
  159. 'query' => $query,
  160. 'newObj' => $newObj,
  161. 'options' => $options,
  162. ]);
  163. return parent::findAndUpdate($query, $newObj, $options);
  164. }
  165. /**
  166. * @see Collection::findOne()
  167. */
  168. public function findOne(array $query = [], array $fields = [])
  169. {
  170. $this->log([
  171. 'findOne' => true,
  172. 'query' => $query,
  173. 'fields' => $fields,
  174. ]);
  175. return parent::findOne($query, $fields);
  176. }
  177. /**
  178. * @see Collection::getDBRef()
  179. */
  180. public function getDBRef(array $reference)
  181. {
  182. $this->log([
  183. 'getDBRef' => true,
  184. 'reference' => $reference,
  185. ]);
  186. return parent::getDBRef($reference);
  187. }
  188. /**
  189. * @see Collection::getIndexInfo()
  190. */
  191. public function getIndexInfo()
  192. {
  193. $this->log(['getIndexInfo' => true]);
  194. return parent::getIndexInfo();
  195. }
  196. /**
  197. * @see Collection::group()
  198. */
  199. public function group($keys, array $initial, $reduce, array $options = [])
  200. {
  201. $this->log([
  202. 'group' => true,
  203. 'keys' => $keys,
  204. 'initial' => $initial,
  205. 'reduce' => $reduce,
  206. 'options' => $options,
  207. ]);
  208. return parent::group($keys, $initial, $reduce, $options);
  209. }
  210. /**
  211. * @see Collection::insert()
  212. */
  213. public function insert(array &$a, array $options = [])
  214. {
  215. $this->log([
  216. 'insert' => true,
  217. 'document' => $a,
  218. 'options' => $options,
  219. ]);
  220. return parent::insert($a, $options);
  221. }
  222. /**
  223. * @see Collection::mapReduce()
  224. */
  225. public function mapReduce($map, $reduce, $out = ['inline' => true], array $query = [], array $options = [])
  226. {
  227. $this->log([
  228. 'mapReduce' => true,
  229. 'map' => $map,
  230. 'reduce' => $reduce,
  231. 'out' => $out,
  232. 'query' => $query,
  233. 'options' => $options,
  234. ]);
  235. return parent::mapReduce($map, $reduce, $out, $query, $options);
  236. }
  237. /**
  238. * @see Collection::near()
  239. */
  240. public function near($near, array $query = [], array $options = [])
  241. {
  242. $this->log([
  243. 'geoNear' => true,
  244. 'near' => $near,
  245. 'query' => $query,
  246. 'options' => $options,
  247. ]);
  248. return parent::near($near, $query, $options);
  249. }
  250. /**
  251. * @see Collection::remove()
  252. */
  253. public function remove(array $query, array $options = [])
  254. {
  255. $this->log([
  256. 'remove' => true,
  257. 'query' => $query,
  258. 'options' => $options,
  259. ]);
  260. return parent::remove($query, $options);
  261. }
  262. /**
  263. * @see Collection::save()
  264. */
  265. public function save(array &$a, array $options = [])
  266. {
  267. $this->log([
  268. 'save' => true,
  269. 'document' => $a,
  270. 'options' => $options,
  271. ]);
  272. return parent::save($a, $options);
  273. }
  274. /**
  275. * @see Collection::update()
  276. */
  277. public function update($query, array $newObj, array $options = [])
  278. {
  279. $this->log([
  280. 'update' => true,
  281. 'query' => $query,
  282. 'newObj' => $newObj,
  283. 'options' => $options,
  284. ]);
  285. return parent::update($query, $newObj, $options);
  286. }
  287. /**
  288. * @see Collection::validate()
  289. */
  290. public function validate($scanData = false)
  291. {
  292. $this->log([
  293. 'validate' => true,
  294. 'scanData' => $scanData,
  295. ]);
  296. return parent::validate($scanData);
  297. }
  298. }