TstampTask.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * $Id: TstampTask.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. * Sets properties to the current time, or offsets from the current time.
  24. * The default properties are TSTAMP, DSTAMP and TODAY;
  25. *
  26. * Based on Ant's Tstamp task.
  27. *
  28. * @author Michiel Rook <michiel.rook@gmail.com>
  29. * @version $Revision: 905 $
  30. * @package phing.tasks.system
  31. * @since 2.2.0
  32. */
  33. class TstampTask extends Task
  34. {
  35. private $customFormats = array();
  36. private $prefix = "";
  37. /**
  38. * Set a prefix for the properties. If the prefix does not end with a "."
  39. * one is automatically added.
  40. * @param prefix the prefix to use.
  41. */
  42. public function setPrefix($prefix)
  43. {
  44. $this->prefix = $prefix;
  45. if (!empty($this->prefix))
  46. {
  47. $this->prefix.= ".";
  48. }
  49. }
  50. /**
  51. * Adds a custom format
  52. *
  53. * @param TstampCustomFormat custom format
  54. */
  55. public function addFormat(TstampCustomFormat $cf)
  56. {
  57. $this->customFormats[] = $cf;
  58. }
  59. /**
  60. * Create the timestamps. Custom ones are done before
  61. * the standard ones.
  62. *
  63. * @throws BuildException
  64. */
  65. public function main()
  66. {
  67. foreach ($this->customFormats as $cf)
  68. {
  69. $cf->execute($this);
  70. }
  71. $dstamp = strftime('%Y%m%d');
  72. $this->prefixProperty('DSTAMP', $dstamp);
  73. $tstamp = strftime('%H%M');
  74. $this->prefixProperty('TSTAMP', $tstamp);
  75. $today = strftime('%B %d %Y');
  76. $this->prefixProperty('TODAY', $today);
  77. }
  78. /**
  79. * helper that encapsulates prefix logic and property setting
  80. * policy (i.e. we use setNewProperty instead of setProperty).
  81. */
  82. public function prefixProperty($name, $value)
  83. {
  84. $this->getProject()->setNewProperty($this->prefix . $name, $value);
  85. }
  86. }
  87. /**
  88. * @package phing.tasks.system
  89. */
  90. class TstampCustomFormat
  91. {
  92. private $propertyName = "";
  93. private $pattern = "";
  94. private $locale = "";
  95. /**
  96. * The property to receive the date/time string in the given pattern
  97. *
  98. * @param propertyName the name of the property.
  99. */
  100. public function setProperty($propertyName)
  101. {
  102. $this->propertyName = $propertyName;
  103. }
  104. /**
  105. * The date/time pattern to be used. The values are as
  106. * defined by the PHP strftime() function.
  107. *
  108. * @param pattern
  109. */
  110. public function setPattern($pattern)
  111. {
  112. $this->pattern = $pattern;
  113. }
  114. /**
  115. * The locale used to create date/time string.
  116. *
  117. * @param locale
  118. */
  119. public function setLocale($locale)
  120. {
  121. $this->locale = $locale;
  122. }
  123. /**
  124. * validate parameter and execute the format.
  125. *
  126. * @param TstampTask reference to task
  127. */
  128. public function execute(TstampTask $tstamp)
  129. {
  130. if (empty($this->propertyName))
  131. {
  132. throw new BuildException("property attribute must be provided");
  133. }
  134. if (empty($this->pattern))
  135. {
  136. throw new BuildException("pattern attribute must be provided");
  137. }
  138. if (!empty($this->locale))
  139. {
  140. setlocale(LC_ALL, $this->locale);
  141. }
  142. $value = strftime($this->pattern);
  143. $tstamp->prefixProperty($this->propertyName, $value);
  144. if (!empty($this->locale))
  145. {
  146. // reset locale
  147. setlocale(LC_ALL, NULL);
  148. }
  149. }
  150. }