tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file release_zip_creator.class.php

Documentation is available at release_zip_creator.class.php

  1. <?php
  2.  
  3. // $Id$
  4.  
  5. /**
  6.  * Contains class Tangra_Module_Installer
  7.  *
  8.  * @package  tangra_lib
  9.  * @subpackage  modules_manager
  10.  */
  11.  
  12.  
  13. /**
  14.  * Creates ZIP archive for module release
  15.  *
  16.  * @package  tangra_lib
  17.  * @subpackage  modules_manager
  18.  */
  19. class Release_Zip_Creator extends Tangra_Class {
  20.     /**
  21.      * Creates ZIP archive for module release
  22.      *
  23.      * @param string $source_dir Source dir that contains release files
  24.      * @param string $file_path Path to target Zip file to be created
  25.      * @param integer $cut Path of source files will be cut from this pos (i.e. path in archive will be what is after $cut)
  26.      * @param string $prefix Paths in archive will be prefixed with this value
  27.      * @param octal $chmod Created Zip file will be chmoded to this value
  28.      * @return boolean  Returns true on success, on failure - false
  29.      */
  30.     public static function generate_file($source_dir$file_path$cut$prefix$chmod{
  31.         $ret false;
  32.  
  33.         if (file_exists($source_dir&& is_dir($source_dir)) {
  34.             $zip new ZipArchive();
  35.             if ($zip->open($file_pathZIPARCHIVE::CREATE=== true{
  36.                 $iterator new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source_dir));
  37.  
  38.                 $cut_from_pos strlen($cut);
  39.                 $c 0;
  40.  
  41.                 foreach ($iterator as $key=>$value{
  42.                     if (!fnmatch('*.svn*'$key)) {
  43.                         if ($c 100//workaround max file descriptors limitation
  44.                             $zip->close();
  45.                             $zip new ZipArchive();
  46.                             $zip->open($file_path);
  47.                             $c 0;
  48.                         }
  49.                         $c++;
  50.  
  51.                         if (!$zip->addFile(realpath($key)$prefix.substr($key$cut_from_pos))) {
  52.                             $zip->close();
  53.                             if (file_exists($file_path)) {
  54.                                 unlink($file_path);
  55.                             }
  56.                             throw new Tangra_Exception("Cannot add $key to archive");
  57.                         }
  58.                     }
  59.                 }
  60.  
  61.                 $zip->close();
  62.                 chmod($file_path$chmod);
  63.                 $ret true;
  64.             else {
  65.                 throw new Tangra_Exception('Cannot create '.$file_path);
  66.             }
  67.  
  68.         else {
  69.             throw new Tangra_Exception('$source_dir '.$source_dir.' does not exist or it is not a directory.');
  70.         }
  71.  
  72.  
  73.         return $ret;
  74.     }
  75. }