コード例 #1
0
ファイル: Rsync.php プロジェクト: albertofem/rsync-lib
 /**
  * Gets command generated for this current
  * rsync configuration. You can use it to test
  * or execute it later without using the sync method
  *
  * @param $origin
  * @param $target
  *
  * @return Command
  */
 public function getCommand($origin, $target)
 {
     $command = new Command($this->executable);
     if ($this->skipNewerFiles) {
         $command->addOption("u");
     }
     if ($this->followSymLinks) {
         $command->addOption("L");
     }
     if ($this->dryRun) {
         $command->addOption("n");
     }
     if ($this->verbose) {
         $command->addOption("v");
     }
     if ($this->compression) {
         $command->addOption("z");
     }
     // add any optional options we've specified
     $extra_options = $this->getOptionalParameters();
     if (!empty($extra_options)) {
         // if the extra options were given as a flat string, then convert it to an array
         if (is_string($extra_options)) {
             $extra_options = str_split($extra_options);
         }
         // add each extra option we've defined.
         if (is_array($extra_options)) {
             foreach ($extra_options as $option) {
                 $command->addOption($option);
             }
         }
     }
     if ($this->times) {
         $command->addArgument('times');
     }
     if ($this->deleteFromTarget) {
         $command->addArgument('delete');
     }
     if ($this->removeSource) {
         $command->addArgument('remove-source-files');
     }
     if ($this->deleteExcluded) {
         $command->addArgument('delete-excluded');
     }
     if ($this->info) {
         $command->addArgument('info', $this->info);
     }
     if ($this->compareDest) {
         $command->addArgument('compare-dest', $this->compareDest);
     }
     if (!empty($this->exclude)) {
         foreach ($this->exclude as $excluded) {
             $command->addArgument('exclude', $excluded);
         }
     }
     if (!empty($this->excludeFrom)) {
         $command->addArgument('exclude-from', $this->excludeFrom);
     }
     if ($this->archive) {
         $command->addOption("a");
     }
     if (!$this->archive && $this->recursive) {
         $command->addOption("r");
     }
     if (!is_null($this->ssh)) {
         $ssh = $this->ssh->getConnectionOptions();
         $command->addArgument("rsh", $ssh);
     }
     if (is_null($this->ssh)) {
         $command->addParameter($origin);
         $command->addParameter($target);
     } elseif ($this->remoteOrigin) {
         $command->addParameter($this->ssh->getHostConnection() . ":" . $origin);
         $command->addParameter($target);
     } else {
         $command->addParameter($origin);
         $command->addParameter($this->ssh->getHostConnection() . ":" . $target);
     }
     return $command;
 }
