Back to index

php5  5.3.10
Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions
CLICommand Class Reference

Abstract base console command implementation. More...

Inheritance diagram for CLICommand:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 __construct ($argc, array $argv)
 checkArgTyp ($arg, $i, $argc, $argv)
 cli_get_SP2 ($l1, $arg_inf)
 cli_get_SP3 ($l1, $l2, $arg_inf)
 cli_cmd_arg_help ()
 cli_cmd_run_help ()
 cli_cmd_run_help_list ()

Static Public Member Functions

static notice ($msg)
static error ($msg, $exit_code=1)
static getSubFuncs (CLICommand $cmdclass, $prefix, array $subs)
static getCommands (CLICommand $cmdclass)
static getArgTyps (CLICommand $cmdclass)
static cli_arg_typ_bool ($arg, $cfg, $key)
static cli_arg_typ_int ($arg, $cfg, $key)
static cli_arg_typ_regex ($arg, $cfg, $key)
static cli_arg_typ_select ($arg, $cfg, $key)
static cli_arg_typ_dir ($arg, $cfg, $key)
static cli_arg_typ_file ($arg)
static cli_arg_typ_filenew ($arg, $cfg, $key)
static cli_arg_typ_filecont ($arg, $cfg, $key)
static cli_cmd_inf_help ()
static cli_cmd_inf_help_list ()

Protected Attributes

 $argc
 $argv
 $cmds = array()
 $args = array()
 $typs = array()

Private Member Functions

 cli_wordwrap ($what, $l, $sp)
 cli_help_get_args ($func, $l, $sp, $required)

Detailed Description

Abstract base console command implementation.

Author:
Marcus Boerger
Version:
1.0

Definition at line 17 of file clicommand.inc.


Constructor & Destructor Documentation

CLICommand::__construct ( argc,
array $  argv 
)

Definition at line 25 of file clicommand.inc.

       {
              $this->argc = $argc;
              $this->argv = $argv;
              $this->cmds = self::getCommands($this);
              $this->typs = self::getArgTyps($this);

              if ($argc < 2) {
                     self::error("No command given, check ${argv[0]} help\n");
              } elseif (!isset($this->cmds[$argv[1]]['run'])) {
                     self::error("Unknown command '${argv[1]}', check ${argv[0]} help\n");
              } else {
                     $command = $argv[1];
              }

              if (isset($this->cmds[$command]['arg'])) {
                     $this->args = call_user_func(array($this, $this->cmds[$command]['arg']));
                     $i = 1;
                     $missing = false;
                     while (++$i < $argc) {
                            if ($argv[$i][0] == '-') {
                                   if (strlen($argv[$i]) == 2 && isset($this->args[$argv[$i][1]])) {
                                          $arg = $argv[$i][1];
                                          if (++$i >= $argc) {
                                                 self::error("Missing argument to parameter '$arg' of command '$command', check ${argv[0]} help\n");
                                          } else {
                                                 $this->args[$arg]['val'] = $this->checkArgTyp($arg, $i, $argc, $argv);
                                          }
                                   }  else {
                                          self::error("Unknown parameter '${argv[$i]}' to command $command, check ${argv[0]} help\n");
                                   }
                            } else {
                                   break;
                            }
                     }

                     if (isset($this->args[''])) {
                            if ($i >= $argc) {
                                   if (isset($this->args['']['require']) && $this->args['']['require']) {
                                          self::error("Missing default trailing arguments to command $command, check ${argv[0]} help\n");
                                   }
                            } else {
                                   $this->args['']['val'] = array();
                                   while($i < $argc) {
                                          $this->args['']['val'][] = $argv[$i++];
                                   }
                            }
                     } else if ($i < $argc) {
                            self::error("Unexpected default arguments to command $command, check ${argv[0]} help\n");
                     }

                     foreach($this->args as $arg => $inf) {
                            if (strlen($arg) && !isset($inf['val']) && isset($inf['required']) && $inf['required']) {
                                   $missing .=  "Missing parameter '-$arg' to command $command, check ${argv[0]} help\n";
                            }
                     }

                     if (strlen($missing)) {
                            self::error($missing);
                     }
              }

              call_user_func(array($this, $this->cmds[$command]['run']), $this->args);
       }

Here is the call graph for this function:


Member Function Documentation

CLICommand::checkArgTyp ( arg,
i,
argc,
argv 
)

Definition at line 101 of file clicommand.inc.

       {
              $typ = $this->args[$arg]['typ'];

              if (isset($this->typs[$typ]['typ'])) {
                     return call_user_func(array($this, $this->typs[$typ]['typ']), $argv[$i], $this->args[$arg], $arg);
              } else {
                     return $argv[$i];
              }
       }

Here is the caller graph for this function:

static CLICommand::cli_arg_typ_bool ( arg,
cfg,
key 
) [static]

Definition at line 146 of file clicommand.inc.

       {
              return (bool)$arg;
       }
static CLICommand::cli_arg_typ_dir ( arg,
cfg,
key 
) [static]

