protected function prepareCommand(Command\Definition $definition) { $commandClass = $definition->commandClass; /** @var Command $command */ $command = new $commandClass(); $command->setResponse($this->response); $command->setRequestMapper($this->requestMapper); $command->setIo($this); $commandOptions = $definition->optionsArray(); $commandState = new State(); $commandState->setIo($this); $commandState->commandClass = $commandClass; $requestState = new State(); $requestState->setIo($this); $requestState->commandClass = $commandClass; $this->requestMapper->readOptions($commandOptions, $commandState, $requestState); $this->commandStates[$definition->commandClass] = $commandState; $this->requestStates[$definition->commandClass] = $requestState; foreach ($commandOptions as $option) { $this->globalOptions[$option->name] = $option; // todo consider managing overlapping options if (!$commandState->hasProperty($option->name)) { continue; } $value = $commandState->{$option->name}; //var_dump($option->name, $value); $this->globalState->{$option->name} = $value; $command->{$option->name} = $value; if ($option->type === Option::TYPE_ENUM) { if ($value instanceof Command\Definition) { $command->{$option->name} = $this->prepareCommand($value); } } } return $command; }
/** * @param Io|null $fillFromIo * @return State * @return static */ public static function createState(Io $fillFromIo = null) { $commandClass = get_called_class(); /** @var State $state */ $state = null; if ($fillFromIo !== null) { $state = $fillFromIo->getRequestState($commandClass); if (!$state) { $state = new State(); } $state->setIo($fillFromIo); } else { $state = new State(); } $state->commandClass = $commandClass; return $state; }