[ 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/ -> DeleteTask.php (source)

   1  <?php
   2  /*
   3   *  $Id: DeleteTask.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/Task.php';
  23  
  24  /**
  25   * Deletes a file or directory, or set of files defined by a fileset.
  26   * 
  27   * @version   $Revision: 1.13 $
  28   * @package   phing.tasks.system
  29   */
  30  class DeleteTask extends Task {
  31  
  32      protected $file;
  33      protected $dir;
  34      protected $filesets = array();
  35      protected $includeEmpty = false;
  36  
  37      protected $quiet = false;
  38      protected $failonerror = true;
  39      protected $verbosity = PROJECT_MSG_VERBOSE;
  40      
  41      /** Any filelists of files that should be deleted. */
  42      private $filelists = array();
  43      
  44      /** 
  45       * Set the name of a single file to be removed.
  46       * @param PhingFile $file
  47       */
  48      function setFile(PhingFile $file) {       
  49          $this->file = $file;
  50      }
  51  
  52      /** 
  53       * Set the directory from which files are to be deleted.
  54       * @param PhingFile $dir
  55       */
  56      function setDir(PhingFile $dir) {
  57          $this->dir = $dir;
  58      }
  59  
  60      /**
  61       * Used to force listing of all names of deleted files.
  62       * @param boolean $verbosity
  63       */
  64      function setVerbose($verbosity) {
  65          if ($verbosity) {
  66              $this->verbosity = PROJECT_MSG_INFO;
  67          } else {
  68              $this->verbosity = PROJECT_MSG_VERBOSE;
  69          }
  70      }
  71  
  72      /**
  73       * If the file does not exist, do not display a diagnostic
  74       * message or modify the exit status to reflect an error.
  75       * This means that if a file or directory cannot be deleted,
  76       * then no error is reported. This setting emulates the
  77       * -f option to the Unix rm command. Default is false
  78      * meaning things are verbose
  79       */
  80      function setQuiet($bool) {
  81          $this->quiet = $bool;
  82          if ($this->quiet) {
  83              $this->failonerror = false;
  84          }
  85      }
  86  
  87      /** this flag means 'note errors to the output, but keep going' */
  88      function setFailOnError($bool) {
  89          $this->failonerror = $bool;
  90      }
  91  
  92  
  93      /** Used to delete empty directories.*/
  94      function setIncludeEmptyDirs($includeEmpty) {
  95          $this->includeEmpty = (boolean) $includeEmpty;
  96      }
  97  
  98      /** Nested creator, adds a set of files (nested fileset attribute). */
  99      function createFileSet() {
 100          $num = array_push($this->filesets, new FileSet());
 101          return $this->filesets[$num-1];
 102      }
 103      
 104      /** Nested creator, adds a set of files (nested fileset attribute). */
 105      function createFileList() {
 106          $num = array_push($this->filelists, new FileList());
 107          return $this->filelists[$num-1];
 108      }
 109  
 110      /** Delete the file(s). */
 111      function main() {
 112          if ($this->file === null && $this->dir === null && count($this->filesets) === 0 && count($this->filelists) === 0) {
 113              throw new BuildException("At least one of the file or dir attributes, or a fileset element, or a filelist element must be set.");
 114          }
 115  
 116          if ($this->quiet && $this->failonerror) {
 117              throw new BuildException("quiet and failonerror cannot both be set to true", $this->location);
 118          }
 119  
 120          // delete a single file
 121          if ($this->file !== null) {
 122              if ($this->file->exists()) {
 123                  if ($this->file->isDirectory()) {
 124                      $this->log("Directory " . $this->file->__toString() . " cannot be removed using the file attribute. Use dir instead.");
 125                  } else {
 126                      $this->log("Deleting: " . $this->file->__toString());
 127                      try {
 128                          $this->file->delete();
 129                      } catch(Exception $e) {
 130                          $message = "Unable to delete file " . $this->file->__toString() .": " .$e->getMessage();
 131                          if($this->failonerror) {
 132                              throw new BuildException($message);
 133                          } else {
 134                              $this->log($message, $this->quiet ? PROJECT_MSG_VERBOSE : PROJECT_MSG_WARN);
 135                          }                        
 136                      }
 137                  }
 138              } else {
 139                  $this->log("Could not find file " . $this->file->getAbsolutePath() . " to delete.",PROJECT_MSG_VERBOSE);
 140              }
 141          }
 142  
 143          // delete the directory
 144          if ($this->dir !== null && $this->dir->exists() && $this->dir->isDirectory()) {
 145              if ($this->verbosity === PROJECT_MSG_VERBOSE) {
 146                  $this->log("Deleting directory " . $this->dir->__toString());
 147              }
 148              $this->removeDir($this->dir);
 149          }
 150          
 151          // delete the files in the filelists
 152          foreach($this->filelists as $fl) {
 153              try {
 154                  $files = $fl->getFiles($this->project);
 155                  $this->removeFiles($fl->getDir($this->project), $files, $empty=array());
 156              } catch (BuildException $be) {
 157                  // directory doesn't exist or is not readable
 158                      if ($this->failonerror) {
 159                      throw $be;
 160                  } else {
 161                      $this->log($be->getMessage(), $this->quiet ? PROJECT_MSG_VERBOSE : PROJECT_MSG_WARN);
 162                  }
 163              }
 164          }
 165              
 166          // delete the files in the filesets
 167          foreach($this->filesets as $fs) {
 168              try {
 169                  $ds = $fs->getDirectoryScanner($this->project);
 170                  $files = $ds->getIncludedFiles();
 171                  $dirs = $ds->getIncludedDirectories();
 172                  $this->removeFiles($fs->getDir($this->project), $files, $dirs);
 173              } catch (BuildException $be) {
 174                      // directory doesn't exist or is not readable
 175                      if ($this->failonerror) {
 176                          throw $be;
 177                      } else {
 178                          $this->log($be->getMessage(), $this->quiet ? PROJECT_MSG_VERBOSE : PROJECT_MSG_WARN);
 179                      }
 180                  }
 181          }
 182      }
 183      
 184      /**
 185       * Recursively removes a directory.
 186       * @param PhingFile $d The directory to remove.
 187       */
 188      private function removeDir($d) {
 189          $list = $d->listDir();
 190          if ($list === null) {
 191              $list = array();
 192          }
 193          
 194          foreach($list as $s) {
 195              $f = new PhingFile($d, $s);
 196              if ($f->isDirectory()) {
 197                  $this->removeDir($f);
 198              } else {
 199                  $this->log("Deleting " . $f->__toString(), $this->verbosity);
 200                  try {
 201                      $f->delete();
 202                  } catch (Exception $e) {
 203                      $message = "Unable to delete file " . $f->__toString() . ": " . $e->getMessage();
 204                      if($this->failonerror) {
 205                          throw new BuildException($message);
 206                      } else {
 207                          $this->log($message, $this->quiet ? PROJECT_MSG_VERBOSE : PROJECT_MSG_WARN);
 208                      }
 209                  }               
 210              }
 211          }
 212          $this->log("Deleting directory " . $d->getAbsolutePath(), $this->verbosity);
 213          try {
 214              $d->delete();
 215          } catch (Exception $e) {
 216              $message = "Unable to delete directory " . $d->__toString() . ": " . $e->getMessage();
 217              if($this->failonerror) {
 218                throw new BuildException($message);
 219              } else {
 220                $this->log($message, $this->quiet ? PROJECT_MSG_VERBOSE : PROJECT_MSG_WARN);
 221              }
 222          }               
 223      }
 224  
 225      /**
 226       * remove an array of files in a directory, and a list of subdirectories
 227       * which will only be deleted if 'includeEmpty' is true
 228       * @param PhingFile $d directory to work from
 229       * @param array &$files array of files to delete; can be of zero length
 230       * @param array &$dirs array of directories to delete; can of zero length
 231       */
 232      private function removeFiles(PhingFile $d, &$files, &$dirs) {
 233          if (count($files) > 0) {
 234              $this->log("Deleting " . count($files) . " files from " . $d->__toString());
 235              for ($j=0,$_j=count($files); $j < $_j; $j++) {
 236                  $f = new PhingFile($d, $files[$j]);
 237                  $this->log("Deleting " . $f->getAbsolutePath(), $this->verbosity);
 238                  try {
 239                      $f->delete();
 240                  } catch (Exception $e) {
 241                      $message = "Unable to delete file " . $f->__toString() . ": " . $e->getMessage();
 242                      if($this->failonerror) {
 243                          throw new BuildException($message);
 244                      } else {
 245                          $this->log($message, $this->quiet ? PROJECT_MSG_VERBOSE : PROJECT_MSG_WARN);
 246                      }
 247                  }               
 248  
 249              }
 250          }
 251  
 252          if (count($dirs) > 0 && $this->includeEmpty) {
 253              $dirCount = 0;
 254              for ($j=count($dirs)-1; $j>=0; --$j) {
 255                  $dir = new PhingFile($d, $dirs[$j]);
 256                  $dirFiles = $dir->listDir();
 257                  if ($dirFiles === null || count($dirFiles) === 0) {
 258                      $this->log("Deleting " . $dir->__toString(), $this->verbosity);
 259                      try {
 260                          $dir->delete();
 261                          $dirCount++;
 262                      } catch (Exception $e) {
 263                          $message="Unable to delete directory " + $dir;
 264                          if($this->failonerror) {
 265                              throw new BuildException($message);
 266                          } else {
 267                              $this->log($message, $this->quiet ? PROJECT_MSG_VERBOSE : PROJECT_MSG_WARN);
 268                          }
 269                      }
 270                  }
 271              }
 272              if ($dirCount > 0) {
 273                  $this->log("Deleted $dirCount director" . ($dirCount==1 ? "y" : "ies") . " from " . $d->__toString());
 274              }
 275          }
 276      }
 277  }


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