PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/install/phing/classes/phing/util/SourceFileScanner.php

https://github.com/chregu/fluxcms
PHP | 159 lines | 89 code | 13 blank | 57 comment | 7 complexity | c5bc4c5d1830b46afefa3d8f8466c79f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * $Id$
  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. * Utility class that collects the functionality of the various
  23. * scanDir methods that have been scattered in several tasks before.
  24. *
  25. * The only method returns an array of source files. The array is a
  26. * subset of the files given as a parameter and holds only those that
  27. * are newer than their corresponding target files.
  28. * @package phing.util
  29. */
  30. class SourceFileScanner {
  31. /** Instance of FileUtils */
  32. private $fileUtils;
  33. /** Task this class is working for -- for logging purposes. */
  34. private $task;
  35. /**
  36. * @param task The task we should log messages through
  37. */
  38. function __construct($task) {
  39. $this->task = $task;
  40. $this->fileUtils = new FileUtils();
  41. }
  42. /**
  43. * Restrict the given set of files to those that are newer than
  44. * their corresponding target files.
  45. *
  46. * @param files the original set of files
  47. * @param srcDir all files are relative to this directory
  48. * @param destDir target files live here. if null file names
  49. * returned by the mapper are assumed to be absolute.
  50. * @param FilenameMapper knows how to construct a target file names from
  51. * source file names.
  52. * @param force Boolean that determines if the files should be
  53. * forced to be copied.
  54. */
  55. function restrict(&$files, $srcDir, $destDir, $mapper, $force = false) {
  56. $now = time();
  57. $targetList = "";
  58. /*
  59. If we're on Windows, we have to munge the time up to 2 secs to
  60. be able to check file modification times.
  61. (Windows has a max resolution of two secs for modification times)
  62. */
  63. $osname = strtolower(Phing::getProperty('os.name'));
  64. // indexOf()
  65. $index = ((($res = strpos($osname, 'win')) === false) ? -1 : $res);
  66. if ($index >= 0 ) {
  67. $now += 2000;
  68. }
  69. $v = array();
  70. for ($i=0, $size=count($files); $i < $size; $i++) {
  71. $targets = $mapper->main($files[$i]);
  72. if (empty($targets)) {
  73. $this->task->log($files[$i]." skipped - don't know how to handle it", PROJECT_MSG_VERBOSE);
  74. continue;
  75. }
  76. $src = null;
  77. try {
  78. if ($srcDir === null) {
  79. $src = new File($files[$i]);
  80. } else {
  81. $src = $this->fileUtils->resolveFile($srcDir, $files[$i]);
  82. }
  83. if ($src->lastModified() > $now) {
  84. $this->task->log("Warning: ".$files[$i]." modified in the future (".$src->lastModified()." > ".$now.")", PROJECT_MSG_WARN);
  85. }
  86. } catch (IOException $ioe) {
  87. $this->task->log("Unable to read file ".$files[$i]." (skipping): " . $ioe->getMessage());
  88. continue;
  89. }
  90. $added = false;
  91. $targetList = "";
  92. for ($j=0,$_j=count($targets); (!$added && $j < $_j); $j++) {
  93. $dest = null;
  94. if ($destDir === null) {
  95. $dest = new File($targets[$j]);
  96. } else {
  97. $dest = $this->fileUtils->resolveFile($destDir, $targets[$j]);
  98. }
  99. if (!$dest->exists()) {
  100. $this->task->log($files[$i]." added as " . $dest->__toString() . " doesn't exist.", PROJECT_MSG_VERBOSE);
  101. $v[] =$files[$i];
  102. $added = true;
  103. } elseif ($src->lastModified() > $dest->lastModified()) {
  104. $this->task->log($files[$i]." added as " . $dest->__toString() . " is outdated.", PROJECT_MSG_VERBOSE );
  105. $v[]=$files[$i];
  106. $added = true;
  107. } elseif ($force === true) {
  108. $this->task->log($files[$i]." added as " . $dest->__toString() . " is forced to be overwritten.", PROJECT_MSG_VERBOSE );
  109. $v[]=$files[$i];
  110. $added = true;
  111. } else {
  112. if (strlen($targetList) > 0) {
  113. $targetList .= ", ";
  114. }
  115. $targetList .= $dest->getAbsolutePath();
  116. }
  117. }
  118. if (!$added) {
  119. $this->task->log($files[$i]." omitted as ".$targetList." ".(count($targets) === 1 ? " is " : " are ")."up to date.", PROJECT_MSG_VERBOSE);
  120. }
  121. }
  122. $result = array();
  123. $result = $v;
  124. return $result;
  125. }
  126. /**
  127. * Convenience layer on top of restrict that returns the source
  128. * files as File objects (containing absolute paths if srcDir is
  129. * absolute).
  130. */
  131. function restrictAsFiles(&$files, &$srcDir, &$destDir, &$mapper) {
  132. $res = $this->restrict($files, $srcDir, $destDir, $mapper);
  133. $result = array();
  134. for ($i=0; $i<count($res); $i++) {
  135. $result[$i] = new File($srcDir, $res[$i]);
  136. }
  137. return $result;
  138. }
  139. }
  140. ?>