コード例 #1
0
 /**
  * @param string $dir  Target directory (defaults to ../out)
  * @param string $file Target filename (defaults to auto-generate from package name)
  *
  * @param bool   $dry_run
  *
  * @return string Path to created package
  * @throws Exception
  */
 public function pack($dir = null, $file = null, $dry_run = false)
 {
     if (!$this->hasExtensions() and !$this->hasLanguages() and !$this->hasScriptfile() and !$this->hasUpdateservers()) {
         throw new Exception('Pointless pack! (Missing some extensions / languages / updateservers / scriptfile)');
     }
     $file = Helper::toFileName(trim($file) == '' ? $this->getPkgFileName() : $file);
     $dir = Helper::toFilePath(trim($dir) == '' ? self::$default_target_dir : $dir);
     $path = Helper::toFilePath($dir . DIRECTORY_SEPARATOR . $file);
     if ($dry_run) {
         throw new Exception("DRY RUN:\n" . '$path = ' . $path . "\n" . '$this = ' . print_r($this, true));
     }
     $zip = new ZipArchive();
     if (!file_exists($dir) and !@mkdir($dir) && !is_dir($dir)) {
         throw new Exception("Cannot create/open directory for writing, dir = '{$dir}'");
     }
     if ($zip->open($path, ZipArchive::CREATE) !== true) {
         throw new Exception("Cannot create/open archive for writing, path = '{$path}'");
     }
     foreach ($this->getFiles() as $package_file) {
         if (!$zip->addFromString($package_file->getName(), $package_file->getData())) {
             throw new Exception("Cannot add file to archive, name = '{$package_file->getName()}', path = '{$path}'");
         }
     }
     $zip->setArchiveComment($this->getName() . ($this->getAuthor() != '' ? ' by ' . $this->getAuthor() : '') . "\n" . ($this->getDescription() != '' ? "\n" . $this->getDescription() . "\n" : '') . "\n================================================================\n" . 'Packed using ' . $this->getPackager() . "\n" . $this->getPackagerurl() . "\n");
     if (!$zip->close()) {
         throw new Exception("Cannot close archive, path = '{$path}'");
     }
     return $path;
 }
コード例 #2
0
 /**
  * @param string $name
  *
  * @return File
  */
 public function setName($name = null)
 {
     $this->name = Helper::toFileName($name);
     return $this;
 }