Browse Source

Handle deprecation notices softer

Jordi Boggiano 9 years ago
parent
commit
1753c275ff
2 changed files with 23 additions and 3 deletions
  1. 1 1
      src/Composer/Console/Application.php
  2. 22 2
      src/Composer/Util/ErrorHandler.php

+ 1 - 1
src/Composer/Console/Application.php

@@ -65,7 +65,6 @@ class Application extends BaseApplication
             date_default_timezone_set(@date_default_timezone_get());
         }
 
-        ErrorHandler::register();
         parent::__construct('Composer', Composer::VERSION);
     }
 
@@ -89,6 +88,7 @@ class Application extends BaseApplication
     public function doRun(InputInterface $input, OutputInterface $output)
     {
         $this->io = new ConsoleIO($input, $output, $this->getHelperSet());
+        ErrorHandler::register($this->io);
 
         if (PHP_VERSION_ID < 50302) {
             $this->getIO()->writeError('<warning>Composer only officially supports PHP 5.3.2 and above, you will most likely encounter problems with your PHP '.PHP_VERSION.', upgrading is strongly recommended.</warning>');

+ 22 - 2
src/Composer/Util/ErrorHandler.php

@@ -12,6 +12,8 @@
 
 namespace Composer\Util;
 
+use Composer\IO\IOInterface;
+
 /**
  * Convert PHP errors into exceptions
  *
@@ -19,6 +21,8 @@ namespace Composer\Util;
  */
 class ErrorHandler
 {
+    private static $io;
+
     /**
      * Error handler
      *
@@ -42,7 +46,22 @@ class ErrorHandler
             "\na legitimately suppressed error that you were not supposed to see.";
         }
 
-        throw new \ErrorException($message, 0, $level, $file, $line);
+        if ($level !== E_DEPRECATED && $level !== E_USER_DEPRECATED) {
+            throw new \ErrorException($message, 0, $level, $file, $line);
+        }
+
+        if (self::$io) {
+            self::$io->writeError('<warning>Deprecation Notice: '.$message.' in '.$file.':'.$line.'</warning>');
+            if (self::$io->isVerbose()) {
+                self::$io->writeError('<warning>Stack trace:</warning>');
+                self::$io->writeError(array_filter(array_map(function ($a) {
+                    if (isset($a['line'], $a['file'])) {
+                        return '<warning> '.$a['file'].':'.$a['line'].'</warning>';
+                    }
+                    return null;
+                }, array_slice(debug_backtrace(), 2))));
+            }
+        }
     }
 
     /**
@@ -50,8 +69,9 @@ class ErrorHandler
      *
      * @static
      */
-    public static function register()
+    public static function register(IOInterface $io = null)
     {
         set_error_handler(array(__CLASS__, 'handle'));
+        self::$io = $io;
     }
 }