vendor/symfony/src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     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\HttpKernel\Debug;
       
    13 
       
    14 /**
       
    15  * ErrorHandler.
       
    16  *
       
    17  * @author Fabien Potencier <fabien@symfony.com>
       
    18  */
       
    19 class ErrorHandler
       
    20 {
       
    21     private $levels = array(
       
    22         E_WARNING           => 'Warning',
       
    23         E_NOTICE            => 'Notice',
       
    24         E_USER_ERROR        => 'User Error',
       
    25         E_USER_WARNING      => 'User Warning',
       
    26         E_USER_NOTICE       => 'User Notice',
       
    27         E_STRICT            => 'Runtime Notice',
       
    28         E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
       
    29     );
       
    30 
       
    31     private $level;
       
    32 
       
    33     /**
       
    34      * Register the error handler.
       
    35      *
       
    36      * @param integer $level The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
       
    37      *
       
    38      * @return The registered error handler
       
    39      */
       
    40     static public function register($level = null)
       
    41     {
       
    42         $handler = new static();
       
    43         $handler->setLevel($level);
       
    44 
       
    45         set_error_handler(array($handler, 'handle'));
       
    46 
       
    47         return $handler;
       
    48     }
       
    49 
       
    50     public function setLevel($level)
       
    51     {
       
    52         $this->level = null === $level ? error_reporting() : $level;
       
    53     }
       
    54 
       
    55     /**
       
    56      * @throws \ErrorException When error_reporting returns error
       
    57      */
       
    58     public function handle($level, $message, $file, $line, $context)
       
    59     {
       
    60         if (0 === $this->level) {
       
    61             return false;
       
    62         }
       
    63 
       
    64         if (error_reporting() & $level && $this->level & $level) {
       
    65             throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line));
       
    66         }
       
    67 
       
    68         return false;
       
    69     }
       
    70 }