コード例 #1
0
ファイル: SftpGetCommand.php プロジェクト: giovdk21/deployii
 private function _get($remotePath, $destRelPathArr = [])
 {
     // Remove the first element from the $destRelPathArr since it corresponds to the destination path
     $destSubFoldersArr = $destRelPathArr;
     array_shift($destSubFoldersArr);
     $destRelPath = implode(DIRECTORY_SEPARATOR, $destSubFoldersArr);
     $localFolderName = basename($this->_destPath);
     $localFolderName .= !empty($destRelPath) ? DIRECTORY_SEPARATOR . $destRelPath : '';
     $destPath = $this->_destPath;
     $destPath .= !empty($destSubFoldersArr) ? DIRECTORY_SEPARATOR . $destRelPath : '';
     $this->controller->stdout("\n - {$localFolderName}\t <= {$remotePath}");
     if (!$this->controller->dryRun) {
         if ($this->_sftpHelper->isDir($remotePath)) {
             if (!is_dir($this->_destPath)) {
                 $this->controller->stdout("\n");
                 Log::throwException('sftpGet: remotePath is a directory, therefore destination has to be a directory too');
             }
             $list = $this->_sftpHelper->nlist($remotePath);
             $remoteDirName = basename($remotePath);
             $newSubDir = $destPath . DIRECTORY_SEPARATOR . $remoteDirName;
             $destRelPathArr[] = $remoteDirName;
             // Creating the sub-directory before recursion to allow creation of empty directories
             // (excluding the first level as it corresponds to the destination path)
             if (count($destRelPathArr) > 1 && !is_dir($newSubDir)) {
                 FileHelper::createDirectory($newSubDir);
             }
             foreach ($list as $item) {
                 if ($item !== '.' && $item !== '..') {
                     $this->_get($remotePath . '/' . $item, $destRelPathArr);
                 }
             }
         } elseif (!empty($destSubFoldersArr) || $this->_sftpHelper->fileExists($remotePath)) {
             // if $destSubFoldersArr is not empty it means that we've got the list of files
             // from the nlist command on the remote directory; therefore we don't need to check
             // if the file exists
             $destFile = $destPath;
             if (is_dir($destFile)) {
                 $destFile = $destPath . DIRECTORY_SEPARATOR . basename($remotePath);
             }
             if ($this->_overwrite || !file_exists($destFile)) {
                 $this->_sftpHelper->get($remotePath, $destFile);
             } else {
                 $this->controller->stdout(' [skipped]', Console::FG_PURPLE);
             }
         } else {
             $this->controller->stdout("\n");
             $this->controller->warn('Not found: ' . $remotePath);
         }
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
 }
コード例 #2
0
ファイル: SftpMvCommand.php プロジェクト: giovdk21/deployii
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $res = true;
     $taskRunner = $this->taskRunner;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $pathFrom = !empty($cmdParams[1]) ? $taskRunner->parsePath($cmdParams[1]) : '';
     $pathTo = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : '';
     $overwrite = !empty($cmdParams[3]) ? $cmdParams[3] : false;
     // TODO: check if overwrite is needed after implementing the FTP support
     if (empty($pathFrom) || empty($pathTo)) {
         throw new Exception('sftpMv: Origin and destination cannot be empty');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $this->controller->getConnectionParams($connectionId);
     $this->controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $this->controller->stdout(" Move (overwrite: " . ($overwrite ? 'yes' : 'no') . ") \n " . $pathFrom . " to \t " . $pathTo);
     $insidePathTo = $pathTo . '/' . basename($pathFrom);
     if (!$this->controller->dryRun) {
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $this->controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         if (!$sftpHelper->fileExists($pathFrom)) {
             $this->controller->stdout("\n");
             $this->controller->stderr("Not found: {$pathFrom}", Console::FG_RED);
         } else {
             if ($sftpHelper->isFile($pathFrom) && $sftpHelper->isDir($pathTo)) {
                 $pathTo = $insidePathTo;
             }
             if (!$overwrite && $sftpHelper->isDir($pathFrom) && $sftpHelper->isDir($pathTo)) {
                 if (!$sftpHelper->fileExists($insidePathTo)) {
                     // not overwriting; copy the source directory into the destination folder:
                     $res = $sftpHelper->rename($pathFrom, $insidePathTo);
                 } else {
                     $this->controller->stdout("\n");
                     $this->controller->warn("Destination directory {$insidePathTo} already exists; not overwriting");
                 }
             } elseif (!$overwrite && $sftpHelper->isFile($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 $this->controller->stdout("\n");
                 $this->controller->warn("Destination file {$pathTo} already exists; not overwriting");
             } elseif ($sftpHelper->isDir($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 $this->controller->stdout("\n");
                 $this->controller->stderr("Trying to move a directory to a file: {$pathTo}", Console::FG_RED);
             } elseif (!$sftpHelper->fileExists($pathTo) || $overwrite && $sftpHelper->isDir($pathFrom) && $sftpHelper->isDir($pathTo) || $overwrite && $sftpHelper->isFile($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 // if destination exists, overwrite it with the source file/dir
                 // note: if pathTo is a directory, it has to be empty in order to be overwritten
                 try {
                     $res = $sftpHelper->rename($pathFrom, $pathTo);
                 } catch (ErrorException $e) {
                     $this->controller->stdout("\n");
                     $this->controller->warn($e->getMessage());
                 }
                 if (!$res) {
                     // Note: using sftp, renaming a directory to another does not work even if destination is empty
                     $this->controller->stdout("\n");
                     $this->controller->stderr("Failed moving {$pathFrom} to {$pathTo}", Console::FG_RED);
                 }
             }
         }
         $sftpHelper->flushCache();
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $this->controller->stdout("\n");
     return $res;
 }
コード例 #3
0
ファイル: SftpPutCommand.php プロジェクト: giovdk21/deployii
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $controller = $this->controller;
     $taskRunner = $this->taskRunner;
     $res = true;
     $toBeUploaded = [];
     $this->_connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : $this->_connectionId;
     $srcList = !empty($cmdParams[1]) ? $cmdParams[1] : [];
     $this->_destDir = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : '';
     $this->_srcBaseDir = !empty($cmdParams[3]) ? $taskRunner->parsePath($cmdParams[3]) : '';
     $this->_overwrite = !empty($cmdParams[4]) ? $cmdParams[4] : $this->_overwrite;
     $this->_options = !empty($cmdParams[5]) ? $cmdParams[5] : $this->_options;
     if (empty($this->_connectionId) || empty($this->_destDir)) {
         Log::throwException('sftpPut: Please specify a valid connection id and directory');
     }
     if (empty($srcList) || empty($this->_srcBaseDir)) {
         Log::throwException('sftpPut: srcList and srcBaseDir cannot be empty');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($this->_connectionId);
     $controller->stdout(" " . $this->_connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $controller->stdout(' Uploading files to ');
     $controller->stdout($this->_destDir, Console::FG_CYAN);
     if ($controller->dryRun) {
         $controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     if (is_string($srcList) && $srcList === '*') {
         $list = glob($this->_srcBaseDir . DIRECTORY_SEPARATOR . '*');
         foreach ($list as $srcFullPath) {
             $srcRelPath = substr($srcFullPath, strlen($this->_srcBaseDir) + 1);
             $toBeUploaded[$srcRelPath] = $srcFullPath;
         }
     } else {
         // if $srcList is specified but it is a string, we convert it to an array
         if (!empty($srcList) && is_string($srcList)) {
             $srcList = explode(',', $srcList);
         }
         foreach ($srcList as $srcPath) {
             $parsedPath = $taskRunner->parseStringParams(trim($srcPath));
             $toBeUploaded[$parsedPath] = $this->_srcBaseDir . DIRECTORY_SEPARATOR . $parsedPath;
         }
     }
     if (!$controller->dryRun) {
         // the getConnection method is provided by the SftpConnectReqs Behavior
         /** @noinspection PhpUndefinedMethodInspection */
         $this->_connection = $controller->getConnection($this->_connectionId);
         $this->_sftpHelper = new SftpHelper($this->_connectionId, $this->_connection, $connParams);
         if (!is_dir($this->_srcBaseDir) || !$this->_sftpHelper->isDir($this->_destDir)) {
             $this->controller->stdout("\n");
             Log::throwException('sftpPut: Base and destination have to be directories');
         }
     }
     foreach ($toBeUploaded as $srcRelPath => $srcFullPath) {
         if (file_exists($srcFullPath)) {
             $this->_put($srcFullPath, $srcRelPath);
         } else {
             $this->controller->stdout("\n");
             $this->controller->stderr("{$srcFullPath} does not exists!\n", Console::FG_RED);
         }
     }
     if (!$controller->dryRun) {
         $this->_sftpHelper->flushCache();
     }
     $controller->stdout("\n");
     return $res;
 }