mongo.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. namespace DB;
  16. //! MongoDB wrapper
  17. class Mongo {
  18. //@{
  19. const
  20. E_Profiler='MongoDB profiler is disabled';
  21. //@}
  22. protected
  23. //! UUID
  24. $uuid,
  25. //! Data source name
  26. $dsn,
  27. //! MongoDB object
  28. $db,
  29. //! Legacy flag
  30. $legacy,
  31. //! MongoDB log
  32. $log;
  33. /**
  34. * Return data source name
  35. * @return string
  36. **/
  37. function dsn() {
  38. return $this->dsn;
  39. }
  40. /**
  41. * Return UUID
  42. * @return string
  43. **/
  44. function uuid() {
  45. return $this->uuid;
  46. }
  47. /**
  48. * Return MongoDB profiler results (or disable logging)
  49. * @param $flag bool
  50. * @return string
  51. **/
  52. function log($flag=TRUE) {
  53. if ($flag) {
  54. $cursor=$this->db->selectcollection('system.profile')->find();
  55. foreach (iterator_to_array($cursor) as $frame)
  56. if (!preg_match('/\.system\..+$/',$frame['ns']))
  57. $this->log.=date('r',$this->legacy() ?
  58. $frame['ts']->sec : (round((string)$frame['ts'])/1000)).
  59. ' ('.sprintf('%.1f',$frame['millis']).'ms) '.
  60. $frame['ns'].' ['.$frame['op'].'] '.
  61. (empty($frame['query'])?
  62. '':json_encode($frame['query'])).
  63. (empty($frame['command'])?
  64. '':json_encode($frame['command'])).
  65. PHP_EOL;
  66. } else {
  67. $this->log=FALSE;
  68. if ($this->legacy)
  69. $this->db->setprofilinglevel(-1);
  70. else
  71. $this->db->command(['profile'=>-1]);
  72. }
  73. return $this->log;
  74. }
  75. /**
  76. * Intercept native call to re-enable profiler
  77. * @return int
  78. **/
  79. function drop() {
  80. $out=$this->db->drop();
  81. if ($this->log!==FALSE) {
  82. if ($this->legacy)
  83. $this->db->setprofilinglevel(2);
  84. else
  85. $this->db->command(['profile'=>2]);
  86. }
  87. return $out;
  88. }
  89. /**
  90. * Redirect call to MongoDB object
  91. * @return mixed
  92. * @param $func string
  93. * @param $args array
  94. **/
  95. function __call($func,array $args) {
  96. return call_user_func_array([$this->db,$func],$args);
  97. }
  98. /**
  99. * Return TRUE if legacy driver is loaded
  100. * @return bool
  101. **/
  102. function legacy() {
  103. return $this->legacy;
  104. }
  105. //! Prohibit cloning
  106. private function __clone() {
  107. }
  108. /**
  109. * Instantiate class
  110. * @param $dsn string
  111. * @param $dbname string
  112. * @param $options array
  113. **/
  114. function __construct($dsn,$dbname,array $options=NULL) {
  115. $this->uuid=\Base::instance()->hash($this->dsn=$dsn);
  116. if ($this->legacy=class_exists('\MongoClient')) {
  117. $this->db=new \MongoDB(new \MongoClient($dsn,$options?:[]),$dbname);
  118. $this->db->setprofilinglevel(2);
  119. }
  120. else {
  121. $this->db=(new \MongoDB\Client($dsn,$options?:[]))->$dbname;
  122. $this->db->command(['profile'=>2]);
  123. }
  124. }
  125. }