ConsoleKit\Console::addCommand PHP Method

addCommand() public method

Registers a command
public addCommand ( callback $callback, string $alias = null, boolean $default = false ) : Console
$callback callback Associated class name, function name, Command instance or closure
$alias string Command name to be used in the shell
$default boolean True to set the command as the default one
return Console
    public function addCommand($callback, $alias = null, $default = false)
    {
        if ($alias instanceof \Closure && is_string($callback)) {
            list($alias, $callback) = array($callback, $alias);
        }
        if (is_array($callback) && is_string($callback[0])) {
            $callback = implode('::', $callback);
        }
        $name = '';
        if (is_string($callback)) {
            $name = $callback;
            if (is_callable($callback)) {
                if (strpos($callback, '::') !== false) {
                    list($classname, $methodname) = explode('::', $callback);
                    $name = Utils::dashized($methodname);
                } else {
                    $name = strtolower(trim(str_replace('_', '-', $name), '-'));
                }
            } else {
                if (substr($name, -7) === 'Command') {
                    $name = substr($name, 0, -7);
                }
                $name = Utils::dashized(basename(str_replace('\\', '/', $name)));
            }
        } else {
            if (is_object($callback) && !$callback instanceof Closure) {
                $classname = get_class($callback);
                if (!$callback instanceof Command) {
                    throw new ConsoleException("'{$classname}' must inherit from 'ConsoleKit\\Command'");
                }
                if (substr($classname, -7) === 'Command') {
                    $classname = substr($classname, 0, -7);
                }
                $name = Utils::dashized(basename(str_replace('\\', '/', $classname)));
            } else {
                if (!$alias) {
                    throw new ConsoleException("Commands using closures must have an alias");
                }
            }
        }
        $name = $alias ?: $name;
        $this->commands[$name] = $callback;
        if ($default) {
            $this->defaultCommand = $name;
        }
        return $this;
    }

Usage Example

Beispiel #1
0
 /**
  * Registers a callback to call when a command is
  * executed
  *
  * @param string $command
  * @param callback $callback
  */
 public static function register($command, $callback)
 {
     self::$console->addCommand($callback, $command);
 }