/** * Copies a file. * * This method only copies the file if the origin file is newer than the target file. * * By default, if the target already exists, it is not overridden. * * @param string $originFile The original filename * @param string $targetFile The target filename * @param bool $override Whether to override an existing file or not * * @throws FileNotFoundException When originFile doesn't exist * * @return exit code */ public function copy($originFile, $targetFile, $override = false) { if (!$this->isFileExist($originFile)) { throw new FileNotFoundException(sprintf('Failed to copy "%s" because file does not exist.', $originFile), 0, null, $originFile); } $this->sshExec->exec(strtr('mkdir -p %targetDir% && cp -R %override% %originFile% %targetFile%', ['%originFile%' => $originFile, '%targetDir%' => dirname($targetFile), '%targetFile%' => $targetFile, '%override%' => $override ? '' : '-n'])); if ($this->sshExec->getLastReturnStatus() !== 0) { !$this->logger ?: $this->logger->error(sprintf('Error copy %s to %s', $originFile, $targetFile)); } return $this->sshExec->getLastReturnStatus(); }
/** * @param string $dir * * @return bool */ public function remove($filepath) { $commandLine = null; if ($this->isDirExist($filepath)) { $commandLine = sprintf('rm -fr %s', $filepath); } elseif ($this->isFileExist($filepath)) { $commandLine = sprintf('rm -%s', $filepath); } else { return 0; } $this->sshExec->exec($commandLine); if ($this->sshExec->getLastReturnStatus() !== 0) { !$this->logger ?: $this->logger->error(sprintf('Error remove %s', $filepath)); } return $this->sshExec->getLastReturnStatus() === 0; }
/** * Executes without output buffering symfony console using ssh . * * @param string $symfonyConsolePath * @param string $commandAndOptions Command name and options * @param OutputInterface|null $output * * @return string */ public function exec($symfonyConsolePath, $commandNameAndOptions, $env, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) { $commandLine = strtr('php %symfony_console_path% %command_name_and_options% --env=%env% %verbosity_option%', ['%symfony_console_path%' => $symfonyConsolePath, '%command_name_and_options%' => $commandNameAndOptions, '%env%' => $env, '%verbosity_option%' => $this->getVerbosityOption($verbosity)]); return $this->sshExec->exec($commandLine); }