/libs/Nette/Diagnostics/BlueScreen.php

https://github.com/premiumcombination/nts · PHP · 130 lines · 74 code · 23 blank · 33 comment · 9 complexity · b5fba7697a5f0a7d9febd264e3780fa8 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (http://nette.org)
  4. *
  5. * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
  6. *
  7. * For the full copyright and license information, please view
  8. * the file license.txt that was distributed with this source code.
  9. */
  10. namespace Nette\Diagnostics;
  11. use Nette;
  12. /**
  13. * Red BlueScreen.
  14. *
  15. * @author David Grudl
  16. * @internal
  17. */
  18. class BlueScreen extends Nette\Object
  19. {
  20. /** @var array */
  21. private $panels = array();
  22. /**
  23. * Add custom panel.
  24. * @param callback
  25. * @param string
  26. * @return void
  27. */
  28. public function addPanel($panel, $id = NULL)
  29. {
  30. if ($id === NULL) {
  31. $this->panels[] = $panel;
  32. } else {
  33. $this->panels[$id] = $panel;
  34. }
  35. }
  36. /**
  37. * Renders blue screen.
  38. * @param \Exception
  39. * @return void
  40. */
  41. public function render(\Exception $exception)
  42. {
  43. $panels = $this->panels;
  44. require __DIR__ . '/templates/bluescreen.phtml';
  45. }
  46. /**
  47. * Returns syntax highlighted source code.
  48. * @param string
  49. * @param int
  50. * @param int
  51. * @return string
  52. */
  53. public static function highlightFile($file, $line, $count = 15, $vars = array())
  54. {
  55. if (function_exists('ini_set')) {
  56. ini_set('highlight.comment', '#998; font-style: italic');
  57. ini_set('highlight.default', '#000');
  58. ini_set('highlight.html', '#06B');
  59. ini_set('highlight.keyword', '#D24; font-weight: bold');
  60. ini_set('highlight.string', '#080');
  61. }
  62. $start = max(1, $line - floor($count / 2));
  63. $source = @file_get_contents($file); // intentionally @
  64. if (!$source) {
  65. return;
  66. }
  67. $source = explode("\n", highlight_string($source, TRUE));
  68. $spans = 1;
  69. $out = $source[0]; // <code><span color=highlight.html>
  70. $source = explode('<br />', $source[1]);
  71. array_unshift($source, NULL);
  72. $i = $start; // find last highlighted block
  73. while (--$i >= 1) {
  74. if (preg_match('#.*(</?span[^>]*>)#', $source[$i], $m)) {
  75. if ($m[1] !== '</span>') {
  76. $spans++; $out .= $m[1];
  77. }
  78. break;
  79. }
  80. }
  81. $source = array_slice($source, $start, $count, TRUE);
  82. end($source);
  83. $numWidth = strlen((string) key($source));
  84. foreach ($source as $n => $s) {
  85. $spans += substr_count($s, '<span') - substr_count($s, '</span');
  86. $s = str_replace(array("\r", "\n"), array('', ''), $s);
  87. preg_match_all('#<[^>]+>#', $s, $tags);
  88. if ($n === $line) {
  89. $out .= sprintf(
  90. "<span class='highlight'>%{$numWidth}s: %s\n</span>%s",
  91. $n,
  92. strip_tags($s),
  93. implode('', $tags[0])
  94. );
  95. } else {
  96. $out .= sprintf("<span class='line'>%{$numWidth}s:</span> %s\n", $n, $s);
  97. }
  98. }
  99. $out .= str_repeat('</span>', $spans) . '</code>';
  100. $out = preg_replace_callback('#">\$(\w+)(&nbsp;)?</span>#', function($m) use ($vars) {
  101. return isset($vars[$m[1]])
  102. ? '" title="' . str_replace('"', '&quot;', strip_tags(Helpers::htmlDump($vars[$m[1]]))) . $m[0]
  103. : $m[0];
  104. }, $out);
  105. return $out;
  106. }
  107. }