PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

https://gitlab.com/pthapa81/MeroSaaman-1.0
PHP | 284 lines | 234 code | 38 blank | 12 comment | 32 complexity | 6a1404dcff8fc6e68f176a2bb36688d1 MD5 | raw file
  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\HttpKernel\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. use Symfony\Component\VarDumper\Cloner\Data;
  16. use Symfony\Component\VarDumper\Cloner\VarCloner;
  17. use Symfony\Component\VarDumper\Dumper\CliDumper;
  18. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  19. use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
  20. /**
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. class DumpDataCollector extends DataCollector implements DataDumperInterface
  24. {
  25. private $stopwatch;
  26. private $fileLinkFormat;
  27. private $dataCount = 0;
  28. private $isCollected = true;
  29. private $clonesCount = 0;
  30. private $clonesIndex = 0;
  31. private $rootRefs;
  32. private $charset;
  33. private $dumper;
  34. public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, $charset = null, RequestStack $requestStack = null)
  35. {
  36. $this->stopwatch = $stopwatch;
  37. $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
  38. $this->charset = $charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8';
  39. $this->requestStack = $requestStack;
  40. // All clones share these properties by reference:
  41. $this->rootRefs = array(
  42. &$this->data,
  43. &$this->dataCount,
  44. &$this->isCollected,
  45. &$this->clonesCount,
  46. );
  47. }
  48. public function __clone()
  49. {
  50. $this->clonesIndex = ++$this->clonesCount;
  51. }
  52. public function dump(Data $data)
  53. {
  54. if ($this->stopwatch) {
  55. $this->stopwatch->start('dump');
  56. }
  57. if ($this->isCollected) {
  58. $this->isCollected = false;
  59. }
  60. $trace = PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS : true;
  61. if (PHP_VERSION_ID >= 50400) {
  62. $trace = debug_backtrace($trace, 7);
  63. } else {
  64. $trace = debug_backtrace($trace);
  65. }
  66. $file = $trace[0]['file'];
  67. $line = $trace[0]['line'];
  68. $name = false;
  69. $fileExcerpt = false;
  70. for ($i = 1; $i < 7; ++$i) {
  71. if (isset($trace[$i]['class'], $trace[$i]['function'])
  72. && 'dump' === $trace[$i]['function']
  73. && 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
  74. ) {
  75. $file = $trace[$i]['file'];
  76. $line = $trace[$i]['line'];
  77. while (++$i < 7) {
  78. if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
  79. $file = $trace[$i]['file'];
  80. $line = $trace[$i]['line'];
  81. break;
  82. } elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof \Twig_Template) {
  83. $info = $trace[$i]['object'];
  84. $name = $info->getTemplateName();
  85. $src = $info->getEnvironment()->getLoader()->getSource($name);
  86. $info = $info->getDebugInfo();
  87. if (isset($info[$trace[$i-1]['line']])) {
  88. $file = false;
  89. $line = $info[$trace[$i-1]['line']];
  90. $src = explode("\n", $src);
  91. $fileExcerpt = array();
  92. for ($i = max($line - 3, 1), $max = min($line + 3, count($src)); $i <= $max; ++$i) {
  93. $fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>';
  94. }
  95. $fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>';
  96. }
  97. break;
  98. }
  99. }
  100. break;
  101. }
  102. }
  103. if (false === $name) {
  104. $name = strtr($file, '\\', '/');
  105. $name = substr($name, strrpos($name, '/') + 1);
  106. }
  107. if ($this->dumper) {
  108. $this->doDump($data, $name, $file, $line);
  109. } else {
  110. $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
  111. ++$this->dataCount;
  112. }
  113. if ($this->stopwatch) {
  114. $this->stopwatch->stop('dump');
  115. }
  116. }
  117. public function collect(Request $request, Response $response, \Exception $exception = null)
  118. {
  119. if ($this->requestStack && $this->requestStack->getMasterRequest() !== $request) {
  120. return;
  121. }
  122. // In all conditions that remove the web debug toolbar, dumps are written on the output.
  123. if (!$this->requestStack
  124. || $request->isXmlHttpRequest()
  125. || !$response->headers->has('X-Debug-Token')
  126. || $response->isRedirection()
  127. || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
  128. || 'html' !== $request->getRequestFormat()
  129. || false === strripos($response->getContent(), '</body>')
  130. ) {
  131. if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
  132. $this->dumper = new HtmlDumper('php://output', $this->charset);
  133. } else {
  134. $this->dumper = new CliDumper('php://output', $this->charset);
  135. }
  136. foreach ($this->data as $i => $dump) {
  137. $this->data[$i] = null;
  138. $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
  139. }
  140. $this->data = array();
  141. $this->dataCount = 0;
  142. }
  143. }
  144. public function serialize()
  145. {
  146. if ($this->clonesCount !== $this->clonesIndex) {
  147. return 'a:0:{}';
  148. }
  149. $ser = serialize($this->data);
  150. $this->data = array();
  151. $this->dataCount = 0;
  152. $this->isCollected = true;
  153. $this->dumper = null;
  154. return $ser;
  155. }
  156. public function unserialize($data)
  157. {
  158. parent::unserialize($data);
  159. $this->dataCount = count($this->data);
  160. self::__construct($this->stopwatch);
  161. }
  162. public function getDumpsCount()
  163. {
  164. return $this->dataCount;
  165. }
  166. public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
  167. {
  168. $data = fopen('php://memory', 'r+b');
  169. if ('html' === $format) {
  170. $dumper = new HtmlDumper($data, $this->charset);
  171. } else {
  172. throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
  173. }
  174. $dumps = array();
  175. foreach ($this->data as $dump) {
  176. $dumper->dump($dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth));
  177. rewind($data);
  178. $dump['data'] = stream_get_contents($data);
  179. ftruncate($data, 0);
  180. rewind($data);
  181. $dumps[] = $dump;
  182. }
  183. return $dumps;
  184. }
  185. public function getName()
  186. {
  187. return 'dump';
  188. }
  189. public function __destruct()
  190. {
  191. if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) {
  192. $this->clonesCount = 0;
  193. $this->isCollected = true;
  194. $h = headers_list();
  195. $i = count($h);
  196. array_unshift($h, 'Content-Type: '.ini_get('default_mimetype'));
  197. while (0 !== stripos($h[$i], 'Content-Type:')) {
  198. --$i;
  199. }
  200. if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) {
  201. $this->dumper = new HtmlDumper('php://output', $this->charset);
  202. } else {
  203. $this->dumper = new CliDumper('php://output', $this->charset);
  204. }
  205. foreach ($this->data as $i => $dump) {
  206. $this->data[$i] = null;
  207. $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
  208. }
  209. $this->data = array();
  210. $this->dataCount = 0;
  211. }
  212. }
  213. private function doDump($data, $name, $file, $line)
  214. {
  215. if ($this->dumper instanceof HtmlDumper) {
  216. $name = $this->htmlEncode($name);
  217. $file = $this->htmlEncode($file);
  218. if ('' !== $file) {
  219. if ($this->fileLinkFormat) {
  220. $link = strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
  221. $name = sprintf('<a href="%s" title="%s">%s</a>', $link, $file, $name);
  222. } else {
  223. $name = sprintf('<abbr title="%s">%s</abbr>', $file, $name);
  224. }
  225. }
  226. echo "\n<span class=\"sf-dump-meta\">{$name} on line {$line}:</span>";
  227. } else {
  228. echo "{$name} on line {$line}:\n";
  229. }
  230. $this->dumper->dump($data);
  231. }
  232. private function htmlEncode($s)
  233. {
  234. $html = '';
  235. $dumper = new HtmlDumper(function ($line) use (&$html) {$html .= $line;}, $this->charset);
  236. $dumper->setDumpHeader('');
  237. $dumper->setDumpBoundaries('', '');
  238. $cloner = new VarCloner();
  239. $dumper->dump($cloner->cloneVar($s));
  240. return substr(strip_tags($html), 1, -1);
  241. }
  242. }