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); } }
/** * @inheritdoc */ public function run(&$cmdParams, &$params) { $controller = $this->controller; $res = true; $list = []; $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : ''; $dir = !empty($cmdParams[1]) ? $cmdParams[1] : '.'; $recursive = !empty($cmdParams[2]) ? $cmdParams[2] : false; $varName = !empty($cmdParams[3]) ? $cmdParams[3] : $connectionId . '.list'; if (empty($connectionId)) { Log::throwException('sftpList: Please specify a valid connection id'); } /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */ $connParams = $controller->getConnectionParams($connectionId); $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK); $controller->stdout(' Listing directory '); $controller->stdout($dir, Console::FG_CYAN); if (!$controller->dryRun) { // the getConnection method is provided by the SftpConnectReqs Behavior /** @noinspection PhpUndefinedMethodInspection */ /** @var $connection Net_SFTP|resource */ $connection = $controller->getConnection($connectionId); $sftpHelper = new SftpHelper($connectionId, $connection, $connParams); $list = $sftpHelper->nlist($dir, $recursive, true); // TODO: use SftpHelper to store the list } else { $controller->stdout(' [dry run]', Console::FG_YELLOW); } $params[$varName] = $list; $controller->stdout("\n"); return $res; }