[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <[email protected]> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\VarDumper\Caster; 13 14 use Symfony\Component\ErrorHandler\Exception\SilencedErrorContext; 15 use Symfony\Component\VarDumper\Cloner\Stub; 16 use Symfony\Component\VarDumper\Exception\ThrowingCasterException; 17 18 /** 19 * Casts common Exception classes to array representation. 20 * 21 * @author Nicolas Grekas <[email protected]> 22 * 23 * @final 24 */ 25 class ExceptionCaster 26 { 27 public static $srcContext = 1; 28 public static $traceArgs = true; 29 public static $errorTypes = [ 30 \E_DEPRECATED => 'E_DEPRECATED', 31 \E_USER_DEPRECATED => 'E_USER_DEPRECATED', 32 \E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', 33 \E_ERROR => 'E_ERROR', 34 \E_WARNING => 'E_WARNING', 35 \E_PARSE => 'E_PARSE', 36 \E_NOTICE => 'E_NOTICE', 37 \E_CORE_ERROR => 'E_CORE_ERROR', 38 \E_CORE_WARNING => 'E_CORE_WARNING', 39 \E_COMPILE_ERROR => 'E_COMPILE_ERROR', 40 \E_COMPILE_WARNING => 'E_COMPILE_WARNING', 41 \E_USER_ERROR => 'E_USER_ERROR', 42 \E_USER_WARNING => 'E_USER_WARNING', 43 \E_USER_NOTICE => 'E_USER_NOTICE', 44 \E_STRICT => 'E_STRICT', 45 ]; 46 47 private static $framesCache = []; 48 49 public static function castError(\Error $e, array $a, Stub $stub, bool $isNested, int $filter = 0) 50 { 51 return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter); 52 } 53 54 public static function castException(\Exception $e, array $a, Stub $stub, bool $isNested, int $filter = 0) 55 { 56 return self::filterExceptionArray($stub->class, $a, "\0Exception\0", $filter); 57 } 58 59 public static function castErrorException(\ErrorException $e, array $a, Stub $stub, bool $isNested) 60 { 61 if (isset($a[$s = Caster::PREFIX_PROTECTED.'severity'], self::$errorTypes[$a[$s]])) { 62 $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]); 63 } 64 65 return $a; 66 } 67 68 public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, bool $isNested) 69 { 70 $trace = Caster::PREFIX_VIRTUAL.'trace'; 71 $prefix = Caster::PREFIX_PROTECTED; 72 $xPrefix = "\0Exception\0"; 73 74 if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) { 75 $b = (array) $a[$xPrefix.'previous']; 76 $class = get_debug_type($a[$xPrefix.'previous']); 77 self::traceUnshift($b[$xPrefix.'trace'], $class, $b[$prefix.'file'], $b[$prefix.'line']); 78 $a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -\count($a[$trace]->value)); 79 } 80 81 unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']); 82 83 return $a; 84 } 85 86 public static function castSilencedErrorContext(SilencedErrorContext $e, array $a, Stub $stub, bool $isNested) 87 { 88 $sPrefix = "\0".SilencedErrorContext::class."\0"; 89 90 if (!isset($a[$s = $sPrefix.'severity'])) { 91 return $a; 92 } 93 94 if (isset(self::$errorTypes[$a[$s]])) { 95 $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]); 96 } 97 98 $trace = [[ 99 'file' => $a[$sPrefix.'file'], 100 'line' => $a[$sPrefix.'line'], 101 ]]; 102 103 if (isset($a[$sPrefix.'trace'])) { 104 $trace = array_merge($trace, $a[$sPrefix.'trace']); 105 } 106 107 unset($a[$sPrefix.'file'], $a[$sPrefix.'line'], $a[$sPrefix.'trace']); 108 $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs); 109 110 return $a; 111 } 112 113 public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, bool $isNested) 114 { 115 if (!$isNested) { 116 return $a; 117 } 118 $stub->class = ''; 119 $stub->handle = 0; 120 $frames = $trace->value; 121 $prefix = Caster::PREFIX_VIRTUAL; 122 123 $a = []; 124 $j = \count($frames); 125 if (0 > $i = $trace->sliceOffset) { 126 $i = max(0, $j + $i); 127 } 128 if (!isset($trace->value[$i])) { 129 return []; 130 } 131 $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : ''; 132 $frames[] = ['function' => '']; 133 $collapse = false; 134 135 for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) { 136 $f = $frames[$i]; 137 $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???'; 138 139 $frame = new FrameStub( 140 [ 141 'object' => $f['object'] ?? null, 142 'class' => $f['class'] ?? null, 143 'type' => $f['type'] ?? null, 144 'function' => $f['function'] ?? null, 145 ] + $frames[$i - 1], 146 false, 147 true 148 ); 149 $f = self::castFrameStub($frame, [], $frame, true); 150 if (isset($f[$prefix.'src'])) { 151 foreach ($f[$prefix.'src']->value as $label => $frame) { 152 if (str_starts_with($label, "\0~collapse=0")) { 153 if ($collapse) { 154 $label = substr_replace($label, '1', 11, 1); 155 } else { 156 $collapse = true; 157 } 158 } 159 $label = substr_replace($label, "title=Stack level $j.&", 2, 0); 160 } 161 $f = $frames[$i - 1]; 162 if ($trace->keepArgs && !empty($f['args']) && $frame instanceof EnumStub) { 163 $frame->value['arguments'] = new ArgsStub($f['args'], $f['function'] ?? null, $f['class'] ?? null); 164 } 165 } elseif ('???' !== $lastCall) { 166 $label = new ClassStub($lastCall); 167 if (isset($label->attr['ellipsis'])) { 168 $label->attr['ellipsis'] += 2; 169 $label = substr_replace($prefix, "ellipsis-type=class&ellipsis={$label->attr['ellipsis']}&ellipsis-tail=1&title=Stack level $j.", 2, 0).$label->value.'()'; 170 } else { 171 $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$label->value.'()'; 172 } 173 } else { 174 $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$lastCall; 175 } 176 $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame; 177 178 $lastCall = $call; 179 } 180 if (null !== $trace->sliceLength) { 181 $a = \array_slice($a, 0, $trace->sliceLength, true); 182 } 183 184 return $a; 185 } 186 187 public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, bool $isNested) 188 { 189 if (!$isNested) { 190 return $a; 191 } 192 $f = $frame->value; 193 $prefix = Caster::PREFIX_VIRTUAL; 194 195 if (isset($f['file'], $f['line'])) { 196 $cacheKey = $f; 197 unset($cacheKey['object'], $cacheKey['args']); 198 $cacheKey[] = self::$srcContext; 199 $cacheKey = implode('-', $cacheKey); 200 201 if (isset(self::$framesCache[$cacheKey])) { 202 $a[$prefix.'src'] = self::$framesCache[$cacheKey]; 203 } else { 204 if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) { 205 $f['file'] = substr($f['file'], 0, -\strlen($match[0])); 206 $f['line'] = (int) $match[1]; 207 } 208 $src = $f['line']; 209 $srcKey = $f['file']; 210 $ellipsis = new LinkStub($srcKey, 0); 211 $srcAttr = 'collapse='.(int) $ellipsis->inVendor; 212 $ellipsisTail = $ellipsis->attr['ellipsis-tail'] ?? 0; 213 $ellipsis = $ellipsis->attr['ellipsis'] ?? 0; 214 215 if (is_file($f['file']) && 0 <= self::$srcContext) { 216 if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) { 217 $template = null; 218 if (isset($f['object'])) { 219 $template = $f['object']; 220 } elseif ((new \ReflectionClass($f['class']))->isInstantiable()) { 221 $template = unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class'])); 222 } 223 if (null !== $template) { 224 $ellipsis = 0; 225 $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : ''); 226 $templateInfo = $template->getDebugInfo(); 227 if (isset($templateInfo[$f['line']])) { 228 if (!method_exists($template, 'getSourceContext') || !is_file($templatePath = $template->getSourceContext()->getPath())) { 229 $templatePath = null; 230 } 231 if ($templateSrc) { 232 $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f); 233 $srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']]; 234 } 235 } 236 } 237 } 238 if ($srcKey == $f['file']) { 239 $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, 'php', $f['file'], $f); 240 $srcKey .= ':'.$f['line']; 241 if ($ellipsis) { 242 $ellipsis += 1 + \strlen($f['line']); 243 } 244 } 245 $srcAttr .= sprintf('&separator= &file=%s&line=%d', rawurlencode($f['file']), $f['line']); 246 } else { 247 $srcAttr .= '&separator=:'; 248 } 249 $srcAttr .= $ellipsis ? '&ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : ''; 250 self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(["\0~$srcAttr\0$srcKey" => $src]); 251 } 252 } 253 254 unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']); 255 if ($frame->inTraceStub) { 256 unset($a[$prefix.'class'], $a[$prefix.'type'], $a[$prefix.'function']); 257 } 258 foreach ($a as $k => $v) { 259 if (!$v) { 260 unset($a[$k]); 261 } 262 } 263 if ($frame->keepArgs && !empty($f['args'])) { 264 $a[$prefix.'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']); 265 } 266 267 return $a; 268 } 269 270 private static function filterExceptionArray(string $xClass, array $a, string $xPrefix, int $filter): array 271 { 272 if (isset($a[$xPrefix.'trace'])) { 273 $trace = $a[$xPrefix.'trace']; 274 unset($a[$xPrefix.'trace']); // Ensures the trace is always last 275 } else { 276 $trace = []; 277 } 278 279 if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) { 280 if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) { 281 self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']); 282 } 283 $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs); 284 } 285 if (empty($a[$xPrefix.'previous'])) { 286 unset($a[$xPrefix.'previous']); 287 } 288 unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']); 289 290 if (isset($a[Caster::PREFIX_PROTECTED.'message']) && str_contains($a[Caster::PREFIX_PROTECTED.'message'], "@anonymous\0")) { 291 $a[Caster::PREFIX_PROTECTED.'message'] = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) { 292 return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0]; 293 }, $a[Caster::PREFIX_PROTECTED.'message']); 294 } 295 296 if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) { 297 $a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']); 298 } 299 300 return $a; 301 } 302 303 private static function traceUnshift(array &$trace, ?string $class, string $file, int $line): void 304 { 305 if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) { 306 return; 307 } 308 array_unshift($trace, [ 309 'function' => $class ? 'new '.$class : null, 310 'file' => $file, 311 'line' => $line, 312 ]); 313 } 314 315 private static function extractSource(string $srcLines, int $line, int $srcContext, string $lang, ?string $file, array $frame): EnumStub 316 { 317 $srcLines = explode("\n", $srcLines); 318 $src = []; 319 320 for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) { 321 $src[] = ($srcLines[$i] ?? '')."\n"; 322 } 323 324 if ($frame['function'] ?? false) { 325 $stub = new CutStub(new \stdClass()); 326 $stub->class = (isset($frame['class']) ? $frame['class'].$frame['type'] : '').$frame['function']; 327 $stub->type = Stub::TYPE_OBJECT; 328 $stub->attr['cut_hash'] = true; 329 $stub->attr['file'] = $frame['file']; 330 $stub->attr['line'] = $frame['line']; 331 332 try { 333 $caller = isset($frame['class']) ? new \ReflectionMethod($frame['class'], $frame['function']) : new \ReflectionFunction($frame['function']); 334 $stub->class .= ReflectionCaster::getSignature(ReflectionCaster::castFunctionAbstract($caller, [], $stub, true, Caster::EXCLUDE_VERBOSE)); 335 336 if ($f = $caller->getFileName()) { 337 $stub->attr['file'] = $f; 338 $stub->attr['line'] = $caller->getStartLine(); 339 } 340 } catch (\ReflectionException $e) { 341 // ignore fake class/function 342 } 343 344 $srcLines = ["\0~separator=\0" => $stub]; 345 } else { 346 $stub = null; 347 $srcLines = []; 348 } 349 350 $ltrim = 0; 351 do { 352 $pad = null; 353 for ($i = $srcContext << 1; $i >= 0; --$i) { 354 if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) { 355 if (null === $pad) { 356 $pad = $c; 357 } 358 if ((' ' !== $c && "\t" !== $c) || $pad !== $c) { 359 break; 360 } 361 } 362 } 363 ++$ltrim; 364 } while (0 > $i && null !== $pad); 365 366 --$ltrim; 367 368 foreach ($src as $i => $c) { 369 if ($ltrim) { 370 $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t"); 371 } 372 $c = substr($c, 0, -1); 373 if ($i !== $srcContext) { 374 $c = new ConstStub('default', $c); 375 } else { 376 $c = new ConstStub($c, $stub ? 'in '.$stub->class : ''); 377 if (null !== $file) { 378 $c->attr['file'] = $file; 379 $c->attr['line'] = $line; 380 } 381 } 382 $c->attr['lang'] = $lang; 383 $srcLines[sprintf("\0~separator=› &%d\0", $i + $line - $srcContext)] = $c; 384 } 385 386 return new EnumStub($srcLines); 387 } 388 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |