PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/install/phing/classes/phing/types/selectors/DateSelector.php

https://github.com/chregu/fluxcms
PHP | 214 lines | 133 code | 15 blank | 66 comment | 15 complexity | a47fa443388151f5369a8fa9c613abcb 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. require_once 'phing/types/selectors/BaseExtendSelector.php';
  22. /**
  23. * Selector that chooses files based on their last modified date. Ant uses
  24. * millisecond precision (thanks to Java); PHP is forced to use only seconds
  25. * precision.
  26. *
  27. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  28. * @author Bruce Atherton <bruce@callenish.com> (Ant)
  29. * @version $Revision: 1.9 $
  30. * @package phing.types.selecctors
  31. */
  32. class DateSelector extends BaseExtendSelector {
  33. private $seconds = -1; // millis in Ant, but PHP doesn't support that level of precision
  34. private $dateTime = null;
  35. private $includeDirs = false;
  36. private $granularity = 0;
  37. private $cmp = 2;
  38. const MILLIS_KEY = "millis";
  39. const DATETIME_KEY = "datetime";
  40. const CHECKDIRS_KEY = "checkdirs";
  41. const GRANULARITY_KEY = "granularity";
  42. const WHEN_KEY = "when";
  43. private static $timeComparisons = array("before", "after", "equal");
  44. public function __construct() {
  45. //if (Os.isFamily("dos")) {
  46. // granularity = 2000;
  47. //}
  48. }
  49. public function toString() {
  50. $buf = "{dateselector date: ";
  51. $buf .= $this->dateTime;
  52. $buf .= " compare: ";
  53. if ($this->cmp === 0) {
  54. $buf .= "before";
  55. } elseif ($this->cmp === 1) {
  56. $buf .= "after";
  57. } else {
  58. $buf .= "equal";
  59. }
  60. $buf .= " granularity: ";
  61. $buf .= $this->granularity;
  62. $buf .= "}";
  63. return $buf;
  64. }
  65. /**
  66. * For users that prefer to express time in seconds since 1970
  67. *
  68. * @param int $seconds the time to compare file's last modified date to,
  69. * expressed in milliseconds
  70. */
  71. public function setSeconds($seconds) {
  72. $this->seconds = (int) $seconds;
  73. }
  74. /**
  75. * Returns the seconds value the selector is set for.
  76. */
  77. public function getSeconds() {
  78. return $this->seconds;
  79. }
  80. /**
  81. * Sets the date. The user must supply it in MM/DD/YYYY HH:MM AM_PM
  82. * format
  83. *
  84. * @param string $dateTime a string in MM/DD/YYYY HH:MM AM_PM format
  85. */
  86. public function setDatetime($dateTime) {
  87. $dt = strtotime($dateTime);
  88. if ($dt == -1) {
  89. $this->setError("Date of " . $dateTime
  90. . " Cannot be parsed correctly. It should be in"
  91. . " a format parsable by PHP's strtotime() function.");
  92. } else {
  93. $this->dateTime = $dateTime;
  94. $this->setSeconds($dt);
  95. }
  96. }
  97. /**
  98. * Should we be checking dates on directories?
  99. *
  100. * @param boolean $includeDirs whether to check the timestamp on directories
  101. */
  102. public function setCheckdirs($includeDirs) {
  103. $this->includeDirs = (boolean) $includeDirs;
  104. }
  105. /**
  106. * Sets the number of milliseconds leeway we will give before we consider
  107. * a file not to have matched a date.
  108. * @param int $granularity
  109. */
  110. public function setGranularity($granularity) {
  111. $this->granularity = (int) $granularity;
  112. }
  113. /**
  114. * Sets the type of comparison to be done on the file's last modified
  115. * date.
  116. *
  117. * @param string $cmp The comparison to perform
  118. */
  119. public function setWhen($cmp) {
  120. $idx = array_search($cmp, self::$timeComparisons, true);
  121. if ($idx === null) {
  122. $this->setError("Invalid value for ".WHEN_KEY.": ".$cmp);
  123. } else {
  124. $this->cmp = $idx;
  125. }
  126. }
  127. /**
  128. * When using this as a custom selector, this method will be called.
  129. * It translates each parameter into the appropriate setXXX() call.
  130. *
  131. * @param array $parameters the complete set of parameters for this selector
  132. */
  133. public function setParameters($parameters) {
  134. parent::setParameters($parameters);
  135. if ($parameters !== null) {
  136. for ($i=0,$size=count($parameters); $i < $size; $i++) {
  137. $paramname = $parameters[$i]->getName();
  138. switch(strtolower($paramname)) {
  139. case self::MILLIS_KEY:
  140. $this->setMillis($parameters[$i]->getValue());
  141. break;
  142. case self::DATETIME_KEY:
  143. $this->setDatetime($parameters[$i]->getValue());
  144. break;
  145. case self::CHECKDIRS_KEY:
  146. $this->setCheckdirs($parameters[$i]->getValue());
  147. break;
  148. case self::GRANULARITY_KEY:
  149. $this->setGranularity($parameters[$i]->getValue());
  150. break;
  151. case self::WHEN_KEY:
  152. $this->setWhen($parameters[$i]->getValue());
  153. break;
  154. default:
  155. $this->setError("Invalid parameter " . $paramname);
  156. } // switch
  157. }
  158. }
  159. }
  160. /**
  161. * This is a consistency check to ensure the selector's required
  162. * values have been set.
  163. */
  164. public function verifySettings() {
  165. if ($this->dateTime === null && $this->seconds < 0) {
  166. $this->setError("You must provide a datetime or the number of "
  167. . "seconds.");
  168. } elseif ($this->seconds < 0) {
  169. $this->setError("Date of " . $this->dateTime
  170. . " results in negative seconds"
  171. . " value relative to epoch (January 1, 1970, 00:00:00 GMT).");
  172. }
  173. }
  174. /**
  175. * The heart of the matter. This is where the selector gets to decide
  176. * on the inclusion of a file in a particular fileset.
  177. *
  178. * @param File $basedir the base directory the scan is being done from
  179. * @param string $filename is the name of the file to check
  180. * @param File $file is a java.io.File object the selector can use
  181. * @return boolean Whether the file should be selected or not
  182. */
  183. public function isSelected(File $basedir, $filename, File $file) {
  184. $this->validate();
  185. if ($file->isDirectory() && ($this->includeDirs === false)) {
  186. return true;
  187. }
  188. if ($this->cmp === 0) {
  189. return (($file->lastModified() - $this->granularity) < $this->seconds);
  190. } elseif ($this->cmp === 1) {
  191. return (($file->lastModified() . $this->granularity) > $this->seconds);
  192. } else {
  193. return (abs($file->lastModified() - $this->seconds) <= $this->granularity);
  194. }
  195. }
  196. }