/** * If not overriden, will execute the command specified * as the first argument * * Commands must be defined as methods named after the * command, prefixed with execute (eg. create -> executeCreate) * * @param array $args * @param array $options */ public function execute(array $args, array $options = array()) { if (!count($args)) { throw new ConsoleException("Missing subcommand name"); } $command = ucfirst(Utils::camelize(array_shift($args))); $methodName = "execute{$command}"; if (!method_exists($this, $methodName)) { throw new ConsoleException("Command '{$command}' does not exist"); } $method = new ReflectionMethod($this, $methodName); $params = Utils::computeFuncParams($method, $args, $options); return $method->invokeArgs($this, $params); }
/** * Creates an Help object from a class subclassing Command * * @param string $name * @param string $subCommand * @return Help */ public static function fromCommandClass($name, $subCommand = null) { $prefix = 'execute'; $class = new ReflectionClass($name); if ($subCommand) { $method = $prefix . ucfirst(Utils::camelize($subCommand)); if (!$class->hasMethod($method)) { throw new ConsoleException("Sub command '{$subCommand}' of '{$name}' does not exist"); } return new Help($class->getMethod($method)->getDocComment()); } $help = new Help($class->getDocComment()); foreach ($class->getMethods() as $method) { if (strlen($method->getName()) > strlen($prefix) && substr($method->getName(), 0, strlen($prefix)) === $prefix) { $help->subCommands[] = Utils::dashized(substr($method->getName(), strlen($prefix))); } } return $help; }
public function testCamelize() { $this->assertEquals('fooBar', Utils::camelize('foo-bar')); }