ForeachTask.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /*
  3. * $Id: ForeachTask.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/Task.php';
  22. require_once 'phing/system/io/FileSystem.php';
  23. include_once 'phing/tasks/system/PhingTask.php';
  24. /**
  25. * <foreach> task
  26. *
  27. * Task definition for the foreach task. This task takes a list with
  28. * delimited values, and executes a target with set param.
  29. *
  30. * Usage:
  31. * <foreach list="values" target="targ" param="name" delimiter="|" />
  32. *
  33. * Attributes:
  34. * list --> The list of values to process, with the delimiter character,
  35. * indicated by the "delimiter" attribute, separating each value.
  36. * target --> The target to call for each token, passing the token as the
  37. * parameter with the name indicated by the "param" attribute.
  38. * param --> The name of the parameter to pass the tokens in as to the
  39. * target.
  40. * delimiter --> The delimiter string that separates the values in the "list"
  41. * parameter. The default is ",".
  42. *
  43. * @author Jason Hines <jason@greenhell.com>
  44. * @author Hans Lellelid <hans@xmpl.org>
  45. * @version $Revision: 905 $
  46. * @package phing.tasks.system
  47. */
  48. class ForeachTask extends Task {
  49. /** Delimter-separated list of values to process. */
  50. private $list;
  51. /** Name of parameter to pass to callee */
  52. private $param;
  53. /** Name of absolute path parameter to pass to callee */
  54. private $absparam;
  55. /** Delimiter that separates items in $list */
  56. private $delimiter = ',';
  57. /**
  58. * PhingCallTask that will be invoked w/ calleeTarget.
  59. * @var PhingCallTask
  60. */
  61. private $callee;
  62. /** Array of filesets */
  63. private $filesets = array();
  64. /**
  65. * Target to execute.
  66. * @var string
  67. */
  68. private $calleeTarget;
  69. function init() {
  70. $this->callee = $this->project->createTask("phingcall");
  71. $this->callee->setOwningTarget($this->getOwningTarget());
  72. $this->callee->setTaskName($this->getTaskName());
  73. $this->callee->setLocation($this->getLocation());
  74. $this->callee->init();
  75. }
  76. /**
  77. * This method does the work.
  78. * @return void
  79. */
  80. function main() {
  81. if ($this->list === null && count($this->filesets) == 0) {
  82. throw new BuildException("Need either list or nested fileset to iterate through");
  83. }
  84. if ($this->param === null) {
  85. throw new BuildException("You must supply a property name to set on each iteration in param");
  86. }
  87. if ($this->calleeTarget === null) {
  88. throw new BuildException("You must supply a target to perform");
  89. }
  90. $callee = $this->callee;
  91. $callee->setTarget($this->calleeTarget);
  92. $callee->setInheritAll(true);
  93. $callee->setInheritRefs(true);
  94. if (trim($this->list)) {
  95. $arr = explode($this->delimiter, $this->list);
  96. foreach ($arr as $value) {
  97. $value = trim($value);
  98. $this->log("Setting param '$this->param' to value '$value'", Project::MSG_VERBOSE);
  99. $prop = $callee->createProperty();
  100. $prop->setOverride(true);
  101. $prop->setName($this->param);
  102. $prop->setValue($value);
  103. $callee->main();
  104. }
  105. }
  106. $total_files = 0;
  107. $total_dirs = 0;
  108. // filesets
  109. foreach ($this->filesets as $fs) {
  110. $ds = $fs->getDirectoryScanner($this->project);
  111. $fromDir = $fs->getDir($this->project);
  112. $srcFiles = $ds->getIncludedFiles();
  113. $srcDirs = $ds->getIncludedDirectories();
  114. $this->log(count($srcDirs) . ' directories and ' . count($srcFiles) . ' files', Project::MSG_VERBOSE);
  115. $filecount = count($srcFiles);
  116. $total_files = $total_files + $filecount;
  117. for ($j = 0; $j < $filecount; $j++) {
  118. $value = $srcFiles[$j];
  119. if ($this->param) {
  120. $this->log("Setting param '$this->param' to value '$value'", Project::MSG_VERBOSE);
  121. $prop = $callee->createProperty();
  122. $prop->setOverride(true);
  123. $prop->setName($this->param);
  124. $prop->setValue($value);
  125. }
  126. if ($this->absparam) {
  127. $prop = $callee->createProperty();
  128. $prop->setOverride(true);
  129. $prop->setName($this->absparam);
  130. $prop->setValue($fromDir . FileSystem::getFileSystem()->getSeparator() . $value);
  131. }
  132. $callee->main();
  133. }
  134. $dircount = count($srcDirs);
  135. $total_dirs = $total_dirs + $dircount;
  136. for ($j = 0; $j < $dircount; $j++) {
  137. $value = $srcDirs[$j];
  138. if ($this->param) {
  139. $this->log("Setting param '$this->param' to value '$value'", Project::MSG_VERBOSE);
  140. $prop = $callee->createProperty();
  141. $prop->setOverride(true);
  142. $prop->setName($this->param);
  143. $prop->setValue($value);
  144. }
  145. if ($this->absparam) {
  146. $prop = $callee->createProperty();
  147. $prop->setOverride(true);
  148. $prop->setName($this->absparam);
  149. $prop->setValue($fromDir . FileSystem::getFileSystem()->getSeparator() . $value);
  150. }
  151. $callee->main();
  152. }
  153. }
  154. }
  155. function setList($list) {
  156. $this->list = (string) $list;
  157. }
  158. function setTarget($target) {
  159. $this->calleeTarget = (string) $target;
  160. }
  161. function setParam($param) {
  162. $this->param = (string) $param;
  163. }
  164. function setAbsparam($absparam) {
  165. $this->absparam = (string) $absparam;
  166. }
  167. function setDelimiter($delimiter) {
  168. $this->delimiter = (string) $delimiter;
  169. }
  170. /**
  171. * Nested creator, adds a set of files (nested fileset attribute).
  172. */
  173. function createFileSet() {
  174. $num = array_push($this->filesets, new FileSet());
  175. return $this->filesets[$num-1];
  176. }
  177. /**
  178. * @return Property
  179. */
  180. function createProperty() {
  181. return $this->callee->createProperty();
  182. }
  183. }