LogBuilder.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\mongodb;
  8. use MongoDB\BSON\Binary;
  9. use MongoDB\BSON\Javascript;
  10. use MongoDB\BSON\MaxKey;
  11. use MongoDB\BSON\MinKey;
  12. use MongoDB\BSON\ObjectID;
  13. use MongoDB\BSON\Regex;
  14. use MongoDB\BSON\Timestamp;
  15. use MongoDB\BSON\Type;
  16. use MongoDB\BSON\UTCDatetime;
  17. use yii\base\BaseObject;
  18. /**
  19. * LogBuilder allows composition and escaping of the log entries.
  20. *
  21. * @author Paul Klimov <klimov.paul@gmail.com>
  22. * @since 2.1
  23. */
  24. class LogBuilder extends BaseObject
  25. {
  26. /**
  27. * Generate log/profile token.
  28. * @param string|array $namespace command namespace
  29. * @param array $data command data.
  30. * @return string token.
  31. */
  32. public function generateToken($namespace, $data = [])
  33. {
  34. if (is_array($namespace)) {
  35. $namespace = implode('.', $namespace);
  36. }
  37. return $namespace . '(' . $this->encodeData($data) . ')';
  38. }
  39. /**
  40. * Encodes complex log data into JSON format string.
  41. * @param mixed $data raw data.
  42. * @return string encoded data string.
  43. */
  44. public function encodeData($data)
  45. {
  46. return json_encode($this->processData($data));
  47. }
  48. /**
  49. * Pre-processes the log data before sending it to `json_encode()`.
  50. * @param mixed $data raw data.
  51. * @return mixed the processed data.
  52. */
  53. protected function processData($data)
  54. {
  55. if (is_object($data)) {
  56. if ($data instanceof ObjectID ||
  57. $data instanceof Regex ||
  58. $data instanceof UTCDateTime ||
  59. $data instanceof Timestamp
  60. ) {
  61. $data = get_class($data) . '(' . $data->__toString() . ')';
  62. } elseif ($data instanceof Javascript) {
  63. $data = $this->processJavascript($data);
  64. } elseif ($data instanceof MinKey || $data instanceof MaxKey) {
  65. $data = get_class($data);
  66. } elseif ($data instanceof Binary) {
  67. if (in_array($data->getType(), [Binary::TYPE_MD5, Binary::TYPE_UUID, Binary::TYPE_OLD_UUID], true)) {
  68. $data = $data->getData();
  69. } else {
  70. $data = get_class($data) . '(...)';
  71. }
  72. } elseif ($data instanceof Type) {
  73. // Covers 'Binary', 'DBRef' and others
  74. $data = get_class($data) . '(...)';
  75. } else {
  76. $result = [];
  77. foreach ($data as $name => $value) {
  78. $result[$name] = $value;
  79. }
  80. $data = $result;
  81. }
  82. if ($data === []) {
  83. return new \stdClass();
  84. }
  85. }
  86. if (is_array($data)) {
  87. foreach ($data as $key => $value) {
  88. if (is_array($value) || is_object($value)) {
  89. $data[$key] = $this->processData($value);
  90. }
  91. }
  92. }
  93. return $data;
  94. }
  95. /**
  96. * Processes [[Javascript]] composing recoverable value.
  97. * @param Javascript $javascript javascript BSON object.
  98. * @return string processed javascript.
  99. */
  100. private function processJavascript(Javascript $javascript)
  101. {
  102. $dump = print_r($javascript, true);
  103. $beginPos = strpos($dump, '[javascript] => ');
  104. if ($beginPos === false) {
  105. $beginPos = strpos($dump, '[code] => ');
  106. if ($beginPos === false) {
  107. return $dump;
  108. }
  109. $beginPos += strlen('[code] => ');
  110. } else {
  111. $beginPos += strlen('[javascript] => ');
  112. }
  113. $endPos = strrpos($dump, '[scope] => ');
  114. if ($endPos === false || $beginPos > $endPos) {
  115. return $dump;
  116. }
  117. $content = substr($dump, $beginPos, $endPos - $beginPos);
  118. return get_class($javascript) . '(' . trim($content, " \n\t") . ')';
  119. }
  120. }