[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: ZipTask.php 3076 2006-12-18 08:52:12Z fabien $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://phing.info>. 20 */ 21 22 require_once 'phing/tasks/system/MatchingTask.php'; 23 include_once 'phing/util/SourceFileScanner.php'; 24 include_once 'phing/mappers/MergeMapper.php'; 25 include_once 'phing/util/StringHelper.php'; 26 include_once 'phing/lib/Zip.php'; 27 28 /** 29 * Creates a zip archive using PEAR Archive_Zip (which is presently unreleased 30 * and included with Phing). 31 * 32 * @author Michiel Rook <michiel.rook@gmail.com> 33 * @version $Revision: 1.2 $ 34 * @package phing.tasks.ext 35 * @since 2.1.0 36 */ 37 class ZipTask extends MatchingTask { 38 39 private $zipFile; 40 private $baseDir; 41 42 private $filesets = array(); 43 private $fileSetFiles = array(); 44 45 /** 46 * Add a new fileset. 47 * @return FileSet 48 */ 49 public function createFileSet() { 50 $this->fileset = new FileSet(); 51 $this->filesets[] = $this->fileset; 52 return $this->fileset; 53 } 54 55 /** 56 * Set is the name/location of where to create the zip file. 57 * @param PhingFile $destFile The output of the zip 58 */ 59 public function setDestFile(PhingFile $destFile) { 60 $this->zipFile = $destFile; 61 } 62 63 /** 64 * This is the base directory to look in for things to zip. 65 * @param PhingFile $baseDir 66 */ 67 public function setBasedir(PhingFile $baseDir) { 68 $this->baseDir = $baseDir; 69 } 70 71 /** 72 * do the work 73 * @throws BuildException 74 */ 75 public function main() { 76 77 if ($this->zipFile === null) { 78 throw new BuildException("zipfile attribute must be set!", $this->getLocation()); 79 } 80 81 if ($this->zipFile->exists() && $this->zipFile->isDirectory()) { 82 throw new BuildException("zipfile is a directory!", $this->getLocation()); 83 } 84 85 if ($this->zipFile->exists() && !$this->zipFile->canWrite()) { 86 throw new BuildException("Can not write to the specified zipfile!", $this->getLocation()); 87 } 88 89 // shouldn't need to clone, since the entries in filesets 90 // themselves won't be modified -- only elements will be added 91 $savedFileSets = $this->filesets; 92 93 try { 94 if ($this->baseDir !== null) { 95 if (!$this->baseDir->exists()) { 96 throw new BuildException("basedir does not exist!", $this->getLocation()); 97 } 98 99 // add the main fileset to the list of filesets to process. 100 $mainFileSet = new FileSet($this->fileset); 101 $mainFileSet->setDir($this->baseDir); 102 $this->filesets[] = $mainFileSet; 103 } 104 105 if (empty($this->filesets)) { 106 throw new BuildException("You must supply either a basedir " 107 . "attribute or some nested filesets.", 108 $this->getLocation()); 109 } 110 111 // check if zip is out of date with respect to each 112 // fileset 113 $upToDate = true; 114 foreach($this->filesets as $fs) { 115 $ds = $fs->getDirectoryScanner($this->project); 116 $files = $ds->getIncludedFiles(); 117 if (!$this->archiveIsUpToDate($files, $fs->getDir($this->project))) { 118 $upToDate = false; 119 } 120 for ($i=0, $fcount=count($files); $i < $fcount; $i++) { 121 if ($this->zipFile->equals(new PhingFile($fs->getDir($this->project), $files[$i]))) { 122 throw new BuildException("A zip file cannot include itself", $this->getLocation()); 123 } 124 } 125 } 126 127 if ($upToDate) { 128 $this->log("Nothing to do: " . $this->zipFile->__toString() . " is up to date.", PROJECT_MSG_INFO); 129 return; 130 } 131 132 $this->log("Building zip: " . $this->zipFile->__toString(), PROJECT_MSG_INFO); 133 134 $zip = new Archive_Zip($this->zipFile->getAbsolutePath()); 135 136 foreach($this->filesets as $fs) { 137 $ds = $fs->getDirectoryScanner($this->project); 138 $files = $ds->getIncludedFiles(); 139 140 // FIXME 141 // Current model is only adding directories implicitly. This 142 // won't add any empty directories. Perhaps modify FileSet::getFiles() 143 // to also include empty directories. Not high priority, since non-inclusion 144 // of empty dirs is probably not unexpected behavior for ZipTask. 145 $fsBasedir = $fs->getDir($this->project); 146 $filesToZip = array(); 147 for ($i=0, $fcount=count($files); $i < $fcount; $i++) { 148 $f = new PhingFile($fsBasedir, $files[$i]); 149 $filesToZip[] = $f->getAbsolutePath(); 150 } 151 $zip->add($filesToZip, array('remove_path' => $fsBasedir->getCanonicalPath())); 152 } 153 154 155 } catch (IOException $ioe) { 156 $msg = "Problem creating ZIP: " . $ioe->getMessage(); 157 $this->filesets = $savedFileSets; 158 throw new BuildException($msg, $ioe, $this->getLocation()); 159 } 160 161 $this->filesets = $savedFileSets; 162 } 163 164 /** 165 * @param array $files array of filenames 166 * @param PhingFile $dir 167 * @return boolean 168 */ 169 protected function archiveIsUpToDate($files, $dir) { 170 $sfs = new SourceFileScanner($this); 171 $mm = new MergeMapper(); 172 $mm->setTo($this->zipFile->getAbsolutePath()); 173 return count($sfs->restrict($files, $dir, null, $mm)) == 0; 174 } 175 176 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |