3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Debug; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\Debug\Exception\OutOfMemoryException; if (!defined('ENT_SUBSTITUTE')) { define('ENT_SUBSTITUTE', 8); } /** * ExceptionHandler converts an exception to a Response object. * * It is mostly useful in debug mode to replace the default PHP/XDebug * output with something prettier and more useful. * * As this class is mainly used during Kernel boot, where nothing is yet * available, the Response content is always HTML. * * @author Fabien Potencier <fabien@symfony.com> */ class ExceptionHandler { private $debug; private $charset; private $handler; private $caughtOutput = 0; public function __construct($debug = true, $charset = 'UTF-8') { $this->debug = $debug; $this->charset = $charset; } /** * Registers the exception handler. * * @param bool $debug * * @return ExceptionHandler The registered exception handler */ public static function register($debug = true) { $handler = new static($debug); set_exception_handler(array($handler, 'handle')); return $handler; } /** * Sets a user exception handler. * * @param callable $handler An handler that will be called on Exception * * @return callable|null The previous exception handler if any */ public function setHandler($handler) { if (isset($handler) && !is_callable($handler)) { throw new \LogicException('The exception handler must be a valid PHP callable.'); } $old = $this->handler; $this->handler = $handler; return $old; } /** * {@inheritdoc} * * Sends a response for the given Exception. * * If you have the Symfony HttpFoundation component installed, * this method will use it to create and send the response. If not, * it will fallback to plain PHP functions. * * @see sendPhpResponse * @see createResponse */ public function handle(\Exception $exception) { if ($exception instanceof OutOfMemoryException) { $this->sendPhpResponse($exception); return; } // To be as fail-safe as possible, the exception is first handled // by our simple exception handler, then by the user exception handler. // The latter takes precedence and any output from the former is cancelled, // if and only if nothing bad happens in this handling path. $caughtOutput = 0; $this->caughtOutput = false; ob_start(array($this, 'catchOutput')); try { if (class_exists('Symfony\Component\HttpFoundation\Response')) { $response = $this->createResponse($exception); $response->sendHeaders(); $response->sendContent(); } else { $this->sendPhpResponse($exception); } } catch (\Exception $e) { // Ignore this $e exception, we have to deal with $exception } if (false === $this->caughtOutput) { ob_end_clean(); } if (isset($this->caughtOutput[0])) { ob_start(array($this, 'cleanOutput')); echo $this->caughtOutput; $caughtOutput = ob_get_length(); } $this->caughtOutput = 0; if (!empty($this->handler)) { try { call_user_func($this->handler, $exception); if ($caughtOutput) { $this->caughtOutput = $caughtOutput; } ob_end_flush(); } catch (\Exception $e) { if (!$caughtOutput) { // All handlers failed. Let PHP handle that now. throw $exception; } } } } /** * Sends the error associated with the given Exception as a plain PHP response. * * This method uses plain PHP functions like header() and echo to output * the response. * * @param \Exception|FlattenException $exception An \Exception instance */ public function sendPhpResponse($exception) { echo $exception->getMessage(); } /** * @internal */ public function catchOutput($buffer) { $this->caughtOutput = $buffer; return '456'; } /** * @internal */ public function cleanOutput($buffer) { if ($this->caughtOutput) { // use substr_replace() instead of substr() for mbstring overloading resistance $cleanBuffer = substr_replace($buffer, '', 0, $this->caughtOutput); if (isset($cleanBuffer[0])) { $buffer = $cleanBuffer; } } return $buffer; } } ExceptionHandler::register(); throw new \Exception('foo msg');
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
filename:       /in/39ves
function name:  (null)
number of ops:  16
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cdefined'
          1        SEND_VAL_EX                                              'ENT_SUBSTITUTE'
          2        DO_FCALL                                      0  $0      
          3        BOOL_NOT                                         ~1      $0
          4      > JMPZ                                                     ~1, ->9
   19     5    >   INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cdefine'
          6        SEND_VAL_EX                                              'ENT_SUBSTITUTE'
          7        SEND_VAL_EX                                              8
          8        DO_FCALL                                      0          
  187     9    >   INIT_STATIC_METHOD_CALL                                  'Symfony%5CComponent%5CDebug%5CExceptionHandler', 'register'
         10        DO_FCALL                                      0          
  189    11        NEW                                              $4      'Exception'
         12        SEND_VAL_EX                                              'foo+msg'
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $4
         15*     > RETURN                                                   1

Class Symfony\Component\Debug\ExceptionHandler:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/39ves
function name:  __construct
number of ops:  7
compiled vars:  !0 = $debug, !1 = $charset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV_INIT                                        !0      <true>
          1        RECV_INIT                                        !1      'UTF-8'
   42     2        ASSIGN_OBJ                                               'debug'
          3        OP_DATA                                                  !0
   43     4        ASSIGN_OBJ                                               'charset'
          5        OP_DATA                                                  !1
   44     6      > RETURN                                                   null

End of function __construct

Function register:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/39ves
function name:  register
number of ops:  12
compiled vars:  !0 = $debug, !1 = $handler
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV_INIT                                        !0      <true>
   55     1        NEW                          static              $2      
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $2
   57     5        INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cset_exception_handler'
          6        INIT_ARRAY                                       ~5      !1
          7        ADD_ARRAY_ELEMENT                                ~5      'handle'
          8        SEND_VAL_EX                                              ~5
          9        DO_FCALL                                      0          
   59    10      > RETURN                                                   !1
   60    11*     > RETURN                                                   null

End of function register

Function sethandler:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/39ves
function name:  setHandler
number of ops:  19
compiled vars:  !0 = $handler, !1 = $old
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   RECV                                             !0      
   71     1        ISSET_ISEMPTY_CV                                 ~2      !0
          2      > JMPZ_EX                                          ~2      ~2, ->8
          3    >   INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cis_callable'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $3      
          6        BOOL_NOT                                         ~4      $3
          7        BOOL                                             ~2      ~4
          8    > > JMPZ                                                     ~2, ->13
   72     9    >   NEW                                              $5      'LogicException'
         10        SEND_VAL_EX                                              'The+exception+handler+must+be+a+valid+PHP+callable.'
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $5
   74    13    >   FETCH_OBJ_R                                      ~7      'handler'
         14        ASSIGN                                                   !1, ~7
   75    15        ASSIGN_OBJ                                               'handler'
         16        OP_DATA                                                  !0
   77    17      > RETURN                                                   !1
   78    18*     > RETURN                                                   null

End of function sethandler

Function handle:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 29
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 39
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 53
Branch analysis from position: 42
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 74
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 67
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 74
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
Branch analysis from position: 74
Branch analysis from position: 53
Branch analysis from position: 39
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Found catch point at position: 33
Branch analysis from position: 33
2 jumps found. (Code = 107) Position 1 = 34, Position 2 = -2
Branch analysis from position: 34
Found catch point at position: 70
Branch analysis from position: 70
2 jumps found. (Code = 107) Position 1 = 71, Position 2 = -2
Branch analysis from position: 71
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 74
Branch analysis from position: 73
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 74
filename:       /in/39ves
function name:  handle
number of ops:  75
compiled vars:  !0 = $exception, !1 = $caughtOutput, !2 = $response, !3 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   92     0  E >   RECV                                             !0      
   94     1        INSTANCEOF                                               !0, 'Symfony%5CComponent%5CDebug%5CException%5COutOfMemoryException'
          2      > JMPZ                                                     ~4, ->7
   95     3    >   INIT_METHOD_CALL                                         'sendPhpResponse'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
   97     6      > RETURN                                                   null
  105     7    >   ASSIGN                                                   !1, 0
  107     8        ASSIGN_OBJ                                               'caughtOutput'
          9        OP_DATA                                                  <false>
  108    10        INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cob_start'
         11        FETCH_THIS                                       ~8      
         12        INIT_ARRAY                                       ~9      ~8
         13        ADD_ARRAY_ELEMENT                                ~9      'catchOutput'
         14        SEND_VAL_EX                                              ~9
         15        DO_FCALL                                      0          
  110    16        INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cclass_exists'
         17        SEND_VAL_EX                                              'Symfony%5CComponent%5CHttpFoundation%5CResponse'
         18        DO_FCALL                                      0  $11     
         19      > JMPZ                                                     $11, ->29
  111    20    >   INIT_METHOD_CALL                                         'createResponse'
         21        SEND_VAR_EX                                              !0
         22        DO_FCALL                                      0  $12     
         23        ASSIGN                                                   !2, $12
  112    24        INIT_METHOD_CALL                                         !2, 'sendHeaders'
         25        DO_FCALL                                      0          
  113    26        INIT_METHOD_CALL                                         !2, 'sendContent'
         27        DO_FCALL                                      0          
         28      > JMP                                                      ->32
  115    29    >   INIT_METHOD_CALL                                         'sendPhpResponse'
         30        SEND_VAR_EX                                              !0
         31        DO_FCALL                                      0          
         32    > > JMP                                                      ->34
  117    33  E > > CATCH                                       last         'Exception'
  120    34    >   FETCH_OBJ_R                                      ~17     'caughtOutput'
         35        TYPE_CHECK                                    4          ~17
         36      > JMPZ                                                     ~18, ->39
  121    37    >   INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cob_end_clean'
         38        DO_FCALL                                      0          
  123    39    >   FETCH_OBJ_IS                                     ~20     'caughtOutput'
         40        ISSET_ISEMPTY_DIM_OBJ                         0          ~20, 0
         41      > JMPZ                                                     ~21, ->53
  124    42    >   INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cob_start'
         43        FETCH_THIS                                       ~22     
         44        INIT_ARRAY                                       ~23     ~22
         45        ADD_ARRAY_ELEMENT                                ~23     'cleanOutput'
         46        SEND_VAL_EX                                              ~23
         47        DO_FCALL                                      0          
  125    48        FETCH_OBJ_R                                      ~25     'caughtOutput'
         49        ECHO                                                     ~25
  126    50        INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cob_get_length'
         51        DO_FCALL                                      0  $26     
         52        ASSIGN                                                   !1, $26
  128    53    >   ASSIGN_OBJ                                               'caughtOutput'
         54        OP_DATA                                                  0
  130    55        ISSET_ISEMPTY_PROP_OBJ                           ~29     'handler'
         56        BOOL_NOT                                         ~30     ~29
         57      > JMPZ                                                     ~30, ->74
  132    58    >   INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Ccall_user_func'
         59        CHECK_FUNC_ARG                                           
         60        FETCH_OBJ_FUNC_ARG                               $31     'handler'
         61        SEND_FUNC_ARG                                            $31
         62        SEND_VAR_EX                                              !0
         63        DO_FCALL                                      0          
  134    64      > JMPZ                                                     !1, ->67
  135    65    >   ASSIGN_OBJ                                               'caughtOutput'
         66        OP_DATA                                                  !1
  137    67    >   INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Cob_end_flush'
         68        DO_FCALL                                      0          
         69      > JMP                                                      ->74
  138    70  E > > CATCH                                       last         'Exception'
  139    71    >   BOOL_NOT                                         ~35     !1
         72      > JMPZ                                                     ~35, ->74
  141    73    > > THROW                                         0          !0
  145    74    > > RETURN                                                   null

End of function handle

Function sendphpresponse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/39ves
function name:  sendPhpResponse
number of ops:  5
compiled vars:  !0 = $exception
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  155     0  E >   RECV                                             !0      
  157     1        INIT_METHOD_CALL                                         !0, 'getMessage'
          2        DO_FCALL                                      0  $1      
          3        ECHO                                                     $1
  158     4      > RETURN                                                   null

End of function sendphpresponse

Function catchoutput:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/39ves
function name:  catchOutput
number of ops:  5
compiled vars:  !0 = $buffer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  163     0  E >   RECV                                             !0      
  165     1        ASSIGN_OBJ                                               'caughtOutput'
          2        OP_DATA                                                  !0
  167     3      > RETURN                                                   '456'
  168     4*     > RETURN                                                   null

End of function catchoutput

Function cleanoutput:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
Branch analysis from position: 15
filename:       /in/39ves
function name:  cleanOutput
number of ops:  17
compiled vars:  !0 = $buffer, !1 = $cleanBuffer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  173     0  E >   RECV                                             !0      
  175     1        FETCH_OBJ_R                                      ~2      'caughtOutput'
          2      > JMPZ                                                     ~2, ->15
  177     3    >   INIT_NS_FCALL_BY_NAME                                    'Symfony%5CComponent%5CDebug%5Csubstr_replace'
          4        SEND_VAR_EX                                              !0
          5        SEND_VAL_EX                                              ''
          6        SEND_VAL_EX                                              0
          7        CHECK_FUNC_ARG                                           
          8        FETCH_OBJ_FUNC_ARG                               $3      'caughtOutput'
          9        SEND_FUNC_ARG                                            $3
         10        DO_FCALL                                      0  $4      
         11        ASSIGN                                                   !1, $4
  178    12        ISSET_ISEMPTY_DIM_OBJ                         0          !1, 0
         13      > JMPZ                                                     ~6, ->15
  179    14    >   ASSIGN                                                   !0, !1
  183    15    > > RETURN                                                   !0
  184    16*     > RETURN                                                   null

End of function cleanoutput

End of class Symfony\Component\Debug\ExceptionHandler.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
183.06 ms | 953 KiB | 36 Q