PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/yiisoft/yii2/helpers/BaseVarDumper.php

https://gitlab.com/aintenebris/memoria
PHP | 195 lines | 130 code | 9 blank | 56 comment | 10 complexity | 54aac5bd3ae3733f207d3260116bf071 MD5 | raw file
  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\helpers;
  8. /**
  9. * BaseVarDumper provides concrete implementation for [[VarDumper]].
  10. *
  11. * Do not use BaseVarDumper. Use [[VarDumper]] instead.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @since 2.0
  15. */
  16. class BaseVarDumper
  17. {
  18. private static $_objects;
  19. private static $_output;
  20. private static $_depth;
  21. /**
  22. * Displays a variable.
  23. * This method achieves the similar functionality as var_dump and print_r
  24. * but is more robust when handling complex objects such as Yii controllers.
  25. * @param mixed $var variable to be dumped
  26. * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
  27. * @param boolean $highlight whether the result should be syntax-highlighted
  28. */
  29. public static function dump($var, $depth = 10, $highlight = false)
  30. {
  31. echo static::dumpAsString($var, $depth, $highlight);
  32. }
  33. /**
  34. * Dumps a variable in terms of a string.
  35. * This method achieves the similar functionality as var_dump and print_r
  36. * but is more robust when handling complex objects such as Yii controllers.
  37. * @param mixed $var variable to be dumped
  38. * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
  39. * @param boolean $highlight whether the result should be syntax-highlighted
  40. * @return string the string representation of the variable
  41. */
  42. public static function dumpAsString($var, $depth = 10, $highlight = false)
  43. {
  44. self::$_output = '';
  45. self::$_objects = [];
  46. self::$_depth = $depth;
  47. self::dumpInternal($var, 0);
  48. if ($highlight) {
  49. $result = highlight_string("<?php\n" . self::$_output, true);
  50. self::$_output = preg_replace('/&lt;\\?php<br \\/>/', '', $result, 1);
  51. }
  52. return self::$_output;
  53. }
  54. /**
  55. * @param mixed $var variable to be dumped
  56. * @param integer $level depth level
  57. */
  58. private static function dumpInternal($var, $level)
  59. {
  60. switch (gettype($var)) {
  61. case 'boolean':
  62. self::$_output .= $var ? 'true' : 'false';
  63. break;
  64. case 'integer':
  65. self::$_output .= "$var";
  66. break;
  67. case 'double':
  68. self::$_output .= "$var";
  69. break;
  70. case 'string':
  71. self::$_output .= "'" . addslashes($var) . "'";
  72. break;
  73. case 'resource':
  74. self::$_output .= '{resource}';
  75. break;
  76. case 'NULL':
  77. self::$_output .= "null";
  78. break;
  79. case 'unknown type':
  80. self::$_output .= '{unknown}';
  81. break;
  82. case 'array':
  83. if (self::$_depth <= $level) {
  84. self::$_output .= '[...]';
  85. } elseif (empty($var)) {
  86. self::$_output .= '[]';
  87. } else {
  88. $keys = array_keys($var);
  89. $spaces = str_repeat(' ', $level * 4);
  90. self::$_output .= '[';
  91. foreach ($keys as $key) {
  92. self::$_output .= "\n" . $spaces . ' ';
  93. self::dumpInternal($key, 0);
  94. self::$_output .= ' => ';
  95. self::dumpInternal($var[$key], $level + 1);
  96. }
  97. self::$_output .= "\n" . $spaces . ']';
  98. }
  99. break;
  100. case 'object':
  101. if (($id = array_search($var, self::$_objects, true)) !== false) {
  102. self::$_output .= get_class($var) . '#' . ($id + 1) . '(...)';
  103. } elseif (self::$_depth <= $level) {
  104. self::$_output .= get_class($var) . '(...)';
  105. } else {
  106. $id = array_push(self::$_objects, $var);
  107. $className = get_class($var);
  108. $spaces = str_repeat(' ', $level * 4);
  109. self::$_output .= "$className#$id\n" . $spaces . '(';
  110. foreach ((array) $var as $key => $value) {
  111. $keyDisplay = strtr(trim($key), "\0", ':');
  112. self::$_output .= "\n" . $spaces . " [$keyDisplay] => ";
  113. self::dumpInternal($value, $level + 1);
  114. }
  115. self::$_output .= "\n" . $spaces . ')';
  116. }
  117. break;
  118. }
  119. }
  120. /**
  121. * Exports a variable as a string representation.
  122. *
  123. * The string is a valid PHP expression that can be evaluated by PHP parser
  124. * and the evaluation result will give back the variable value.
  125. *
  126. * This method is similar to `var_export()`. The main difference is that
  127. * it generates more compact string representation using short array syntax.
  128. *
  129. * It also handles objects by using the PHP functions serialize() and unserialize().
  130. *
  131. * PHP 5.4 or above is required to parse the exported value.
  132. *
  133. * @param mixed $var the variable to be exported.
  134. * @return string a string representation of the variable
  135. */
  136. public static function export($var)
  137. {
  138. self::$_output = '';
  139. self::exportInternal($var, 0);
  140. return self::$_output;
  141. }
  142. /**
  143. * @param mixed $var variable to be exported
  144. * @param integer $level depth level
  145. */
  146. private static function exportInternal($var, $level)
  147. {
  148. switch (gettype($var)) {
  149. case 'NULL':
  150. self::$_output .= 'null';
  151. break;
  152. case 'array':
  153. if (empty($var)) {
  154. self::$_output .= '[]';
  155. } else {
  156. $keys = array_keys($var);
  157. $outputKeys = ($keys !== range(0, count($var) - 1));
  158. $spaces = str_repeat(' ', $level * 4);
  159. self::$_output .= '[';
  160. foreach ($keys as $key) {
  161. self::$_output .= "\n" . $spaces . ' ';
  162. if ($outputKeys) {
  163. self::exportInternal($key, 0);
  164. self::$_output .= ' => ';
  165. }
  166. self::exportInternal($var[$key], $level + 1);
  167. self::$_output .= ',';
  168. }
  169. self::$_output .= "\n" . $spaces . ']';
  170. }
  171. break;
  172. case 'object':
  173. try {
  174. $output = 'unserialize(' . var_export(serialize($var), true) . ')';
  175. } catch (\Exception $e) {
  176. // serialize may fail, for example: if object contains a `\Closure` instance
  177. // so we use regular `var_export()` as fallback
  178. $output = var_export($var, true);
  179. }
  180. self::$_output .= $output;
  181. break;
  182. default:
  183. self::$_output .= var_export($var, true);
  184. }
  185. }
  186. }