1
|
|
<?php
|
2
|
|
/**
|
3
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
4
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
5
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
6
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
7
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
8
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
9
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
10
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
11
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
12
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
13
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
|
*
|
15
|
|
* This software consists of voluntary contributions made by many individuals
|
16
|
|
* and is licensed under the LGPL. For more information please see
|
17
|
|
* <http://phing.info>.
|
18
|
|
*/
|
19
|
|
|
20
|
|
/**
|
21
|
|
* Sets properties to the current time, or offsets from the current time.
|
22
|
|
* The default properties are TSTAMP, DSTAMP and TODAY;
|
23
|
|
*
|
24
|
|
* Based on Ant's Tstamp task.
|
25
|
|
*
|
26
|
|
* @author Michiel Rook <mrook@php.net>
|
27
|
|
* @package phing.tasks.system
|
28
|
|
* @since 2.2.0
|
29
|
|
*/
|
30
|
|
class TstampTask extends Task
|
31
|
|
{
|
32
|
|
private $customFormats = [];
|
33
|
|
|
34
|
|
private $prefix = "";
|
35
|
|
|
36
|
|
/**
|
37
|
|
* Set a prefix for the properties. If the prefix does not end with a "."
|
38
|
|
* one is automatically added.
|
39
|
|
*
|
40
|
|
* @param string $prefix the prefix to use.
|
41
|
|
*/
|
42
|
1
|
public function setPrefix($prefix)
|
43
|
|
{
|
44
|
1
|
$this->prefix = $prefix;
|
45
|
|
|
46
|
1
|
if (!empty($this->prefix)) {
|
47
|
1
|
$this->prefix .= ".";
|
48
|
|
}
|
49
|
|
}
|
50
|
|
|
51
|
|
/**
|
52
|
|
* Adds a custom format
|
53
|
|
*
|
54
|
|
* @param TstampCustomFormat custom format
|
55
|
|
*/
|
56
|
1
|
public function addFormat(TstampCustomFormat $cf)
|
57
|
|
{
|
58
|
1
|
$this->customFormats[] = $cf;
|
59
|
|
}
|
60
|
|
|
61
|
|
/**
|
62
|
|
* Create the timestamps. Custom ones are done before
|
63
|
|
* the standard ones.
|
64
|
|
*
|
65
|
|
* @throws BuildException
|
66
|
|
*/
|
67
|
1
|
public function main()
|
68
|
|
{
|
69
|
1
|
$d = $this->getNow();
|
70
|
|
|
71
|
1
|
foreach ($this->customFormats as $cf) {
|
72
|
1
|
$cf->execute($this, $d, $this->getLocation());
|
73
|
|
}
|
74
|
|
|
75
|
1
|
$dstamp = strftime('%Y%m%d', $d);
|
76
|
1
|
$this->prefixProperty('DSTAMP', $dstamp);
|
77
|
|
|
78
|
1
|
$tstamp = strftime('%H%M', $d);
|
79
|
1
|
$this->prefixProperty('TSTAMP', $tstamp);
|
80
|
|
|
81
|
1
|
$today = strftime('%B %d %Y', $d);
|
82
|
1
|
$this->prefixProperty('TODAY', $today);
|
83
|
|
}
|
84
|
|
|
85
|
|
/**
|
86
|
|
* helper that encapsulates prefix logic and property setting
|
87
|
|
* policy (i.e. we use setNewProperty instead of setProperty).
|
88
|
|
*
|
89
|
|
* @param $name
|
90
|
|
* @param $value
|
91
|
|
*/
|
92
|
1
|
public function prefixProperty($name, $value)
|
93
|
|
{
|
94
|
1
|
$this->getProject()->setNewProperty($this->prefix . $name, $value);
|
95
|
|
}
|
96
|
|
|
97
|
1
|
protected function getNow(): int
|
98
|
|
{
|
99
|
1
|
$property = $this->getProject()->getProperty('phing.tstamp.now.iso');
|
100
|
|
|
101
|
1
|
if ($property !== null && $property !== '') {
|
102
|
|
try {
|
103
|
1
|
$dateTime = new DateTime($property);
|
104
|
0
|
} catch (Exception $e) {
|
105
|
0
|
$this->log('magic property phing.tstamp.now.iso ignored as ' . $property . ' is not a valid number');
|
106
|
0
|
$dateTime = new DateTime();
|
107
|
|
}
|
108
|
|
|
109
|
1
|
return $dateTime->getTimestamp();
|
110
|
|
}
|
111
|
|
|
112
|
1
|
$property = $this->getProject()->getProperty('phing.tstamp.now');
|
113
|
|
|
114
|
1
|
$dateTime = (new DateTime())->getTimestamp();
|
115
|
|
|
116
|
1
|
if ($property !== null && $property !== '') {
|
117
|
1
|
$dateTime = DateTime::createFromFormat('U', $property);
|
118
|
1
|
if ($dateTime === false) {
|
119
|
0
|
$this->log('magic property phing.tstamp.now ignored as ' . $property . ' is not a valid number');
|
120
|
|
} else {
|
121
|
1
|
$dateTime = $dateTime->getTimestamp();
|
122
|
|
}
|
123
|
|
}
|
124
|
|
|
125
|
1
|
return $dateTime;
|
126
|
|
}
|
127
|
|
}
|