ConditionBase.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. * $Id: ConditionBase.php 905 2010-10-05 16:28:03Z mrook $
  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. require_once 'phing/ProjectComponent.php';
  22. include_once 'phing/Project.php';
  23. include_once 'phing/tasks/system/AvailableTask.php';
  24. include_once 'phing/tasks/system/condition/Condition.php';
  25. /**
  26. * Abstract baseclass for the <condition> task as well as several
  27. * conditions - ensures that the types of conditions inside the task
  28. * and the "container" conditions are in sync.
  29. *
  30. * @author Hans Lellelid <hans@xmpl.org>
  31. * @author Andreas Aderhold <andi@binarycloud.com>
  32. * @copyright © 2001,2002 THYRELL. All rights reserved
  33. * @version $Revision: 905 $
  34. * @package phing.tasks.system.condition
  35. */
  36. abstract class ConditionBase extends ProjectComponent implements IteratorAggregate {
  37. public $conditions = array(); // needs to be public for "inner" class access
  38. function countConditions() {
  39. return count($this->conditions);
  40. }
  41. /**
  42. * Required for IteratorAggregate
  43. */
  44. function getIterator() {
  45. return new ConditionEnumeration($this);
  46. }
  47. function getConditions() {
  48. return $this->conditions;
  49. }
  50. /**
  51. * @return void
  52. */
  53. function addAvailable(AvailableTask $a) {
  54. $this->conditions[] = $a;
  55. }
  56. /**
  57. * @return NotCondition
  58. */
  59. function createNot() {
  60. include_once 'phing/tasks/system/condition/NotCondition.php';
  61. $num = array_push($this->conditions, new NotCondition());
  62. return $this->conditions[$num-1];
  63. }
  64. /**
  65. * @return AndCondition
  66. */
  67. function createAnd() {
  68. include_once 'phing/tasks/system/condition/AndCondition.php';
  69. $num = array_push($this->conditions, new AndCondition());
  70. return $this->conditions[$num-1];
  71. }
  72. /**
  73. * @return OrCondition
  74. */
  75. function createOr() {
  76. include_once 'phing/tasks/system/condition/OrCondition.php';
  77. $num = array_push($this->conditions, new OrCondition());
  78. return $this->conditions[$num-1];
  79. }
  80. /**
  81. * @return EqualsCondition
  82. */
  83. function createEquals() {
  84. include_once 'phing/tasks/system/condition/EqualsCondition.php';
  85. $num = array_push($this->conditions, new EqualsCondition());
  86. return $this->conditions[$num-1];
  87. }
  88. /**
  89. * @return OsCondition
  90. */
  91. function createOs() {
  92. include_once 'phing/tasks/system/condition/OsCondition.php';
  93. $num = array_push($this->conditions, new OsCondition());
  94. return $this->conditions[$num-1];
  95. }
  96. /**
  97. * @return IsFalseCondition
  98. */
  99. function createIsFalse() {
  100. include_once 'phing/tasks/system/condition/IsFalseCondition.php';
  101. $num = array_push($this->conditions, new IsFalseCondition());
  102. return $this->conditions[$num-1];
  103. }
  104. /**
  105. * @return IsTrueCondition
  106. */
  107. function createIsTrue() {
  108. include_once 'phing/tasks/system/condition/IsTrueCondition.php';
  109. $num = array_push($this->conditions, new IsTrueCondition());
  110. return $this->conditions[$num-1];
  111. }
  112. /**
  113. * @return ContainsCondition
  114. */
  115. function createContains() {
  116. include_once 'phing/tasks/system/condition/ContainsCondition.php';
  117. $num = array_push($this->conditions, new ContainsCondition());
  118. return $this->conditions[$num-1];
  119. }
  120. /**
  121. * @return IsSetCondition
  122. */
  123. function createIsSet() {
  124. include_once 'phing/tasks/system/condition/IsSetCondition.php';
  125. $num = array_push($this->conditions, new IsSetCondition());
  126. return $this->conditions[$num-1];
  127. }
  128. /**
  129. * @return ReferenceExistsCondition
  130. */
  131. function createReferenceExists() {
  132. include_once 'phing/tasks/system/condition/ReferenceExistsCondition.php';
  133. $num = array_push($this->conditions, new ReferenceExistsCondition());
  134. return $this->conditions[$num-1];
  135. }
  136. }
  137. /**
  138. * "Inner" class for handling enumerations.
  139. * Uses build-in PHP5 iterator support.
  140. *
  141. * @package phing.tasks.system.condition
  142. */
  143. class ConditionEnumeration implements Iterator {
  144. /** Current element number */
  145. private $num = 0;
  146. /** "Outer" ConditionBase class. */
  147. private $outer;
  148. function __construct(ConditionBase $outer) {
  149. $this->outer = $outer;
  150. }
  151. public function valid() {
  152. return $this->outer->countConditions() > $this->num;
  153. }
  154. function current() {
  155. $o = $this->outer->conditions[$this->num];
  156. if ($o instanceof ProjectComponent) {
  157. $o->setProject($this->outer->getProject());
  158. }
  159. return $o;
  160. }
  161. function next() {
  162. $this->num++;
  163. }
  164. function key() {
  165. return $this->num;
  166. }
  167. function rewind() {
  168. $this->num = 0;
  169. }
  170. }