1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Task\System; |
22
|
|
|
|
23
|
|
|
use DateTime; |
24
|
|
|
use Error; |
25
|
|
|
use Exception; |
26
|
|
|
use IntlDateFormatter; |
27
|
|
|
use Phing\Exception\BuildException; |
28
|
|
|
use Phing\Project; |
29
|
|
|
use Phing\Task; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Sets properties to the current time, or offsets from the current time. |
33
|
|
|
* The default properties are TSTAMP, DSTAMP and TODAY;. |
34
|
|
|
* |
35
|
|
|
* Based on Ant's Tstamp task. |
36
|
|
|
* |
37
|
|
|
* @author Michiel Rook <[email protected]> |
38
|
|
|
* |
39
|
|
|
* @since 2.2.0 |
40
|
|
|
*/ |
41
|
|
|
class TstampTask extends Task |
42
|
|
|
{ |
43
|
|
|
/** @var \Phing\Task\System\TstampCustomFormat[] */ |
44
|
|
|
private $customFormats = []; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
private $prefix = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set a prefix for the properties. |
51
|
|
|
*/ |
52
|
1 |
|
public function setPrefix(string $prefix): void |
53
|
|
|
{ |
54
|
1 |
|
$this->prefix = $prefix; |
55
|
|
|
|
56
|
1 |
|
if (!empty($this->prefix)) { |
57
|
1 |
|
$this->prefix .= '.'; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param TstampCustomFormat $format object representing `<format/>` tag |
63
|
|
|
*/ |
64
|
24 |
|
public function addFormat(TstampCustomFormat $format): void |
65
|
|
|
{ |
66
|
24 |
|
$this->customFormats[] = $format; |
67
|
|
|
} |
68
|
|
|
|
69
|
28 |
|
public function init(): void |
70
|
|
|
{ |
71
|
|
|
// Testing class instead of extension to allow polyfills |
72
|
28 |
|
if (!class_exists(IntlDateFormatter::class)) { |
73
|
|
|
throw new BuildException('TstampTask requires Intl extension'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create the timestamps. Custom ones are done before the standard ones. |
79
|
|
|
*/ |
80
|
29 |
|
public function main(): void |
81
|
|
|
{ |
82
|
29 |
|
$unixTime = $this->getUnixTime(); |
83
|
|
|
|
84
|
29 |
|
foreach ($this->customFormats as $format) { |
85
|
24 |
|
$format->validate($this); |
86
|
24 |
|
$this->createProperty($format->propertyName, $unixTime, $format->pattern, $format->locale, $format->timezone); |
87
|
|
|
} |
88
|
|
|
|
89
|
29 |
|
$this->createProperty('DSTAMP', $unixTime, 'yyyyMMdd'); |
90
|
29 |
|
$this->createProperty('TSTAMP', $unixTime, 'HHmm'); |
91
|
29 |
|
$this->createProperty('TODAY', $unixTime); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $propertyName name of the property to be created |
96
|
|
|
* @param int $unixTimestamp unix timestamp to be converted |
97
|
|
|
* @param null|string $pattern ICU pattern, when null locale-dependent date pattern is used |
98
|
|
|
* @param null|string $locale locale to use with timestamp, when null PHP default locale is used |
99
|
|
|
* @param null|string $timezone timezone to use with timestamp, when null PHP default timezone is used |
100
|
|
|
*/ |
101
|
29 |
|
protected function createProperty(string $propertyName, int $unixTimestamp, ?string $pattern = null, ?string $locale = null, ?string $timezone = null): void |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
29 |
|
$formatter = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE, $timezone, IntlDateFormatter::GREGORIAN, $pattern); |
105
|
29 |
|
$value = $formatter->format($unixTimestamp); |
106
|
1 |
|
} catch (Error $e) { |
107
|
1 |
|
$value = ""; |
108
|
1 |
|
$this->log("Unable to format date (locale $locale) [{$e->getMessage()}]", Project::MSG_WARN); |
109
|
|
|
} |
110
|
29 |
|
$this->getProject()->setNewProperty($this->prefix . $propertyName, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
29 |
|
protected function getUnixTime(): int |
114
|
|
|
{ |
115
|
|
|
// phing.tstamp.now.iso |
116
|
29 |
|
$property = $this->getProject()->getProperty('phing.tstamp.now.iso'); |
117
|
29 |
|
if (null !== $property && '' !== $property) { |
118
|
|
|
try { |
119
|
3 |
|
$dateTime = new DateTime($property); |
120
|
|
|
|
121
|
3 |
|
return $dateTime->getTimestamp(); |
122
|
|
|
} catch (Exception $e) { |
123
|
|
|
$this->log('magic property phing.tstamp.now.iso ignored as ' . $property . ' is not a valid number'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// phing.tstamp.now |
128
|
26 |
|
$property = $this->getProject()->getProperty('phing.tstamp.now'); |
129
|
26 |
|
if (null !== $property && '' !== $property) { |
130
|
4 |
|
$dateTime = DateTime::createFromFormat('U', $property); |
131
|
4 |
|
if ($dateTime instanceof DateTime) { |
|
|
|
|
132
|
4 |
|
return $dateTime->getTimestamp(); |
133
|
|
|
} |
134
|
|
|
$this->log('magic property phing.tstamp.now ignored as ' . $property . ' is not a valid number'); |
135
|
|
|
} |
136
|
|
|
|
137
|
22 |
|
return time(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|