Ejemplo n.º 1
0
 /**
  * @param string $path Path to file
  * @param string $name Override file name
  *
  * @return File
  * @throws Exception
  */
 public static function createFromPath($path, $name = null)
 {
     $path = Helper::toFilePath($path);
     if (!file_exists($path)) {
         throw new Exception("File '{$path}' does not exist");
     }
     if (($data = file_get_contents($path)) === false) {
         throw new Exception("Cannot get contents of file '{$path}'");
     }
     return new static($name != null ? $name : $path, $data);
 }
Ejemplo n.º 2
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;
 }