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 / IfTask.php < prev    next >
Encoding:
PHP Script  |  2006-03-10  |  7.1 KB  |  224 lines

  1. <?php
  2.  
  3. /*
  4.  *  $Id: IfTask.php 43 2006-03-10 14:31:51Z mrook $
  5.  *
  6.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  10.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  12.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  13.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17.  *
  18.  * This software consists of voluntary contributions made by many individuals
  19.  * and is licensed under the LGPL. For more information please see
  20.  * <http://phing.info>.
  21.  */
  22.  
  23. require_once 'phing/tasks/system/condition/ConditionBase.php';
  24. require_once 'phing/tasks/system/SequentialTask.php';
  25.  
  26. /**
  27.  * Perform some tasks based on whether a given condition holds true or
  28.  * not.
  29.  *
  30.  * <p>This task is heavily based on the Condition framework that can
  31.  * be found in Ant 1.4 and later, therefore it cannot be used in
  32.  * conjunction with versions of Ant prior to 1.4.</p>
  33.  *
  34.  * <p>This task doesn't have any attributes, the condition to test is
  35.  * specified by a nested element - see the documentation of your
  36.  * <code><condition></code> task (see
  37.  * <a href="http://jakarta.apache.org/ant/manual/CoreTasks/condition.html">the
  38.  * online documentation</a> for example) for a complete list of nested
  39.  * elements.</p>
  40.  *
  41.  * <p>Just like the <code><condition></code> task, only a single
  42.  * condition can be specified - you combine them using
  43.  * <code><and></code> or <code><or></code> conditions.</p>
  44.  *
  45.  * <p>In addition to the condition, you can specify three different
  46.  * child elements, <code><elseif></code>, <code><then></code> and
  47.  * <code><else></code>.  All three subelements are optional.
  48.  *
  49.  * Both <code><then></code> and <code><else></code> must not be
  50.  * used more than once inside the if task.  Both are
  51.  * containers for Ant tasks, just like Ant's
  52.  * <code><parallel></code> and <code><sequential></code>
  53.  * tasks - in fact they are implemented using the same class as Ant's
  54.  * <code><sequential></code> task.</p>
  55.  *
  56.  *  The <code><elseif></code> behaves exactly like an <code><if></code>
  57.  * except that it cannot contain the <code><else></code> element
  58.  * inside of it.  You may specify as may of these as you like, and the
  59.  * order they are specified is the order they are evaluated in.  If the
  60.  * condition on the <code><if></code> is false, then the first
  61.  * <code><elseif></code> who's conditional evaluates to true
  62.  * will be executed.  The <code><else></code> will be executed
  63.  * only if the <code><if></code> and all <code><elseif></code>
  64.  * conditions are false.
  65.  *
  66.  * <p>Use the following task to define the <code><if></code>
  67.  * task before you use it the first time:</p>
  68.  *
  69.  * <pre><code>
  70.  *   <taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" />
  71.  * </code></pre>
  72.  *
  73.  * <h3>Crude Example</h3>
  74.  *
  75.  * <code>
  76.  * <if>
  77.  *  <equals arg1="${foo}" arg2="bar" />
  78.  *  <then>
  79.  *    <echo message="The value of property foo is bar" />
  80.  *  </then>
  81.  *  <else>
  82.  *    <echo message="The value of property foo is not bar" />
  83.  *  </else>
  84.  * </if>
  85.  * </code>
  86.  *
  87.  * <code>
  88.  * <if>
  89.  *  <equals arg1="${foo}" arg2="bar" />
  90.  *  <then>
  91.  *   <echo message="The value of property foo is 'bar'" />
  92.  *  </then>
  93.  *
  94.  *  <elseif>
  95.  *   <equals arg1="${foo}" arg2="foo" />
  96.  *   <then>
  97.  *    <echo message="The value of property foo is 'foo'" />
  98.  *   </then>
  99.  *  </elseif>
  100.  *
  101.  *  <else>
  102.  *   <echo message="The value of property foo is not 'foo' or 'bar'" />
  103.  *  </else>
  104.  * </if>
  105.  * </code>
  106.  *
  107.  * @author <a href="mailto:stefan.bodewig@freenet.de">Stefan Bodewig</a>
  108.  */
  109. class IfTask extends ConditionBase {
  110.  
  111.  
  112.     private $thenTasks = null;
  113.     private $elseIfTasks = array();
  114.     private $elseTasks = null;
  115.  
  116.     /***
  117.      * A nested Else if task
  118.      */
  119.     public function addElseIf(ElseIfTask $ei)
  120.     {
  121.         $this->elseIfTasks[] = $ei;
  122.     }
  123.  
  124.     /**
  125.      * A nested <then> element - a container of tasks that will
  126.      * be run if the condition holds true.
  127.      *
  128.      * <p>Not required.</p>
  129.      */
  130.     public function addThen(SequentialTask $t) {
  131.         if ($this->thenTasks != null) {
  132.             throw new BuildException("You must not nest more than one <then> into <if>");
  133.         }
  134.         $this->thenTasks = $t;
  135.     }
  136.  
  137.     /**
  138.      * A nested <else> element - a container of tasks that will
  139.      * be run if the condition doesn't hold true.
  140.      *
  141.      * <p>Not required.</p>
  142.      */
  143.     public function addElse(SequentialTask $e) {
  144.         if ($this->elseTasks != null) {
  145.             throw new BuildException("You must not nest more than one <else> into <if>");
  146.         }
  147.         $this->elseTasks = $e;
  148.     }
  149.  
  150.     public function main() {
  151.     
  152.         if ($this->countConditions() > 1) {
  153.             throw new BuildException("You must not nest more than one condition into <if>");
  154.         }
  155.         if ($this->countConditions() < 1) {
  156.             throw new BuildException("You must nest a condition into <if>");
  157.         }
  158.         $conditions = $this->getConditions();
  159.         $c = $conditions[0];
  160.         
  161.         if ($c->evaluate()) {
  162.             if ($this->thenTasks != null) {
  163.                 $this->thenTasks->main();
  164.             }
  165.         } else {
  166.             $done = false;
  167.             $sz = count($this->elseIfTasks);
  168.             for($i=0; $i < $sz && !$done; $i++) {
  169.                 $ei = $this->elseIfTasks[$i];
  170.                 if ($ei->evaluate()) {
  171.                     $done = true;
  172.                     $ei->main();
  173.                 }
  174.             }
  175.  
  176.             if (!$done && $this->elseTasks != null) {
  177.                 $this->elseTasks->main();
  178.             }
  179.         }
  180.     }
  181. }
  182.  
  183. /**
  184.  * "Inner" class for IfTask.
  185.  * This class has same basic structure as the IfTask, although of course it doesn't support <else> tags.
  186.  */
  187. class ElseIfTask extends ConditionBase {
  188.  
  189.         private $thenTasks = null;
  190.  
  191.         public function addThen(SequentialTask $t) {
  192.             if ($this->thenTasks != null) {
  193.                 throw new BuildException("You must not nest more than one <then> into <elseif>");
  194.             }
  195.             $this->thenTasks = $t;
  196.         }
  197.     
  198.         /**
  199.          * @return boolean
  200.          */
  201.         public function evaluate() {
  202.         
  203.             if ($this->countConditions() > 1) {
  204.                 throw new BuildException("You must not nest more than one condition into <elseif>");
  205.             }
  206.             if ($this->countConditions() < 1) {
  207.                 throw new BuildException("You must nest a condition into <elseif>");
  208.             }
  209.             
  210.             $conditions = $this->getConditions();
  211.             $c = $conditions[0];
  212.  
  213.             return $c->evaluate();
  214.         }
  215.         
  216.         /**
  217.          * 
  218.          */
  219.         public function main() {
  220.             if ($this->thenTasks != null) {
  221.                 $this->thenTasks->main();
  222.             }
  223.         }
  224.     }