From 8056491a1ae5bd88ef031e10f0efc3bfb435a1d9 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 19 Aug 2014 09:05:55 +0000 Subject: [PATCH] Add error management in CLI (display the trigger_errors in stderr) git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1732 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- cli.php | 77 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/cli.php b/cli.php index b3d0073..d59f1f9 100644 --- a/cli.php +++ b/cli.php @@ -13,36 +13,55 @@ class cli require ("domframework/cli.php"); $cli = new cli; $cli->run(); */ - public function run () - { - global $argv; - $launcher = $argv[0]; - chdir (dirname ($argv[0])."/.."); - array_shift ($argv); - if (isset ($argv[0]) && $argv[0] === "-h") - { - echo "Execute in CLI a controller or a model (in expert mode)\n"; - echo " $launcher -h : display this help\n"; - echo " $launcher -list : display controllers\n"; - echo " $launcher -expert -list : display controllers and models\n"; - echo " $launcher -listmethods : \n"; - echo " display the methods available the controller class\n"; - echo " $launcher -expert -listmethods :\n"; - echo " display the methods available the model or controller class". - "\n"; - echo " $launcher -listmethodsdetails : \n"; - echo " display the methods available the controller class\n"; - echo " $launcher -expert -listmethodsdetails :\n"; - echo " display the methods available the model or controller class". - "\n"; - echo " $launcher [args]\n"; - echo " execute the method with the provided args\n"; - echo " $launcher -expert [args]\n"; - echo " execute the method with the provided args\n"; - echo "You can replace ONE arg by a dash (-) to read from stdin\n"; - echo "Arrays must be coded like key1=val1&key2=val2&key3=val3...\n"; - exit; + public function __construct () + { + // Catch the trigger_errors and display them politely + set_error_handler(array(&$this, "cliErrorHandler")); + } + + /** The error handler for CLI : display error in STDERR */ + public function cliErrorHandler ($errno, $errstr, $errfile, $errline) + { + if (!(error_reporting() & $errno)) + { + // This error code is not included in error_reporting + return; + } + file_put_contents ("php://stderr", "$errstr [".basename ($errfile) . + ":$errline]\n"); + } + + public function run () + { + global $argv; + $launcher = $argv[0]; + chdir (dirname ($argv[0])."/.."); + array_shift ($argv); + + if (isset ($argv[0]) && $argv[0] === "-h") + { + echo "Execute in CLI a controller or a model (in expert mode)\n"; + echo " $launcher -h : display this help\n"; + echo " $launcher -list : display controllers\n"; + echo " $launcher -expert -list : display controllers and models\n"; + echo " $launcher -listmethods : \n"; + echo " display the methods available the controller class\n"; + echo " $launcher -expert -listmethods :\n"; + echo " display the methods available the model or controller class". + "\n"; + echo " $launcher -listmethodsdetails : \n"; + echo " display the methods available the controller class\n"; + echo " $launcher -expert -listmethodsdetails :\n"; + echo " display the methods available the model or controller class". + "\n"; + echo " $launcher [args]\n"; + echo " execute the method with the provided args\n"; + echo " $launcher -expert [args]\n"; + echo " execute the method with the provided args\n"; + echo "You can replace ONE arg by a dash (-) to read from stdin\n"; + echo "Arrays must be coded like key1=val1&key2=val2&key3=val3...\n"; + exit; } $EXPERT = FALSE;