From 1753c275ffbfd4c96045218e1d1ada17282a0e0a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 31 May 2015 12:26:17 +0100 Subject: [PATCH] Handle deprecation notices softer --- src/Composer/Console/Application.php | 2 +- src/Composer/Util/ErrorHandler.php | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 9896afe42..ee4f4e9c0 100644 --- a/src/Composer/Console/Application.php +++ b/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('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.'); diff --git a/src/Composer/Util/ErrorHandler.php b/src/Composer/Util/ErrorHandler.php index d179e2cc9..b10cb15d5 100644 --- a/src/Composer/Util/ErrorHandler.php +++ b/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('Deprecation Notice: '.$message.' in '.$file.':'.$line.''); + if (self::$io->isVerbose()) { + self::$io->writeError('Stack trace:'); + self::$io->writeError(array_filter(array_map(function ($a) { + if (isset($a['line'], $a['file'])) { + return ' '.$a['file'].':'.$a['line'].''; + } + 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; } }