Definition at line 181 of file clicommand.inc.

       {
              $f = realpath($arg);

              if ($f===false || !file_exists($f) || !is_dir($f)) {
                     self::error("Requested path '$arg' does not exist.\n");
              }
              return $f;
       }

Here is the call graph for this function:

static CLICommand::cli_arg_typ_file ( arg) [static]

Definition at line 191 of file clicommand.inc.

       {
              $f = new SplFileInfo($arg);
              $f = $f->getRealPath();
              if ($f===false || !file_exists($f)) {
                     echo "Requested file '$arg' does not exist.\n";
                     exit(1);
              }
              return $f;
       }

Here is the caller graph for this function:

static CLICommand::cli_arg_typ_filecont ( arg,
cfg,
key 
) [static]

Definition at line 213 of file clicommand.inc.

       {
              return file_get_contents(self::cli_arg_typ_file($arg, $cfg, $key));
       }
static CLICommand::cli_arg_typ_filenew ( arg,
cfg,
key 
) [static]

Definition at line 202 of file clicommand.inc.

       {
              $d = dirname($arg);
              $f = realpath($d);

              if ($f === false) {
                     self::error("Path for file '$arg' does not exist.\n");
              }
              return $f . '/' . basename($arg);
       }

Here is the call graph for this function:

Here is the caller graph for this function:

static CLICommand::cli_arg_typ_int ( arg,
cfg,
key 
) [static]

Definition at line 151 of file clicommand.inc.

       {
              if ((int)$arg != $arg) {
                     self::error("Argument to -$key must be an integer.\n");
              }

              return (int)$arg;
       }

Here is the call graph for this function:

static CLICommand::cli_arg_typ_regex ( arg,
cfg,
key 
) [static]

Definition at line 160 of file clicommand.inc.

       {
              if (strlen($arg)) {
                     if (strlen($arg) > 1 && $arg[0] == $arg[strlen($arg)-1] && strpos('/,', $arg) !== false) {
                            return $arg;
                     } else {
                            return '/' . $arg . '/';
                     }
              } else {
                     return NULL;
              }
       }
static CLICommand::cli_arg_typ_select ( arg,
cfg,
key 
) [static]

