No Description

CliDumper.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Dumper;
  11. use Symfony\Component\VarDumper\Cloner\Cursor;
  12. /**
  13. * CliDumper dumps variables for command line output.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class CliDumper extends AbstractDumper
  18. {
  19. public static $defaultColors;
  20. public static $defaultOutput = 'php://stdout';
  21. protected $colors;
  22. protected $maxStringWidth = 0;
  23. protected $styles = array(
  24. // See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
  25. 'default' => '38;5;208',
  26. 'num' => '1;38;5;38',
  27. 'const' => '1;38;5;208',
  28. 'str' => '1;38;5;113',
  29. 'cchr' => '7',
  30. 'note' => '38;5;38',
  31. 'ref' => '38;5;247',
  32. 'public' => '',
  33. 'protected' => '',
  34. 'private' => '',
  35. 'meta' => '38;5;170',
  36. 'key' => '38;5;113',
  37. 'index' => '38;5;38',
  38. );
  39. protected static $controlCharsRx = '/[\x00-\x1F\x7F]/';
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function __construct($output = null, $charset = null)
  44. {
  45. parent::__construct($output, $charset);
  46. if ('\\' === DIRECTORY_SEPARATOR && false !== @getenv('ANSICON')) {
  47. // Use only the base 16 xterm colors when using ANSICON
  48. $this->setStyles(array(
  49. 'default' => '31',
  50. 'num' => '1;34',
  51. 'const' => '1;31',
  52. 'str' => '1;32',
  53. 'note' => '34',
  54. 'ref' => '1;30',
  55. 'meta' => '35',
  56. 'key' => '32',
  57. 'index' => '34',
  58. ));
  59. }
  60. }
  61. /**
  62. * Enables/disables colored output.
  63. *
  64. * @param bool $colors
  65. */
  66. public function setColors($colors)
  67. {
  68. $this->colors = (bool) $colors;
  69. }
  70. /**
  71. * Sets the maximum number of characters per line for dumped strings.
  72. *
  73. * @param int $maxStringWidth
  74. */
  75. public function setMaxStringWidth($maxStringWidth)
  76. {
  77. if (function_exists('iconv')) {
  78. $this->maxStringWidth = (int) $maxStringWidth;
  79. }
  80. }
  81. /**
  82. * Configures styles.
  83. *
  84. * @param array $styles A map of style names to style definitions.
  85. */
  86. public function setStyles(array $styles)
  87. {
  88. $this->styles = $styles + $this->styles;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function dumpScalar(Cursor $cursor, $type, $value)
  94. {
  95. $this->dumpKey($cursor);
  96. $style = 'const';
  97. $attr = array();
  98. switch ($type) {
  99. case 'integer':
  100. $style = 'num';
  101. break;
  102. case 'double':
  103. $style = 'num';
  104. switch (true) {
  105. case INF === $value: $value = 'INF'; break;
  106. case -INF === $value: $value = '-INF'; break;
  107. case is_nan($value): $value = 'NAN'; break;
  108. default:
  109. $value = (string) $value;
  110. if (false === strpos($value, $this->decimalPoint)) {
  111. $value .= $this->decimalPoint.'0';
  112. }
  113. break;
  114. }
  115. break;
  116. case 'NULL':
  117. $value = 'null';
  118. break;
  119. case 'boolean':
  120. $value = $value ? 'true' : 'false';
  121. break;
  122. default:
  123. $attr['value'] = isset($value[0]) && !preg_match('//u', $value) ? $this->utf8Encode($value) : $value;
  124. $value = isset($type[0]) && !preg_match('//u', $type) ? $this->utf8Encode($type) : $type;
  125. break;
  126. }
  127. $this->line .= $this->style($style, $value, $attr);
  128. $this->dumpLine($cursor->depth);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function dumpString(Cursor $cursor, $str, $bin, $cut)
  134. {
  135. $this->dumpKey($cursor);
  136. if ($bin) {
  137. $str = $this->utf8Encode($str);
  138. }
  139. if ('' === $str) {
  140. $this->line .= '""';
  141. $this->dumpLine($cursor->depth);
  142. } else {
  143. $attr = array(
  144. 'length' => function_exists('iconv_strlen') && 0 <= $cut ? iconv_strlen($str, 'UTF-8') + $cut : 0,
  145. 'binary' => $bin,
  146. );
  147. $str = explode("\n", $str);
  148. $m = count($str) - 1;
  149. $i = $lineCut = 0;
  150. if ($bin) {
  151. $this->line .= 'b';
  152. }
  153. if ($m) {
  154. $this->line .= '"""';
  155. $this->dumpLine($cursor->depth);
  156. } else {
  157. $this->line .= '"';
  158. }
  159. foreach ($str as $str) {
  160. if (0 < $this->maxStringWidth && $this->maxStringWidth < $len = iconv_strlen($str, 'UTF-8')) {
  161. $str = iconv_substr($str, 0, $this->maxStringWidth, 'UTF-8');
  162. $lineCut = $len - $this->maxStringWidth;
  163. }
  164. if ($m) {
  165. $this->line .= $this->indentPad;
  166. }
  167. $this->line .= $this->style('str', $str, $attr);
  168. if ($i++ == $m) {
  169. $this->line .= '"';
  170. if ($m) {
  171. $this->line .= '""';
  172. }
  173. if ($cut < 0) {
  174. $this->line .= '…';
  175. $lineCut = 0;
  176. } elseif ($cut) {
  177. $lineCut += $cut;
  178. }
  179. }
  180. if ($lineCut) {
  181. $this->line .= '…'.$lineCut;
  182. $lineCut = 0;
  183. }
  184. $this->dumpLine($cursor->depth);
  185. }
  186. }
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function enterHash(Cursor $cursor, $type, $class, $hasChild)
  192. {
  193. $this->dumpKey($cursor);
  194. if (!preg_match('//u', $class)) {
  195. $class = $this->utf8Encode($class);
  196. }
  197. if (Cursor::HASH_OBJECT === $type) {
  198. $prefix = 'stdClass' !== $class ? $this->style('note', $class).' {' : '{';
  199. } elseif (Cursor::HASH_RESOURCE === $type) {
  200. $prefix = $this->style('note', ':'.$class).' {';
  201. } else {
  202. $prefix = $class ? $this->style('note', 'array:'.$class).' [' : '[';
  203. }
  204. if ($cursor->softRefCount || 0 < $cursor->softRefHandle) {
  205. $prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), array('count' => $cursor->softRefCount));
  206. } elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) {
  207. $prefix .= $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount));
  208. }
  209. $this->line .= $prefix;
  210. if ($hasChild) {
  211. $this->dumpLine($cursor->depth);
  212. }
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
  218. {
  219. $this->dumpEllipsis($cursor, $hasChild, $cut);
  220. $this->line .= Cursor::HASH_OBJECT === $type || Cursor::HASH_RESOURCE === $type ? '}' : ']';
  221. $this->dumpLine($cursor->depth);
  222. }
  223. /**
  224. * Dumps an ellipsis for cut children.
  225. *
  226. * @param Cursor $cursor The Cursor position in the dump.
  227. * @param bool $hasChild When the dump of the hash has child item.
  228. * @param int $cut The number of items the hash has been cut by.
  229. */
  230. protected function dumpEllipsis(Cursor $cursor, $hasChild, $cut)
  231. {
  232. if ($cut) {
  233. $this->line .= ' …';
  234. if (0 < $cut) {
  235. $this->line .= $cut;
  236. }
  237. if ($hasChild) {
  238. $this->dumpLine($cursor->depth + 1);
  239. }
  240. }
  241. }
  242. /**
  243. * Dumps a key in a hash structure.
  244. *
  245. * @param Cursor $cursor The Cursor position in the dump.
  246. */
  247. protected function dumpKey(Cursor $cursor)
  248. {
  249. if (null !== $key = $cursor->hashKey) {
  250. if ($cursor->hashKeyIsBinary) {
  251. $key = $this->utf8Encode($key);
  252. }
  253. $attr = array('binary' => $cursor->hashKeyIsBinary);
  254. $bin = $cursor->hashKeyIsBinary ? 'b' : '';
  255. $style = 'key';
  256. switch ($cursor->hashType) {
  257. default:
  258. case Cursor::HASH_INDEXED:
  259. $style = 'index';
  260. case Cursor::HASH_ASSOC:
  261. if (is_int($key)) {
  262. $this->line .= $this->style($style, $key).' => ';
  263. } else {
  264. $this->line .= $bin.'"'.$this->style($style, $key).'" => ';
  265. }
  266. break;
  267. case Cursor::HASH_RESOURCE:
  268. $key = "\0~\0".$key;
  269. // No break;
  270. case Cursor::HASH_OBJECT:
  271. if (!isset($key[0]) || "\0" !== $key[0]) {
  272. $this->line .= '+'.$bin.$this->style('public', $key).': ';
  273. } elseif (0 < strpos($key, "\0", 1)) {
  274. $key = explode("\0", substr($key, 1), 2);
  275. switch ($key[0]) {
  276. case '+': // User inserted keys
  277. $attr['dynamic'] = true;
  278. $this->line .= '+'.$bin.'"'.$this->style('public', $key[1], $attr).'": ';
  279. break 2;
  280. case '~':
  281. $style = 'meta';
  282. break;
  283. case '*':
  284. $style = 'protected';
  285. $bin = '#'.$bin;
  286. break;
  287. default:
  288. $attr['class'] = $key[0];
  289. $style = 'private';
  290. $bin = '-'.$bin;
  291. break;
  292. }
  293. $this->line .= $bin.$this->style($style, $key[1], $attr).': ';
  294. } else {
  295. // This case should not happen
  296. $this->line .= '-'.$bin.'"'.$this->style('private', $key, array('class' => '')).'": ';
  297. }
  298. break;
  299. }
  300. if ($cursor->hardRefTo) {
  301. $this->line .= $this->style('ref', '&'.($cursor->hardRefCount ? $cursor->hardRefTo : ''), array('count' => $cursor->hardRefCount)).' ';
  302. }
  303. }
  304. }
  305. /**
  306. * Decorates a value with some style.
  307. *
  308. * @param string $style The type of style being applied.
  309. * @param string $value The value being styled.
  310. * @param array $attr Optional context information.
  311. *
  312. * @return string The value with style decoration.
  313. */
  314. protected function style($style, $value, $attr = array())
  315. {
  316. if (null === $this->colors) {
  317. $this->colors = $this->supportsColors();
  318. }
  319. $style = $this->styles[$style];
  320. $cchr = $this->colors ? "\033[m\033[{$style};{$this->styles['cchr']}m%s\033[m\033[{$style}m" : '%s';
  321. $value = preg_replace_callback(self::$controlCharsRx, function ($r) use ($cchr) {
  322. return sprintf($cchr, "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0])));
  323. }, $value);
  324. return $this->colors ? sprintf("\033[%sm%s\033[m\033[%sm", $style, $value, $this->styles['default']) : $value;
  325. }
  326. /**
  327. * @return bool Tells if the current output stream supports ANSI colors or not.
  328. */
  329. protected function supportsColors()
  330. {
  331. if ($this->outputStream !== static::$defaultOutput) {
  332. return @(is_resource($this->outputStream) && function_exists('posix_isatty') && posix_isatty($this->outputStream));
  333. }
  334. if (null !== static::$defaultColors) {
  335. return static::$defaultColors;
  336. }
  337. if (isset($_SERVER['argv'][1])) {
  338. $colors = $_SERVER['argv'];
  339. $i = count($colors);
  340. while (--$i > 0) {
  341. if (isset($colors[$i][5])) {
  342. switch ($colors[$i]) {
  343. case '--ansi':
  344. case '--color':
  345. case '--color=yes':
  346. case '--color=force':
  347. case '--color=always':
  348. return static::$defaultColors = true;
  349. case '--no-ansi':
  350. case '--color=no':
  351. case '--color=none':
  352. case '--color=never':
  353. return static::$defaultColors = false;
  354. }
  355. }
  356. }
  357. }
  358. if ('\\' === DIRECTORY_SEPARATOR) {
  359. static::$defaultColors = @(false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI'));
  360. } elseif (function_exists('posix_isatty')) {
  361. $h = stream_get_meta_data($this->outputStream) + array('wrapper_type' => null);
  362. $h = 'Output' === $h['stream_type'] && 'PHP' === $h['wrapper_type'] ? fopen('php://stdout', 'wb') : $this->outputStream;
  363. static::$defaultColors = @posix_isatty($h);
  364. } else {
  365. static::$defaultColors = false;
  366. }
  367. return static::$defaultColors;
  368. }
  369. /**
  370. * {@inheritdoc}
  371. */
  372. protected function dumpLine($depth)
  373. {
  374. if ($this->colors) {
  375. $this->line = sprintf("\033[%sm%s\033[m", $this->styles['default'], $this->line);
  376. }
  377. parent::dumpLine($depth);
  378. }
  379. }