PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/SafeForKids/vendor/symfony/var-dumper/Caster/ExceptionCaster.php

https://gitlab.com/rocs/Streaming-Safe-for-Kids
PHP | 248 lines | 203 code | 32 blank | 13 comment | 41 complexity | 5d18fb03d8c0e99d27b65f6d09bebe6f 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\VarDumper\Caster;
  11. use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
  12. use Symfony\Component\VarDumper\Cloner\Stub;
  13. /**
  14. * Casts common Exception classes to array representation.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. class ExceptionCaster
  19. {
  20. public static $srcContext = 1;
  21. public static $traceArgs = true;
  22. public static $errorTypes = array(
  23. E_DEPRECATED => 'E_DEPRECATED',
  24. E_USER_DEPRECATED => 'E_USER_DEPRECATED',
  25. E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
  26. E_ERROR => 'E_ERROR',
  27. E_WARNING => 'E_WARNING',
  28. E_PARSE => 'E_PARSE',
  29. E_NOTICE => 'E_NOTICE',
  30. E_CORE_ERROR => 'E_CORE_ERROR',
  31. E_CORE_WARNING => 'E_CORE_WARNING',
  32. E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  33. E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  34. E_USER_ERROR => 'E_USER_ERROR',
  35. E_USER_WARNING => 'E_USER_WARNING',
  36. E_USER_NOTICE => 'E_USER_NOTICE',
  37. E_STRICT => 'E_STRICT',
  38. );
  39. public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0)
  40. {
  41. return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter);
  42. }
  43. public static function castException(\Exception $e, array $a, Stub $stub, $isNested, $filter = 0)
  44. {
  45. return self::filterExceptionArray($stub->class, $a, "\0Exception\0", $filter);
  46. }
  47. public static function castErrorException(\ErrorException $e, array $a, Stub $stub, $isNested)
  48. {
  49. if (isset($a[$s = Caster::PREFIX_PROTECTED.'severity'], self::$errorTypes[$a[$s]])) {
  50. $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
  51. }
  52. return $a;
  53. }
  54. public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
  55. {
  56. $prefix = Caster::PREFIX_PROTECTED;
  57. $xPrefix = "\0Exception\0";
  58. if (isset($a[$xPrefix.'previous'], $a[$xPrefix.'trace'])) {
  59. $b = (array) $a[$xPrefix.'previous'];
  60. array_unshift($b[$xPrefix.'trace'], array(
  61. 'function' => 'new '.get_class($a[$xPrefix.'previous']),
  62. 'file' => $b[$prefix.'file'],
  63. 'line' => $b[$prefix.'line'],
  64. ));
  65. $a[$xPrefix.'trace'] = new TraceStub($b[$xPrefix.'trace'], false, 0, -1 - count($a[$xPrefix.'trace']->value));
  66. }
  67. unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
  68. return $a;
  69. }
  70. public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $isNested)
  71. {
  72. if (!$isNested) {
  73. return $a;
  74. }
  75. $stub->class = '';
  76. $stub->handle = 0;
  77. $frames = $trace->value;
  78. $a = array();
  79. $j = count($frames);
  80. if (0 > $i = $trace->sliceOffset) {
  81. $i = max(0, $j + $i);
  82. }
  83. if (!isset($trace->value[$i])) {
  84. return array();
  85. }
  86. $lastCall = isset($frames[$i]['function']) ? ' ==> '.(isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
  87. for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
  88. $call = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[$i]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '???';
  89. $a[Caster::PREFIX_VIRTUAL.$j.'. '.$call.$lastCall] = new FrameStub(
  90. array(
  91. 'object' => isset($frames[$i]['object']) ? $frames[$i]['object'] : null,
  92. 'class' => isset($frames[$i]['class']) ? $frames[$i]['class'] : null,
  93. 'type' => isset($frames[$i]['type']) ? $frames[$i]['type'] : null,
  94. 'function' => isset($frames[$i]['function']) ? $frames[$i]['function'] : null,
  95. ) + $frames[$i - 1],
  96. $trace->keepArgs,
  97. true
  98. );
  99. $lastCall = ' ==> '.$call;
  100. }
  101. $a[Caster::PREFIX_VIRTUAL.$j.'. {main}'.$lastCall] = new FrameStub(
  102. array(
  103. 'object' => null,
  104. 'class' => null,
  105. 'type' => null,
  106. 'function' => '{main}',
  107. ) + $frames[$i - 1],
  108. $trace->keepArgs,
  109. true
  110. );
  111. if (null !== $trace->sliceLength) {
  112. $a = array_slice($a, 0, $trace->sliceLength, true);
  113. }
  114. return $a;
  115. }
  116. public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $isNested)
  117. {
  118. if (!$isNested) {
  119. return $a;
  120. }
  121. $f = $frame->value;
  122. $prefix = Caster::PREFIX_VIRTUAL;
  123. if (isset($f['file'], $f['line'])) {
  124. if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
  125. $f['file'] = substr($f['file'], 0, -strlen($match[0]));
  126. $f['line'] = (int) $match[1];
  127. }
  128. if (file_exists($f['file']) && 0 <= self::$srcContext) {
  129. $src[$f['file'].':'.$f['line']] = self::extractSource(explode("\n", file_get_contents($f['file'])), $f['line'], self::$srcContext);
  130. if (!empty($f['class']) && is_subclass_of($f['class'], 'Twig_Template') && method_exists($f['class'], 'getDebugInfo')) {
  131. $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));
  132. $templateName = $template->getTemplateName();
  133. $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
  134. $templateInfo = $template->getDebugInfo();
  135. if (isset($templateInfo[$f['line']])) {
  136. if (method_exists($template, 'getSourceContext')) {
  137. $templateName = $template->getSourceContext()->getPath() ?: $templateName;
  138. }
  139. if ($templateSrc) {
  140. $templateSrc = explode("\n", $templateSrc);
  141. $src[$templateName.':'.$templateInfo[$f['line']]] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
  142. } else {
  143. $src[$templateName] = $templateInfo[$f['line']];
  144. }
  145. }
  146. }
  147. } else {
  148. $src[$f['file']] = $f['line'];
  149. }
  150. $a[$prefix.'src'] = new EnumStub($src);
  151. }
  152. unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
  153. if ($frame->inTraceStub) {
  154. unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']);
  155. }
  156. foreach ($a as $k => $v) {
  157. if (!$v) {
  158. unset($a[$k]);
  159. }
  160. }
  161. if ($frame->keepArgs && isset($f['args'])) {
  162. $a[$prefix.'args'] = $f['args'];
  163. }
  164. return $a;
  165. }
  166. private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter)
  167. {
  168. if (isset($a[$xPrefix.'trace'])) {
  169. $trace = $a[$xPrefix.'trace'];
  170. unset($a[$xPrefix.'trace']); // Ensures the trace is always last
  171. } else {
  172. $trace = array();
  173. }
  174. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  175. array_unshift($trace, array(
  176. 'function' => $xClass ? 'new '.$xClass : null,
  177. 'file' => $a[Caster::PREFIX_PROTECTED.'file'],
  178. 'line' => $a[Caster::PREFIX_PROTECTED.'line'],
  179. ));
  180. $a[$xPrefix.'trace'] = new TraceStub($trace, self::$traceArgs);
  181. }
  182. if (empty($a[$xPrefix.'previous'])) {
  183. unset($a[$xPrefix.'previous']);
  184. }
  185. unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
  186. return $a;
  187. }
  188. private static function extractSource(array $srcArray, $line, $srcContext)
  189. {
  190. $src = array();
  191. for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
  192. $src[] = (isset($srcArray[$i]) ? $srcArray[$i] : '')."\n";
  193. }
  194. $ltrim = 0;
  195. do {
  196. $pad = null;
  197. for ($i = $srcContext << 1; $i >= 0; --$i) {
  198. if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {
  199. if (null === $pad) {
  200. $pad = $c;
  201. }
  202. if ((' ' !== $c && "\t" !== $c) || $pad !== $c) {
  203. break;
  204. }
  205. }
  206. }
  207. ++$ltrim;
  208. } while (0 > $i && null !== $pad);
  209. if (--$ltrim) {
  210. foreach ($src as $i => $line) {
  211. $src[$i] = isset($line[$ltrim]) && "\r" !== $line[$ltrim] ? substr($line, $ltrim) : ltrim($line, " \t");
  212. }
  213. }
  214. return implode('', $src);
  215. }
  216. }