/** * @inheritdoc */ public function run(&$cmdParams, &$params) { // TODO: add log info $res = true; $taskRunner = $this->taskRunner; $toBeParsed = []; $fileList = !empty($cmdParams[0]) ? $cmdParams[0] : []; $this->_replaceList = !empty($cmdParams[1]) ? $cmdParams[1] : $this->_replaceList; $baseDir = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : ''; $options = !empty($cmdParams[3]) ? $cmdParams[3] : []; if (!empty($baseDir) && (!empty($baseDir) && !is_dir($baseDir))) { Log::throwException('replaceInFiles: baseDir has to be a directory'); } // if $fileList is specified but it is a string, we convert it to an array if (!empty($fileList) && is_string($fileList)) { $fileList = explode(',', $fileList); } foreach ($fileList as $srcPath) { $parsedPath = $taskRunner->parseStringAliases(trim($srcPath)); if (!empty($baseDir)) { $toBeParsed[$parsedPath] = $baseDir . DIRECTORY_SEPARATOR . $parsedPath; } else { $toBeParsed[] = $parsedPath; } } $this->controller->stdout("Replacing in files: "); foreach ($toBeParsed as $srcRelPath => $srcFullPath) { if (file_exists($srcFullPath)) { if (is_dir($srcFullPath)) { $files = FileHelper::findFiles($srcFullPath, $options); foreach ($files as $foundPath) { $this->_replace($foundPath); } } elseif (FileHelper::filterPath($srcFullPath, $options)) { $this->_replace($srcFullPath); } } else { $this->controller->stderr("\n{$srcFullPath} does not exists!\n", Console::FG_RED); } } $this->controller->stdout("\n"); return $res; }
public static function saeCopyDirectory($src, $dst, $options = []) { $handle = opendir($src); if ($handle === false) { throw new InvalidParamException('Unable to open directory: ' . $src); } if (!isset($options['basePath'])) { // this should be done only once $options['basePath'] = realpath($src); } while (($file = readdir($handle)) !== false) { if ($file === '.' || $file === '..') { continue; } $from = $src . DIRECTORY_SEPARATOR . $file; $to = $dst . '/' . $file; if (FileHelper::filterPath($from, $options)) { if (isset($options['beforeCopy']) && !call_user_func($options['beforeCopy'], $from, $to)) { continue; } if (is_file($from)) { copy($from, $to); } else { static::saeCopyDirectory($from, $to, $options); } if (isset($options['afterCopy'])) { call_user_func($options['afterCopy'], $from, $to); } } } closedir($handle); }
/** * @inheritdoc */ public function run(&$cmdParams, &$params) { $res = true; $taskRunner = $this->taskRunner; $toBeAdded = []; $srcList = !empty($cmdParams[0]) ? $cmdParams[0] : []; $destFile = !empty($cmdParams[1]) ? $taskRunner->parsePath($cmdParams[1]) : ''; $srcBaseDir = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : ''; $format = !empty($cmdParams[3]) ? strtolower($cmdParams[3]) : self::CMP_GZ; $options = !empty($cmdParams[4]) ? $cmdParams[4] : []; if (!empty($srcBaseDir) && !is_dir($srcBaseDir)) { Log::throwException('compress: srcBaseDir has to be a directory'); } if (empty($destFile) || file_exists($destFile) && is_dir($destFile)) { Log::throwException('compress: Destination has to be a file'); } switch ($format) { case self::CMP_NONE: $extension = '.tar'; $compression = \Phar::NONE; $doCompress = false; break; case self::CMP_GZ: $extension = '.tar.gz'; $compression = \Phar::GZ; $doCompress = true; break; case self::CMP_BZ2: $extension = '.tar.bz2'; $compression = \Phar::BZ2; $doCompress = true; break; default: $extension = ''; $compression = false; $doCompress = false; Log::throwException('compress: Invalid format specified: ' . $format); break; } // 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->parseStringAliases(trim($srcPath)); if (!empty($srcBaseDir)) { $toBeAdded[$parsedPath] = $srcBaseDir . DIRECTORY_SEPARATOR . $parsedPath; } else { $toBeAdded[] = $parsedPath; } } $this->controller->stdout("Creating archive: "); $this->controller->stdout($destFile, Console::FG_BLUE); $archive = null; $destDir = dirname($destFile); $destBaseFile = $destDir . DIRECTORY_SEPARATOR . basename($destFile, '.tar'); if (!$this->controller->dryRun) { if (!is_dir($destDir)) { FileHelper::createDirectory($destDir); } @unlink($destFile); @unlink($destBaseFile); try { $archive = new \PharData($destFile); } catch (\Exception $e) { Log::throwException($e->getMessage()); } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } $this->controller->stdout("\n"); $this->controller->stdout("Adding to archive: "); foreach ($toBeAdded as $srcRelPath => $srcFullPath) { if (file_exists($srcFullPath)) { $this->controller->stdout("\n - " . $srcFullPath); if (!$this->controller->dryRun) { if (is_dir($srcFullPath)) { $files = FileHelper::findFiles($srcFullPath, $options); $archive->buildFromIterator(new ArrayIterator($files), !empty($srcBaseDir) ? $srcBaseDir : $srcFullPath); } elseif (FileHelper::filterPath($srcFullPath, $options)) { $archive->addFile($srcFullPath, !empty($srcBaseDir) ? $srcRelPath : basename($srcFullPath)); } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } } else { $this->controller->stderr("\n{$srcFullPath} does not exists!\n", Console::FG_RED); } } $this->controller->stdout("\n"); if ($doCompress) { $this->controller->stdout("Compressing archive: "); $this->controller->stdout($destBaseFile . $extension, Console::FG_CYAN); if (!$this->controller->dryRun) { @unlink($destBaseFile . $extension); try { $archive->compress($compression, $extension); } catch (\Exception $e) { Log::throwException($e->getMessage()); } @unlink($destFile); } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } $this->controller->stdout("\n"); } return $res; }
private function _put($srcFullPath, $srcRelPath) { $destRelPath = $this->_destDir . '/' . $srcRelPath; $this->controller->stdout("\n - " . $srcRelPath . "\t => " . $destRelPath); if (!$this->controller->dryRun) { if (is_dir($srcFullPath)) { $files = FileHelper::findFiles($srcFullPath, $this->_options); foreach ($files as $foundPath) { $relativePath = substr($foundPath, strlen($this->_srcBaseDir) + 1); $this->_sftpHelper->mkdir($destRelPath); $this->_put($foundPath, $relativePath, $this->_srcBaseDir, $this->_destDir, $this->_options); } } elseif (FileHelper::filterPath($srcFullPath, $this->_options)) { $this->_sftpHelper->mkdir(dirname($destRelPath), -1, true); if ($this->_overwrite || !$this->_sftpHelper->fileExists($destRelPath)) { $res = $this->_sftpHelper->put($destRelPath, $srcFullPath); if (!$res) { Log::logger()->addError('sftpPut: error uploading file {from} to {to}', ['from' => $srcFullPath, 'to' => $destRelPath]); } } else { $this->controller->stdout(' [skipped]', Console::FG_PURPLE); } } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } }