[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/phing/tasks/system/ -> MoveTask.php (source)

   1  <?php
   2  /*
   3   *  $Id: MoveTask.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/CopyTask.php';
  23  include_once 'phing/system/io/PhingFile.php';
  24  include_once 'phing/system/io/IOException.php';
  25  
  26  /**
  27   * Moves a file or directory to a new file or directory.
  28   * 
  29   * By default, the destination file is overwritten if it
  30   * already exists.  When overwrite is turned off, then files
  31   * are only moved if the source file is newer than the
  32   * destination file, or when the destination file does not
  33   * exist.
  34   *
  35   * Source files and directories are only deleted when the file or
  36   * directory has been copied to the destination successfully.
  37   *
  38   * @version $Revision: 1.8 $
  39   * @package phing.tasks.system
  40   */
  41  class MoveTask extends CopyTask {
  42  
  43      function __construct() {
  44          parent::__construct();
  45          $this->forceOverwrite = true;
  46      }
  47      
  48      protected function doWork() {
  49      
  50          $copyMapSize = count($this->fileCopyMap);
  51          if ($copyMapSize > 0) {
  52              // files to move
  53              $this->log("Moving $copyMapSize files to " . $this->destDir->getAbsolutePath());
  54  
  55              foreach($this->fileCopyMap as $from => $to) {
  56                  if ($from == $to) {
  57                      $this->log("Skipping self-move of $from", $this->verbosity);
  58                      continue;
  59                  }
  60  
  61                  $moved = false;
  62                  $f = new PhingFile($from);
  63                  $d = new PhingFile($to);
  64                  
  65                  $moved = false;
  66                  try { // try to rename                    
  67                      $this->log("Attempting to rename $from to $to", $this->verbosity);
  68                      $this->renameFile($f, $d, $this->forceOverwrite);
  69                      $moved = true;
  70                  } catch (IOException $ioe) {
  71                      $moved = false;
  72                      $this->log("Failed to rename $from to $to: " . $ioe->getMessage(), $this->verbosity);
  73                  }
  74  
  75                  if (!$moved) {                    
  76                      try { // try to move
  77                          $this->log("Moving $from to $to", $this->verbosity);
  78  
  79                          $this->fileUtils->copyFile($f, $d, $this->forceOverwrite, $this->preserveLMT, $this->filterChains, $this->getProject());                        
  80  
  81                          $f = new PhingFile($fromFile);
  82                          $f->delete();
  83                      } catch (IOException $ioe) {
  84                          $msg = "Failed to move $from to $to: " . $ioe->getMessage();
  85                          throw new BuildException($msg, $this->location);
  86                      }
  87                  } // if !moved
  88              } // foreach fileCopyMap
  89          } // if copyMapSize
  90  
  91          // handle empty dirs if appropriate
  92          if ($this->includeEmpty) {
  93              $e = array_keys($this->dirCopyMap);
  94              $count = 0;
  95              foreach ($e as $dir) {
  96                  $d = new PhingFile((string) $dir);
  97                  if (!$d->exists()) {
  98                      if (!$d->mkdirs()) {
  99                          $this->log("Unable to create directory " . $d->getAbsolutePath(), PROJECT_MSG_ERR);
 100                      } else {
 101                          $count++;
 102                      }
 103                  }
 104              }
 105              if ($count > 0) {
 106                  $this->log("moved $count empty director" . ($count == 1 ? "y" : "ies") . " to " . $this->destDir->getAbsolutePath());
 107              }
 108          }
 109  
 110          if (count($this->filesets) > 0) {
 111              // process filesets
 112              foreach($this->filesets as $fs) {
 113                  $dir = $fs->getDir($this->project);
 114                  if ($this->okToDelete($dir)) {
 115                      $this->deleteDir($dir);
 116                  }
 117              }
 118          }
 119      }
 120  
 121      /** Its only ok to delete a dir tree if there are no files in it. */
 122      private function okToDelete($d) {
 123          $list = $d->listDir();
 124          if ($list === null) {
 125              return false;     // maybe io error?
 126          }
 127          
 128          foreach($list as $s) {
 129              $f = new PhingFile($d, $s);
 130              if ($f->isDirectory()) {
 131                  if (!$this->okToDelete($f)) {
 132                      return false;
 133                  }
 134              } else {
 135                  // found a file
 136                  return false;
 137              }
 138          }
 139          return true;
 140      }
 141  
 142      /** Go and delete the directory tree. */
 143      private function deleteDir($d) {
 144      
 145          $list = $d->listDir();
 146          if ($list === null) {
 147              return;      // on an io error list() can return null
 148          }
 149          
 150          foreach($list as $fname) {
 151              $f = new PhingFile($d, $fname);
 152              if ($f->isDirectory()) {
 153                  $this->deleteDir($f);
 154              } else {
 155                  throw new BuildException("UNEXPECTED ERROR - The file " . $f->getAbsolutePath() . " should not exist!");
 156              }
 157          }
 158  
 159          $this->log("Deleting directory " . $d->getPath(), $this->verbosity);
 160          try {
 161              $d->delete();
 162          } catch (Exception $e) {
 163              throw new BuildException("Unable to delete directory " . $d->__toString() . ": " . $e->getMessage());
 164          }
 165      }
 166  
 167      /**
 168       * Attempts to rename a file from a source to a destination.
 169       * If overwrite is set to true, this method overwrites existing file
 170       * even if the destination file is newer.
 171       * Otherwise, the source f
 172       * ile is renamed only if the destination file #
 173       * is older than it.
 174       */
 175      private function renameFile(PhingFile $sourceFile, PhingFile $destFile, $overwrite) {
 176          $renamed = true;
 177  
 178          // ensure that parent dir of dest file exists!
 179          $parent = $destFile->getParentFile();
 180          if ($parent !== null) {
 181              if (!$parent->exists()) {
 182                  $parent->mkdirs();
 183              }
 184          }
 185          if ($destFile->exists()) {
 186              try {
 187                  $destFile->delete();
 188              } catch (Exception $e) {
 189                  throw new BuildException("Unable to remove existing file " . $destFile->__toString() . ": " . $e->getMessage());
 190              }
 191          }
 192          $renamed = $sourceFile->renameTo($destFile);
 193  
 194          return $renamed;
 195      }
 196  }
 197  ?>


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7