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 / system / AvailableTask.php < prev    next >
Encoding:
PHP Script  |  2007-02-05  |  4.3 KB  |  133 lines

  1. <?php
  2. /*
  3.  *  $Id: AvailableTask.php 144 2007-02-05 15:19:00Z hans $
  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. include_once 'phing/tasks/system/condition/ConditionBase.php';
  24.  
  25. /**
  26.  *  <available> task.
  27.  *
  28.  *  Note: implements condition interface (see condition/Condition.php)
  29.  *
  30.  *  @author    Andreas Aderhold <andi@binarycloud.com>
  31.  *  @copyright ⌐ 2001,2002 THYRELL. All rights reserved
  32.  *  @version   $Revision: 1.11 $
  33.  *  @package   phing.tasks.system
  34.  */
  35. class AvailableTask extends Task {
  36.  
  37.     /** Property to check for. */
  38.     private $property;
  39.     
  40.     /** Value property should be set to. */
  41.     private $value = "true";
  42.     
  43.     /** Resource to check for */
  44.     private $resource;
  45.     
  46.     private $type = null;
  47.     private $filepath = null;
  48.  
  49.     function setProperty($property) {
  50.         $this->property = (string) $property;
  51.     }
  52.  
  53.     function setValue($value) {
  54.         $this->value = (string) $value;
  55.     }
  56.  
  57.     function setFile(PhingFile $file) {
  58.         $this->file = $file;
  59.     }
  60.  
  61.     function setResource($resource) {
  62.         $this->resource = (string) $resource;
  63.     }
  64.  
  65.     function setType($type) {
  66.         $this->type = (string) strtolower($type);
  67.     }
  68.  
  69.     function main() {
  70.         if ($this->property === null) {
  71.             throw new BuildException("property attribute is required", $this->location);
  72.         }
  73.         if ($this->evaluate()) {
  74.             $this->project->setProperty($this->property, $this->value);
  75.         }
  76.     }
  77.  
  78.     function evaluate() {
  79.         if ($this->file === null && $this->resource === null) {
  80.             throw new BuildException("At least one of (file|resource) is required", $this->location);            
  81.         }
  82.  
  83.         if ($this->type !== null && ($this->type !== "file" && $this->type !== "dir")) {
  84.             throw new BuildException("Type must be one of either dir or file", $this->location);
  85.         }
  86.         
  87.         if (($this->file !== null) && !$this->_checkFile()) {
  88.             $this->log("Unable to find " . $this->file->__toString() . " to set property " . $this->property, Project::MSG_VERBOSE);
  89.             return false;
  90.         }
  91.  
  92.         if (($this->resource !== null) && !$this->_checkResource($this->resource)) {
  93.             $this->log("Unable to load resource " . $this->resource . " to set property " . $this->property, Project::MSG_VERBOSE);
  94.             return false;
  95.         }
  96.  
  97.         return true;
  98.     }
  99.  
  100.     // this is prepared for the path type
  101.     function _checkFile() {
  102.         if ($this->filepath === null) {
  103.             return $this->_checkFile1($this->file);
  104.         } else {
  105.             $paths = $this->filepath->listDir();
  106.             for($i=0,$pcnt=count($paths); $i < $pcnt; $i++) {
  107.                 $this->log("Searching " . $paths[$i], Project::MSG_VERBOSE);
  108.                 $tmp = new PhingFile($paths[$i], $this->file->getName());
  109.                 if($tmp->isFile()) {
  110.                     return true;
  111.                 }
  112.             }
  113.         }
  114.         return false;
  115.     }
  116.  
  117.     function _checkFile1($file) {
  118.         if ($this->type !== null) {
  119.             if ($this->type === "dir") {
  120.                 return $file->isDirectory();
  121.             } else if ($this->type === "file") {
  122.                 return $file->isFile();
  123.             }
  124.         }
  125.         return $file->exists();
  126.     }
  127.  
  128.     function _checkResource($resource) {
  129.         return $this->_checkFile1(new PhingFile(Phing::getResourcePath($resource)));
  130.     }
  131.  
  132. }
  133.