コード例 #2
0
ファイル: CommandTest.php プロジェクト: chaos-drone/exec
 public function testCommandClass()
 {
     $commandLine = "echo \"Hello world!\"";
     $c = new Command($commandLine);
     $this->assertEquals($commandLine, $c);
     $stdOut = "./stdout.log";
     $c->setStdOut($stdOut);
     $this->assertEquals($stdOut, $c->getStdOut(), "stdout setter and getter are ok.");
     $stdErr = "./stderr.log";
     $c->setStdErr($stdErr);
     $this->assertEquals($stdErr, $c->getStdErr(), "stderr setter and getter are ok.");
     $c = new Command($commandLine);
     $c->runInBackground();
     if (substr(php_uname(), 0, 7) == "Windows") {
         $stdOutNul = ">NUL";
         $stdErrNul = "2>NUL";
     } else {
         $stdOutNul = "/dev/null";
         $stdErrNul = "2>/dev/null";
     }
     $this->assertEquals($stdOutNul, $c->getStdOut(), "If stdout is not set set it to {$stdOutNul} when running in background.");
     $this->assertEquals($stdErrNul, $c->getStdErr(), "If stderr is not set set it to {$stdErrNul} when running in background.");
     $c = new Command($commandLine);
     $c->setStdErr($stdErr);
     $c->setStdOut($stdOut);
     $c->runInBackground();
     $this->assertEquals($stdOut, $c->getStdOut(), "If stdout is set don't overwrite it when running in background.");
     $this->assertEquals($stdErr, $c->getStdErr(), "If stderr is set don't overwrite it when running in background.");
     $arg1 = "arg1";
     $arg2 = "arg2";
     $arg3 = "arg3";
     $c->addArgument($arg1);
     $c->addArgument($arg3);
     $expectedLine = "{$commandLine} {$arg1} {$arg3}";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends arguments.");
     $c->addArgument($arg2, 2);
     $expectedLine = "{$commandLine} {$arg1} {$arg2} {$arg3}";
     $this->assertEquals($expectedLine, (string) $c, "Command properly insertets arguments at given position.");
     $arg2_1 = "arg2_1";
     $c->addArgument($arg2_1, 2, true);
     $expectedLine = "{$commandLine} {$arg1} {$arg2_1} {$arg3}";
     $this->assertEquals($expectedLine, (string) $c, "Command properly replaces arguments.");
     $c = new Command($commandLine);
     $c->addArgument($arg3, 3);
     $c->addArgument($arg1, 1);
     $c->addArgument($arg2, 2);
     $expectedLine = "{$commandLine} {$arg1} {$arg2} {$arg3}";
     $this->assertEquals($expectedLine, (string) $c, "Command properly insertets arguments added in mixed order.");
     $c = new Command($commandLine);
     $c->addOption("-c");
     $expectedLine = "{$commandLine} -c";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends no-value option.");
     $c->removeOption("-c");
     $this->assertEquals($commandLine, (string) $c, "Command properly removesg options.");
     $c->addOption("-c:", "value_c");
     $expectedLine = "{$commandLine} -c \"value_c\"";
     $this->assertEquals($expectedLine, (string) $c, "Commadn properly appends options with required value.");
     $c = new Command($commandLine);
     $c->addOption("-c::");
     $expectedLine = "{$commandLine} -c";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends optional value option without value set.");
     $c = new Command($commandLine);
     $c->addOption("-c::", "value_c");
     $expectedLine = "{$commandLine} -c=\"value_c\"";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends optional value option.");
     $c = new Command($commandLine);
     $c->addOption("-c", "value_c");
     $expectedLine = "{$commandLine} -c";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends no-value option without value even if value is set.");
     $c = new Command($commandLine);
     $c->addOption("-a");
     $c->addOption("-b");
     $expectedLine = "{$commandLine} -a -b";
     $this->assertEquals($expectedLine, (string) $c, "Command properly appends multiple options");
 }
コード例 #3
0
ファイル: Rsync.php プロジェクト: no2key/rsync-lib
 /**
  * Gets command generated for this current
  * rsync configuration. You can use it to test
  * or execute it later without using the sync method
  *
  * @param $origin
  * @param $target
  *
  * @return Command
  */
 public function getCommand($origin, $target)
 {
     $command = new Command($this->executable);
     if ($this->skipNewerFiles) {
         $command->addOption("u");
     }
     if ($this->followSymLinks) {
         $command->addOption("L");
     }
     if ($this->dryRun) {
         $command->addOption("n");
     }
     if ($this->verbose) {
         $command->addOption("v");
     }
     if ($this->times) {
         $command->addArgument('times');
     }
     if ($this->deleteFromTarget) {
         $command->addArgument('delete');
     }
     if ($this->deleteExcluded) {
         $command->addArgument('delete-excluded');
     }
     if (!empty($this->exclude)) {
         foreach ($this->exclude as $excluded) {
             $command->addArgument('exclude', $excluded);
         }
     }
     if ($this->archive) {
         $command->addOption("a");
     }
     if (!$this->archive && $this->recursive) {
         $command->addOption("r");
     }
     if (!is_null($this->ssh)) {
         $ssh = $this->ssh->getConnectionOptions();
         $command->addArgument("rsh", $ssh);
     }
     $command->addParameter($origin);
     if (is_null($this->ssh)) {
         $command->addParameter($target);
     } else {
         $command->addParameter($this->ssh->getHostConnection() . ":" . $target);
     }
     return $command;
 }
コード例 #4
0
ファイル: SSH.php プロジェクト: no2key/rsync-lib
 /**
  * Gets commands for this SSH connection
  *
  * @param bool $hostConnection
  *
  * @return string
  *
  * @throws \InvalidArgumentException If you don't specify a SSH username or host
  */
 public function getCommand($hostConnection = true)
 {
     if (is_null($this->username)) {
         throw new \InvalidArgumentException("You must specify a SSH username");
     }
     if (is_null($this->host)) {
         throw new \InvalidArgumentException("You must specify a SSH host to connect");
     }
     $command = new Command($this->executable);
     if ($this->port != 22) {
         $command->addArgument("p", $this->port);
     }
     if (!is_null($this->publicKey)) {
         $command->addArgument("i", $this->publicKey);
     }
     if ($hostConnection) {
         $command->addParameter($this->getHostConnection());
     }
     return $command;
 }