ReflexiveTask.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * $Id: ReflexiveTask.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. /**
  23. * This task is for using filter chains to make changes to files and overwrite the original files.
  24. *
  25. * This task was created to serve the need for "cleanup" tasks -- e.g. a ReplaceRegexp task or strip task
  26. * being used to modify files and then overwrite the modified files. In many (most?) cases you probably
  27. * should just use a copy task to preserve the original source files, but this task supports situations
  28. * where there is no src vs. build directory, and modifying source files is actually desired.
  29. *
  30. * <code>
  31. * <reflexive>
  32. * <fileset dir=".">
  33. * <include pattern="*.html">
  34. * </fileset>
  35. * <filterchain>
  36. * <replaceregexp>
  37. * <regexp pattern="\n\r" replace="\n"/>
  38. * </replaceregexp>
  39. * </filterchain>
  40. * </reflexive>
  41. * </code>
  42. *
  43. * @author Hans Lellelid <hans@xmpl.org>
  44. * @version $Revision: 905 $
  45. * @package phing.tasks.system
  46. */
  47. class ReflexiveTask extends Task {
  48. /** Single file to process. */
  49. private $file;
  50. /** Any filesets that should be processed. */
  51. private $filesets = array();
  52. /** Any filters to be applied before append happens. */
  53. private $filterChains = array();
  54. /** Alias for setFrom() */
  55. function setFile(PhingFile $f) {
  56. $this->file = $f;
  57. }
  58. /** Nested creator, adds a set of files (nested fileset attribute). */
  59. function createFileSet() {
  60. $num = array_push($this->filesets, new FileSet());
  61. return $this->filesets[$num-1];
  62. }
  63. /**
  64. * Creates a filterchain
  65. *
  66. * @return object The created filterchain object
  67. */
  68. function createFilterChain() {
  69. $num = array_push($this->filterChains, new FilterChain($this->project));
  70. return $this->filterChains[$num-1];
  71. }
  72. /** Append the file(s). */
  73. function main() {
  74. if ($this->file === null && empty($this->filesets)) {
  75. throw new BuildException("You must specify a file or fileset(s) for the <reflexive> task.");
  76. }
  77. // compile a list of all files to modify, both file attrib and fileset elements
  78. // can be used.
  79. $files = array();
  80. if ($this->file !== null) {
  81. $files[] = $this->file;
  82. }
  83. if (!empty($this->filesets)) {
  84. $filenames = array();
  85. foreach($this->filesets as $fs) {
  86. try {
  87. $ds = $fs->getDirectoryScanner($this->project);
  88. $filenames = $ds->getIncludedFiles(); // get included filenames
  89. $dir = $fs->getDir($this->project);
  90. foreach ($filenames as $fname) {
  91. $files[] = new PhingFile($dir, $fname);
  92. }
  93. } catch (BuildException $be) {
  94. $this->log($be->getMessage(), Project::MSG_WARN);
  95. }
  96. }
  97. }
  98. $this->log("Applying reflexive processing to " . count($files) . " files.");
  99. // These "slots" allow filters to retrieve information about the currently-being-process files
  100. $slot = $this->getRegisterSlot("currentFile");
  101. $basenameSlot = $this->getRegisterSlot("currentFile.basename");
  102. foreach($files as $file) {
  103. // set the register slots
  104. $slot->setValue($file->getPath());
  105. $basenameSlot->setValue($file->getName());
  106. // 1) read contents of file, pulling through any filters
  107. $in = null;
  108. try {
  109. $contents = "";
  110. $in = FileUtils::getChainedReader(new FileReader($file), $this->filterChains, $this->project);
  111. while(-1 !== ($buffer = $in->read())) {
  112. $contents .= $buffer;
  113. }
  114. $in->close();
  115. } catch (Exception $e) {
  116. if ($in) $in->close();
  117. $this->log("Erorr reading file: " . $e->getMessage(), Project::MSG_WARN);
  118. }
  119. try {
  120. // now create a FileWriter w/ the same file, and write to the file
  121. $out = new FileWriter($file);
  122. $out->write($contents);
  123. $out->close();
  124. $this->log("Applying reflexive processing to " . $file->getPath(), Project::MSG_VERBOSE);
  125. } catch (Exception $e) {
  126. if ($out) $out->close();
  127. $this->log("Error writing file back: " . $e->getMessage(), Project::MSG_WARN);
  128. }
  129. }
  130. }
  131. }