コード例 #1
0
ファイル: Zip.php プロジェクト: Leemo/deployer
 /**
  * Execute task and return report info
  *
  * @return \Deployer\Task
  */
 public function execute()
 {
     $zipDir = \Deployer::applyGlobalParams($this->param('dir'));
     $zipFile = \Deployer::applyGlobalParams($this->param('file'));
     if (is_file($zipFile)) {
         unlink($zipFile);
     }
     if (!is_dir($zipDir)) {
         throw new \Deployer\TaskException('"dir" must be a valid directory');
     }
     $zip = new \ZipArchive();
     if (!$zip->open($zipFile, \ZIPARCHIVE::CREATE)) {
         throw new \Deployer\TaskException('can\'t create zip file');
     }
     $dirs = \Deployer::sortByDepth(\Deployer::dirs($zipDir, SORT_ASC));
     foreach ($dirs as $dir) {
         $newDir = $this->_localPath($zipDir, $dir);
         $zip->addEmptyDir($newDir);
     }
     $files = \Deployer::files($zipDir);
     foreach ($files as $file) {
         $newFile = $this->_localPath($zipDir, $file);
         $zip->addFile($file, $newFile);
     }
     $zip->close();
     if (is_file($zipFile)) {
         return $this->info(sprintf('file created; size: %s', \Deployer::size(filezize($zipFile))));
     }
     return $this;
 }