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