PageRenderTime 31ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/kint/kint/decorators/rich.php

https://gitlab.com/Drulenium-bot/devel
PHP | 319 lines | 231 code | 65 blank | 23 comment | 44 complexity | 716fa65be45f59119abe28db04e529a4 MD5 | raw file
  1. <?php
  2. class Kint_Decorators_Rich
  3. {
  4. # make calls to Kint::dump() from different places in source coloured differently.
  5. private static $_usedColors = array();
  6. public static function decorate( kintVariableData $kintVar )
  7. {
  8. $output = '<dl>';
  9. $extendedPresent = $kintVar->extendedValue !== null || $kintVar->_alternatives !== null;
  10. if ( $extendedPresent ) {
  11. $class = 'kint-parent';
  12. if ( Kint::$expandedByDefault ) {
  13. $class .= ' kint-show';
  14. }
  15. $output .= '<dt class="' . $class . '">';
  16. } else {
  17. $output .= '<dt>';
  18. }
  19. if ( $extendedPresent ) {
  20. $output .= '<span class="kint-popup-trigger" title="Open in new window">&rarr;</span><nav></nav>';
  21. }
  22. $output .= self::_drawHeader( $kintVar ) . $kintVar->value . '</dt>';
  23. if ( $extendedPresent ) {
  24. $output .= '<dd>';
  25. }
  26. if ( isset( $kintVar->extendedValue ) ) {
  27. if ( is_array( $kintVar->extendedValue ) ) {
  28. foreach ( $kintVar->extendedValue as $v ) {
  29. $output .= self::decorate( $v );
  30. }
  31. } elseif ( is_string( $kintVar->extendedValue ) ) {
  32. $output .= '<pre>' . $kintVar->extendedValue . '</pre>';
  33. } else {
  34. $output .= self::decorate( $kintVar->extendedValue ); //it's kint's container
  35. }
  36. } elseif ( isset( $kintVar->_alternatives ) ) {
  37. $output .= "<ul class=\"kint-tabs\">";
  38. foreach ( $kintVar->_alternatives as $k => $var ) {
  39. $active = $k === 0 ? ' class="kint-active-tab"' : '';
  40. $output .= "<li{$active}>" . self::_drawHeader( $var, false ) . '</li>';
  41. }
  42. $output .= "</ul><ul>";
  43. foreach ( $kintVar->_alternatives as $var ) {
  44. $output .= "<li>";
  45. $var = $var->value;
  46. if ( is_array( $var ) ) {
  47. foreach ( $var as $v ) {
  48. $output .= is_string( $v )
  49. ? '<pre>' . $v . '</pre>'
  50. : self::decorate( $v );
  51. }
  52. } elseif ( is_string( $var ) ) {
  53. $output .= '<pre>' . $var . '</pre>';
  54. } elseif ( isset( $var ) ) {
  55. throw new Exception(
  56. 'Kint has encountered an error, '
  57. . 'please paste this report to https://github.com/raveren/kint/issues<br>'
  58. . 'Error encountered at ' . basename( __FILE__ ) . ':' . __LINE__ . '<br>'
  59. . ' variables: '
  60. . htmlspecialchars( var_export( $kintVar->_alternatives, true ), ENT_QUOTES )
  61. );
  62. }
  63. $output .= "</li>";
  64. }
  65. $output .= "</ul>";
  66. }
  67. if ( $extendedPresent ) {
  68. $output .= '</dd>';
  69. }
  70. $output .= '</dl>';
  71. return $output;
  72. }
  73. public static function decorateTrace( $traceData )
  74. {
  75. $output = '<dl class="kint-trace">';
  76. foreach ( $traceData as $i => $step ) {
  77. $class = 'kint-parent';
  78. if ( Kint::$expandedByDefault ) {
  79. $class .= ' kint-show';
  80. }
  81. $output .= '<dt class="' . $class . '">'
  82. . '<b>' . ( $i + 1 ) . '</b> '
  83. . '<nav></nav>'
  84. . '<var>';
  85. if ( isset( $step['file'] ) ) {
  86. $output .= self::_ideLink( $step['file'], $step['line'] );
  87. } else {
  88. $output .= 'PHP internal call';
  89. }
  90. $output .= '</var>';
  91. $output .= $step['function'];
  92. if ( isset( $step['args'] ) ) {
  93. $output .= '(' . implode( ', ', array_keys( $step['args'] ) ) . ')';
  94. }
  95. $output .= '</dt><dd>';
  96. $firstTab = ' class="kint-active-tab"';
  97. $output .= '<ul class="kint-tabs">';
  98. if ( !empty( $step['source'] ) ) {
  99. $output .= "<li{$firstTab}>Source</li>";
  100. $firstTab = '';
  101. }
  102. if ( !empty( $step['args'] ) ) {
  103. $output .= "<li{$firstTab}>Arguments</li>";
  104. $firstTab = '';
  105. }
  106. if ( !empty( $step['object'] ) ) {
  107. kintParser::reset();
  108. $calleeDump = kintParser::factory( $step['object'] );
  109. $output .= "<li{$firstTab}>Callee object [{$calleeDump->type}]</li>";
  110. }
  111. $output .= '</ul><ul>';
  112. if ( !empty( $step['source'] ) ) {
  113. $output .= "<li><pre class=\"kint-source\">{$step['source']}</pre></li>";
  114. }
  115. if ( !empty( $step['args'] ) ) {
  116. $output .= "<li>";
  117. foreach ( $step['args'] as $k => $arg ) {
  118. kintParser::reset();
  119. $output .= self::decorate( kintParser::factory( $arg, $k ) );
  120. }
  121. $output .= "</li>";
  122. }
  123. if ( !empty( $step['object'] ) ) {
  124. $output .= "<li>" . self::decorate( $calleeDump ) . "</li>";
  125. }
  126. $output .= '</ul></dd>';
  127. }
  128. $output .= '</dl>';
  129. return $output;
  130. }
  131. /**
  132. * called for each dump, opens the html tag
  133. *
  134. * @param array $callee caller information taken from debug backtrace
  135. *
  136. * @return string
  137. */
  138. public static function wrapStart()
  139. {
  140. return "<div class=\"kint\">";
  141. }
  142. /**
  143. * closes Kint::_wrapStart() started html tags and displays callee information
  144. *
  145. * @param array $callee caller information taken from debug backtrace
  146. * @param array $miniTrace full path to kint call
  147. * @param array $prevCaller previous caller information taken from debug backtrace
  148. *
  149. * @return string
  150. */
  151. public static function wrapEnd( $callee, $miniTrace, $prevCaller )
  152. {
  153. if ( !Kint::$displayCalledFrom ) {
  154. return '</div>';
  155. }
  156. $callingFunction = '';
  157. $calleeInfo = '';
  158. $traceDisplay = '';
  159. if ( isset( $prevCaller['class'] ) ) {
  160. $callingFunction = $prevCaller['class'];
  161. }
  162. if ( isset( $prevCaller['type'] ) ) {
  163. $callingFunction .= $prevCaller['type'];
  164. }
  165. if ( isset( $prevCaller['function'] )
  166. && !in_array( $prevCaller['function'], array( 'include', 'include_once', 'require', 'require_once' ) )
  167. ) {
  168. $callingFunction .= $prevCaller['function'] . '()';
  169. }
  170. $callingFunction and $callingFunction = " [{$callingFunction}]";
  171. if ( isset( $callee['file'] ) ) {
  172. $calleeInfo .= 'Called from ' . self::_ideLink( $callee['file'], $callee['line'] );
  173. }
  174. if ( !empty( $miniTrace ) ) {
  175. $traceDisplay = '<ol>';
  176. foreach ( $miniTrace as $step ) {
  177. $traceDisplay .= '<li>' . self::_ideLink( $step['file'], $step['line'] ); // closing tag not required
  178. if ( isset( $step['function'] )
  179. && !in_array( $step['function'], array( 'include', 'include_once', 'require', 'require_once' ) )
  180. ) {
  181. $classString = ' [';
  182. if ( isset( $step['class'] ) ) {
  183. $classString .= $step['class'];
  184. }
  185. if ( isset( $step['type'] ) ) {
  186. $classString .= $step['type'];
  187. }
  188. $classString .= $step['function'] . '()]';
  189. $traceDisplay .= $classString;
  190. }
  191. }
  192. $traceDisplay .= '</ol>';
  193. $calleeInfo = '<nav></nav>' . $calleeInfo;
  194. }
  195. return "<footer>"
  196. . '<span class="kint-popup-trigger" title="Open in new window">&rarr;</span> '
  197. . "{$calleeInfo}{$callingFunction}{$traceDisplay}"
  198. . "</footer></div>";
  199. }
  200. private static function _drawHeader( kintVariableData $kintVar, $verbose = true )
  201. {
  202. $output = '';
  203. if ( $verbose ) {
  204. if ( $kintVar->access !== null ) {
  205. $output .= "<var>" . $kintVar->access . "</var> ";
  206. }
  207. if ( $kintVar->name !== null && $kintVar->name !== '' ) {
  208. $output .= "<dfn>" . kintParser::escape( $kintVar->name ) . "</dfn> ";
  209. }
  210. if ( $kintVar->operator !== null ) {
  211. $output .= $kintVar->operator . " ";
  212. }
  213. }
  214. if ( $kintVar->type !== null ) {
  215. if ( $verbose ) {
  216. $output .= "<var>";
  217. }
  218. $output .= $kintVar->type;
  219. if ( $verbose ) {
  220. $output .= "</var>";
  221. } else {
  222. $output .= " ";
  223. }
  224. }
  225. if ( $kintVar->size !== null ) {
  226. $output .= "(" . $kintVar->size . ") ";
  227. }
  228. return $output;
  229. }
  230. private static function _ideLink( $file, $line )
  231. {
  232. $shortenedPath = Kint::shortenPath( $file );
  233. if ( !Kint::$fileLinkFormat ) return $shortenedPath . ':' . $line;
  234. $ideLink = Kint::getIdeLink( $file, $line );
  235. $class = ( strpos( $ideLink, 'http://' ) === 0 ) ? 'class="kint-ide-link" ' : '';
  236. return "<a {$class}href=\"{$ideLink}\">{$shortenedPath}:{$line}</a>";
  237. }
  238. /**
  239. * produces css and js required for display. May be called multiple times, will only produce output once per
  240. * pageload or until `-` or `@` modifier is used
  241. *
  242. * @return string
  243. */
  244. public static function init()
  245. {
  246. $baseDir = KINT_DIR . 'view/compiled/';
  247. if ( !is_readable( $cssFile = $baseDir . Kint::$theme . '.css' ) ) {
  248. $cssFile = $baseDir . 'original.css';
  249. }
  250. return
  251. '<script class="-kint-js">' . file_get_contents( $baseDir . 'kint.js' ) . '</script>'
  252. . '<style class="-kint-css">' . file_get_contents( $cssFile ) . "</style>\n";
  253. }
  254. }