/** * @param ConnectionInterface $connection * @param CommandInterface $command * * @return ResultInterface */ public function exec(ConnectionInterface $connection, CommandInterface $command) { $command->execBegin(); fwrite($connection->getConnection(), $command->asString() . PHP_EOL); usleep(500000); $connection->history()->add($command); $commandLineLabel = null; if ($connection instanceof TerminalInterface) { $commandLineLabel = $connection->getCommandLineLabel(); } $content = self::read($connection->getConnection(), 8192, $commandLineLabel); $result = $this->createResult($content, 0, $command); $command->execEnd(); return $result; }
/** * @param ConnectionInterface $connection * @param CommandInterface $command * * @return ResultInterface * @throws \RuntimeException */ public function exec(ConnectionInterface $connection, CommandInterface $command) { $command->execBegin(); $stream = @ssh2_exec($connection->getConnection(), $command->asString(), $command instanceof ExecCommandInterface ? $command->getPty() : null, $command->getEnv(), $command->getWidth(), $command->getHeight(), $command->getWidthHeightType()); $connection->history()->add($command); if (!$stream) { throw new \RuntimeException(sprintf('Can not execute command `%s`', $command->asString())); } $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); stream_set_blocking($stream, 1); stream_set_blocking($errorStream, 1); $error = stream_get_contents($errorStream); $content = stream_get_contents($stream); fclose($errorStream); fclose($stream); if ($error) { $result = $this->createResult($error, true, $command); } else { $result = $this->createResult($content, false, $command); } $command->execEnd(); return $result; }
/** * @param CommandInterface $command * * @return mixed */ public function add(CommandInterface $command) { $this->history[$command->asString()] = $command; return $this; }
/** * @param $result * @param $isError * @param CommandInterface $command * * @return Result */ protected function createResult($result, $isError, CommandInterface $command) { $result = new Result($result, $isError, $command); $command->setResult($result); return $result; }