PHP_Shell
[ class tree: PHP_Shell ] [ index: PHP_Shell ] [ all elements ]

Source for file php-shell-cmd.php

Documentation is available at php-shell-cmd.php

  1. <?php
  2.  
  3. /**
  4. * the wrapper around the PHP_Shell class
  5. *
  6. * - load extensions
  7. * - set default error-handler
  8. * - add exec-hooks for the extensions
  9. *
  10. * To keep the namespace clashing between shell and your program
  11. * as small as possible all public variables and functions from
  12. * the shell are prefixed with __shell:
  13. * - $__shell is the object of the shell
  14. *   can be read, this is the shell object itself, don't touch it
  15. * - $__shell_retval is the return value of the eval() before
  16. *   it is printed
  17. *   can't be read, but overwrites existing vars with this name
  18. * - $__shell_exception is the catched Exception on Warnings, Notices, ..
  19. *   can't be read, but overwrites existing vars with this name
  20. */
  21.  
  22. require_once "PHP/Shell.php";
  23. require_once "PHP/Shell/Extensions/Autoload.php";
  24. require_once "PHP/Shell/Extensions/AutoloadDebug.php";
  25. require_once "PHP/Shell/Extensions/Colour.php";
  26. require_once "PHP/Shell/Extensions/ExecutionTime.php";
  27. require_once "PHP/Shell/Extensions/InlineHelp.php";
  28. require_once "PHP/Shell/Extensions/VerbosePrint.php";
  29.     
  30. /**
  31. * default error-handler
  32. *
  33. * Instead of printing the NOTICE or WARNING from php we wan't the turn non-FATAL
  34. * messages into exceptions and handle them in our own way.
  35. *
  36. * you can set your own error-handler by createing a function named
  37. * __shell_error_handler
  38. *
  39. @param integer $errno Error-Number
  40. @param string $errstr Error-Message
  41. @param string $errfile Filename where the error was raised
  42. @param interger $errline Line-Number in the File
  43. @param mixed $errctx ...
  44. */
  45. function __shell_default_error_handler($errno$errstr$errfile$errline$errctx{
  46.     ## ... what is this errno again ?
  47.     if ($errno == 2048return;
  48.   
  49.     throw new Exception(sprintf("%s:%d\r\n%s"$errfile$errline$errstr));
  50. }
  51.  
  52. set_error_handler("__shell_default_error_handler");
  53.  
  54. $__shell = new PHP_Shell();
  55. $__shell_exts = PHP_Shell_Extensions::getInstance();
  56. $__shell_exts->registerExtensions(array(
  57.     "options"        => PHP_Shell_Options::getInstance()/* the :set command */
  58.  
  59.     "autoload"       => new PHP_Shell_Extensions_Autoload(),
  60.     "autoload_debug" => new PHP_Shell_Extensions_AutoloadDebug(),
  61.     "colour"         => new PHP_Shell_Extensions_Colour(),
  62.     "exectime"       => new PHP_Shell_Extensions_ExecutionTime(),
  63.     "inlinehelp"     => new PHP_Shell_Extensions_InlineHelp(),
  64.     "verboseprint"   => new PHP_Shell_Extensions_VerbosePrint(),
  65. ));
  66.  
  67. $f = <<<EOF
  68. PHP-Shell - Version %s%s
  69. (c) 2006, Jan Kneschke <jan@kneschke.de>
  70.  
  71. >> use '?' to open the inline help 
  72.  
  73. EOF;
  74.  
  75. printf($f
  76.     $__shell->getVersion()
  77.     $__shell->hasReadline(', with readline() support' '');
  78. unset($f);
  79.  
  80. print $__shell_exts->colour->getColour("default");
  81. while($__shell->input()) {
  82.     if ($__shell_exts->autoload->isAutoloadEnabled(&& !function_exists('__autoload')) {
  83.         /**
  84.         * default autoloader
  85.         *
  86.         * If a class doesn't exist try to load it by guessing the filename
  87.         * class PHP_Shell should be located in PHP/Shell.php.
  88.         *
  89.         * you can set your own autoloader by defining __autoload() before including
  90.         * this file
  91.         * 
  92.         * @param string $classname name of the class
  93.         */
  94.  
  95.         function __autoload($classname{
  96.             global $__shell_exts;
  97.  
  98.             if ($__shell_exts->autoload_debug->isAutoloadDebug()) {
  99.                 print str_repeat("."$__shell_exts->autoload_debug->incAutoloadDepth())." -> autoloading $classname".PHP_EOL;
  100.             }
  101.             include_once str_replace('_''/'$classname).'.php';
  102.             if ($__shell_exts->autoload_debug->isAutoloadDebug()) {
  103.                 print str_repeat("."$__shell_exts->autoload_debug->decAutoloadDepth())." <- autoloading $classname".PHP_EOL;
  104.             }
  105.         }
  106.     }
  107.  
  108.     try {
  109.         $__shell_exts->exectime->startParseTime();
  110.         if ($__shell->parse(== 0{
  111.             ## we have a full command, execute it
  112.  
  113.             $__shell_exts->exectime->startExecTime();
  114.  
  115.             $__shell_retval = eval($__shell->getCode())
  116.             if (isset($__shell_retval)) {
  117.                 print $__shell_exts->colour->getColour("value");
  118.  
  119.                 if (function_exists("__shell_print_var")) {
  120.                     __shell_print_var($__shell_retval$__shell_exts->verboseprint->isVerbose());
  121.                 else {
  122.                     var_export($__shell_retval);
  123.                 }
  124.             }
  125.             ## cleanup the variable namespace
  126.             unset($__shell_retval);
  127.             $__shell->resetCode();
  128.         }
  129.     catch(Exception $__shell_exception{
  130.         print $__shell_exts->colour->getColour("exception");
  131.         print $__shell_exception->getMessage();
  132.         
  133.         $__shell->resetCode();
  134.  
  135.         ## cleanup the variable namespace
  136.         unset($__shell_exception);
  137.     }
  138.     print $__shell_exts->colour->getColour("default");
  139.     $__shell_exts->exectime->stopTime();
  140.     if ($__shell_exts->exectime->isShow()) {
  141.         printf(" (parse: %.4fms, exec: %.4fms)"
  142.             $__shell_exts->exectime->getParseTime(),
  143.             $__shell_exts->exectime->getExecTime()
  144.         );
  145.     }
  146. }
  147.  
  148. print $__shell_exts->colour->getColour("reset");

Documentation generated on Mon, 11 Mar 2019 14:40:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.