home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / tasks / ext / ExtractBaseTask.php < prev    next >
Encoding:
PHP Script  |  2007-02-05  |  6.6 KB  |  183 lines

  1. <?php
  2. /*
  3.  *
  4.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  5.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  8.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  9.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  10.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  11.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  12.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  13.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  14.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15.  *
  16.  * This software consists of voluntary contributions made by many individuals
  17.  * and is licensed under the LGPL. For more information please see
  18.  * <http://phing.info>.
  19.  */
  20.  
  21. require_once 'phing/tasks/system/MatchingTask.php';
  22.  
  23. /**
  24.  * Base class for extracting tasks such as Unzip and Untar.
  25.  *
  26.  * @author    Joakim Bodin <joakim.bodin+phing@gmail.com>
  27.  * @version   $Revision: 1.0 $
  28.  * @package   phing.tasks.ext
  29.  * @since     2.2.0
  30.  */
  31. abstract class ExtractBaseTask extends MatchingTask {
  32.     /**
  33.      * @var PhingFile $file
  34.      */
  35.     protected $file;
  36.     /**
  37.      * @var PhingFile $todir
  38.      */
  39.     protected $todir;
  40.     protected $removepath;
  41.     protected $filesets = array(); // all fileset objects assigned to this task
  42.  
  43.     /**
  44.      * Add a new fileset.
  45.      * @return FileSet
  46.      */
  47.     public function createFileSet() {
  48.         $this->fileset = new FileSet();
  49.         $this->filesets[] = $this->fileset;
  50.         return $this->fileset;
  51.     }
  52.  
  53.     /**
  54.      * Set the name of the zip file to extract.
  55.      * @param PhingFile $file zip file to extract
  56.      */
  57.     public function setFile(PhingFile $file) {
  58.         $this->file = $file;
  59.     }
  60.  
  61.     /**
  62.      * This is the base directory to look in for things to zip.
  63.      * @param PhingFile $baseDir
  64.      */
  65.     public function setToDir(PhingFile $todir) {
  66.         $this->todir = $todir;
  67.     }
  68.     
  69.     public function setRemovePath($removepath)
  70.     {
  71.         $this->removepath = $removepath;
  72.     }
  73.  
  74.     /**
  75.      * do the work
  76.      * @throws BuildException
  77.      */
  78.     public function main() {
  79.     
  80.         $this->validateAttributes();
  81.         
  82.         $filesToExtract = array();
  83.         if ($this->file !== null) {
  84.             if(!$this->isDestinationUpToDate($this->file)) {
  85.                 $filesToExtract[] = $this->file;
  86.             } else {
  87.                 $this->log('Nothing to do: ' . $this->todir->getAbsolutePath() . ' is up to date for ' .  $this->file->getCanonicalPath(), Project::MSG_INFO);
  88.             }
  89.         }
  90.         
  91.         foreach($this->filesets as $compressedArchiveFileset) {
  92.             $compressedArchiveDirScanner = $compressedArchiveFileset->getDirectoryScanner($this->project);
  93.             $compressedArchiveFiles = $compressedArchiveDirScanner->getIncludedFiles();
  94.             $compressedArchiveDir = $compressedArchiveFileset->getDir($this->project);
  95.             
  96.             foreach ($compressedArchiveFiles as $compressedArchiveFilePath) {
  97.                 $compressedArchiveFile = new PhingFile($compressedArchiveDir, $compressedArchiveFilePath);
  98.                 if($compressedArchiveFile->isDirectory())
  99.                 {
  100.                     throw new BuildException($compressedArchiveFile->getAbsolutePath() . ' compressed archive cannot be a directory.');
  101.                 }
  102.                 
  103.                 if(!$this->isDestinationUpToDate($compressedArchiveFile)) {
  104.                    $filesToExtract[] = $compressedArchiveFile;
  105.                 } else {
  106.                     $this->log('Nothing to do: ' . $this->todir->getAbsolutePath() . ' is up to date for ' .  $compressedArchiveFile->getCanonicalPath(), Project::MSG_INFO);
  107.                 }
  108.             }
  109.         }
  110.         
  111.         foreach ($filesToExtract as $compressedArchiveFile) {
  112.             $this->extractArchive($compressedArchiveFile);
  113.         }
  114.     }
  115.     
  116.     abstract protected function extractArchive(PhingFile $compressedArchiveFile);
  117.     
  118.     /**
  119.      * @param array $files array of filenames
  120.      * @param PhingFile $dir
  121.      * @return boolean
  122.      */
  123.     protected function isDestinationUpToDate(PhingFile $compressedArchiveFile) {
  124.         if (!$compressedArchiveFile->exists()) {
  125.             throw new BuildException("Could not find file " . $compressedArchiveFile->__toString() . " to extract.");
  126.         }
  127.         
  128.         $compressedArchiveContent = $this->listArchiveContent($compressedArchiveFile);
  129.         if(is_array($compressedArchiveContent)) {
  130.             
  131.             $fileSystem = FileSystem::getFileSystem();
  132.             foreach ($compressedArchiveContent as $compressArchivePathInfo) {
  133.                 $compressArchiveFilename = $compressArchivePathInfo['filename'];
  134.                 if(!empty($this->removepath) && strlen($compressArchiveFilename) >= strlen($this->removepath))
  135.                 {
  136.                     $compressArchiveFilename = preg_replace('/^' . $this->removepath . '/','', $compressArchiveFilename);
  137.                 }
  138.                 $compressArchivePath = new PhingFile($this->todir, $compressArchiveFilename);
  139.                 
  140.                 if(!$compressArchivePath->exists() ||
  141.                     $fileSystem->compareMTimes($compressedArchiveFile->getCanonicalPath(), $compressArchivePath->getCanonicalPath()) == 1) {
  142.                     return false;
  143.                 }
  144.             }
  145.             
  146.         }
  147.         
  148.         return true;
  149.     }
  150.     
  151.     abstract protected function listArchiveContent(PhingFile $compressedArchiveFile);
  152.     
  153.     /**
  154.      * Validates attributes coming in from XML
  155.      *
  156.      * @access  private
  157.      * @return  void
  158.      * @throws  BuildException
  159.      */
  160.     protected function validateAttributes() {
  161.     
  162.         if ($this->file === null && count($this->filesets) === 0) {
  163.             throw new BuildException("Specify at least one source compressed archive - a file or a fileset.");
  164.         }
  165.  
  166.         if ($this->todir === null) {
  167.             throw new BuildException("todir must be set.");
  168.         }
  169.         
  170.         if ($this->todir !== null && $this->todir->exists() && !$this->todir->isDirectory()) {
  171.             throw new BuildException("todir must be a directory.");
  172.         }
  173.  
  174.         if ($this->file !== null && $this->file->exists() && $this->file->isDirectory()) {
  175.             throw new BuildException("Compressed archive file cannot be a directory.");
  176.         }
  177.         
  178.         if ($this->file !== null && !$this->file->exists()) {
  179.             throw new BuildException("Could not find compressed archive file " . $this->file->__toString() . " to extract.");
  180.         }
  181.     }
  182.     
  183. }