Definition at line 173 of file clicommand.inc.

       {
              if (!in_array($arg, array_keys($cfg['select']))) {
                     self::error("Parameter value '$arg' not one of '" . join("', '", array_keys($cfg['select'])) . "'.\n");
              }
              return $arg;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 281 of file clicommand.inc.

       {
              return array('' => array('typ'=>'any','val'=>NULL,'inf'=>'Optional command to retrieve help for.'));
       }
static CLICommand::cli_cmd_inf_help ( ) [static]

Definition at line 228 of file clicommand.inc.

       {
              return "This help or help for a selected command.";
       }
static CLICommand::cli_cmd_inf_help_list ( ) [static]

Definition at line 339 of file clicommand.inc.

       {
              return "Lists available commands.";
       }

Definition at line 286 of file clicommand.inc.

       {
              $argv  = $this->argv;
              $which = $this->args['']['val'];
              if (isset($which)) {
                     if (count($which) != 1) {
                            self::error("More than one command given.\n");
                     }

                     $which = $which[0];
                     if (!array_key_exists($which, $this->cmds)) {
                            if (strtolower($which) == 'commands') {
                                   self::cli_cmd_run_help_list();
                                   exit(0);
                            }
                            self::error("Unknown command, cannot retrieve help.\n");
                     }

                     $l = strlen($which);
                     $cmds = array($which => $this->cmds[$which]);
              } else {
                     echo "\n$argv[0] <command> [options]\n\n";
                     $l = 0;
                     ksort($this->cmds);
                     foreach($this->cmds as $name => $funcs) {
                            $l = max($l, strlen($name));
                     }
                     $inf = "Commands:";
                     $lst = "";
                     $ind = strlen($inf) + 1;
                     foreach($this->cmds as $name => $funcs) {
                            $lst .= ' ' . $name;
                     }
                     echo $this->cli_wordwrap($inf.$lst, $ind, str_repeat(' ', $ind)) . "\n\n";
                     $cmds = $this->cmds;
              }
              $sp = str_repeat(' ', $l + 2);
              foreach($cmds as $name => $funcs) {
                     $inf = $name . substr($sp, strlen($name));
                     if (isset($funcs['inf'])) {
                            $inf .= $this->cli_wordwrap(call_user_func(array($this, $funcs['inf'])), $l, $sp) . "\n";
                            if (isset($funcs['arg'])) {
                                   $inf .= "\n";
                                   $inf .= $this->cli_help_get_args(array($this, $funcs['arg']), $l, $sp, true);
                                   $inf .= "\n";
                                   $inf .= $this->cli_help_get_args(array($this, $funcs['arg']), $l, $sp, false);
                            }
                     }
                     echo "$inf\n\n";
              }
              exit(0);
       }

Here is the call graph for this function:

Definition at line 344 of file clicommand.inc.

       {
              ksort($this->cmds);
              echo join(' ', array_keys($this->cmds)) . "\n";
       }

Here is the caller graph for this function:

CLICommand::cli_get_SP2 ( l1,
arg_inf 
)

Reimplemented in PharCommand.

Definition at line 218 of file clicommand.inc.

       {
              return str_repeat(' ', $l1 + 2 + 4 + 8);
       }

Here is the caller graph for this function:

CLICommand::cli_get_SP3 ( l1,
l2,
arg_inf 
)

Reimplemented in PharCommand.

Definition at line 223 of file clicommand.inc.

       {
              return str_repeat(' ', $l1 + 2 + 4 + 8 + 2 + $l2 + 2);
       }

Here is the caller graph for this function:

CLICommand::cli_help_get_args ( func,
l,
sp,
required 
) [private]

Definition at line 242 of file clicommand.inc.

       {
              $inf = "";
              foreach(call_user_func($func, $l, $sp) as $arg => $conf) {
                     if ((isset($conf['required']) && $conf['required']) != $required) {
                            continue;
                     }

                     if (strlen($arg)) {
                            $arg = "-$arg  ";
                     } else {
                            $arg = "... ";
                     }

                     $sp2 = $this->cli_get_SP2($l, $inf);
                     $l2  = strlen($sp2);
                     $inf .= $this->cli_wordwrap($sp . $arg . $conf['inf'], $l2, $sp2) . "\n";

                     if (isset($conf['select']) && count($conf['select'])) {
                            $ls = 0;
                            foreach($conf['select'] as $opt => $what) {
                                   $ls = max($ls, strlen($opt));
                            }
                            $sp3 = $this->cli_get_SP3($l, $ls, $inf);
                            $l3  = strlen($sp3);
                            foreach($conf['select'] as $opt => $what) {
                                   $inf .= $this->cli_wordwrap($sp2 . "  " . sprintf("%-${ls}s  ", $opt) . $what, $l3, $sp3) . "\n";
                            }
                     }
              }
              if (strlen($inf)) {
                     if ($required) {
                            return $sp . "Required arguments:\n\n" . $inf;
                     } else {
                            return $sp . "Optional arguments:\n\n". $inf;
                     }
              }
       }

Here is the call graph for this function:

Here is the caller graph for this function:

CLICommand::cli_wordwrap ( what,
l,
sp 
) [private]

Definition at line 233 of file clicommand.inc.

       {
              $p = max(79 - $l, 40);     // minimum length for paragraph
              $b = substr($what, 0, $l); // strip out initial $l
              $r = substr($what, $l);    // remainder
              $r = str_replace("\n", "\n".$sp, $r); // in remainder replace \n's
              return $b . wordwrap($r, $p, "\n".$sp);
       }

Here is the caller graph for this function:

static CLICommand::error ( msg,
exit_code = 1 
) [static]

Definition at line 95 of file clicommand.inc.

       {
              self::notice($msg);
              exit($exit_code);
       }

Here is the call graph for this function:

Here is the caller graph for this function:

static CLICommand::getArgTyps ( CLICommand cmdclass) [static]

Definition at line 141 of file clicommand.inc.

       {
              return self::getSubFuncs($cmdclass, 'cli_arg_', array('typ'));
       }

Here is the call graph for this function:

Here is the caller graph for this function:

static CLICommand::getCommands ( CLICommand cmdclass) [static]

Definition at line 136 of file clicommand.inc.

       {
              return self::getSubFuncs($cmdclass, 'cli_cmd_', array('arg','inf','run'));
       }

Here is the call graph for this function:

Here is the caller graph for this function:

static CLICommand::getSubFuncs ( CLICommand cmdclass,
prefix,
array $  subs 
) [static]

Definition at line 112 of file clicommand.inc.

       {
              $a = array();
              $r = new ReflectionClass($cmdclass);
              $l = strlen($prefix);

              foreach($r->getMethods() as $m) {
                     if (substr($m->name, 0, $l) == $prefix) {
                            foreach($subs as $sub) {
                                   $what = substr($m->name, $l+strlen($sub)+1);
                                   $func = $prefix . $sub . '_' . $what;
                                   $what = str_replace('_', '-', $what);
                                   if ($r->hasMethod($func)) {
                                          if (!isset($a[$what])) {
                                                 $a[$what] = array();
                                          }
                                          $a[$what][$sub] = /*$m->class . '::' .*/ $func;
                                   }
                            }
                     }
              }
              return $a;
       }

Here is the caller graph for this function:

static CLICommand::notice ( msg) [static]

Definition at line 90 of file clicommand.inc.

       {
              fprintf(STDERR, $msg);
       }

Here is the caller graph for this function:


Member Data Documentation

CLICommand::$argc [protected]

Definition at line 19 of file clicommand.inc.

CLICommand::$args = array() [protected]

Definition at line 22 of file clicommand.inc.

CLICommand::$argv [protected]

Definition at line 20 of file clicommand.inc.

CLICommand::$cmds = array() [protected]

Definition at line 21 of file clicommand.inc.

CLICommand::$typs = array() [protected]

Definition at line 23 of file clicommand.inc.


The documentation for this class was generated from the following file: