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
|
|
|
namespace Phing\Task\System; |
21
|
|
|
|
22
|
|
|
use Exception; |
23
|
|
|
use Phing\Exception\BuildException; |
24
|
|
|
use Phing\Task; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A wrapper task that lets you run tasks(s) when another set |
28
|
|
|
* of tasks fails. |
29
|
|
|
* |
30
|
|
|
* Inspired by {@link http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html} |
31
|
|
|
* |
32
|
|
|
* @author Michiel Rook <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class TryCatchTask extends Task |
35
|
|
|
{ |
36
|
|
|
protected $propertyName = ""; |
37
|
|
|
protected $referenceName = ''; |
38
|
|
|
|
39
|
|
|
protected $tryContainer = null; |
40
|
|
|
protected $catchContainer = null; |
41
|
|
|
protected $finallyContainer = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Main method |
45
|
|
|
* |
46
|
|
|
* @throws BuildException |
47
|
|
|
*/ |
48
|
4 |
|
public function main() |
49
|
|
|
{ |
50
|
4 |
|
$exc = null; |
51
|
|
|
|
52
|
4 |
|
if (empty($this->tryContainer)) { |
53
|
|
|
throw new BuildException('A nested <try> element is required'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
try { |
57
|
4 |
|
$this->tryContainer->perform(); |
58
|
4 |
|
} catch (BuildException $e) { |
59
|
4 |
|
if (!empty($this->propertyName)) { |
60
|
4 |
|
$this->project->setProperty($this->propertyName, $e->getMessage()); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
if (!empty($this->referenceName)) { |
64
|
3 |
|
$this->project->addReference($this->referenceName, $e); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
if (!empty($this->catchContainer)) { |
68
|
3 |
|
$this->catchContainer->perform(); |
69
|
|
|
} else { |
70
|
1 |
|
$exc = $e; |
71
|
|
|
} |
72
|
2 |
|
} finally { |
73
|
4 |
|
if (!empty($this->finallyContainer)) { |
74
|
4 |
|
$this->finallyContainer->perform(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
if (!empty($exc)) { |
79
|
1 |
|
throw $exc; |
80
|
|
|
} |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets the name of the property that will |
85
|
|
|
* contain the exception message. |
86
|
|
|
* |
87
|
|
|
* @param string $property |
88
|
|
|
*/ |
89
|
4 |
|
public function setProperty($property) |
90
|
|
|
{ |
91
|
4 |
|
$this->propertyName = (string) $property; |
92
|
4 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets the name of the reference that will |
96
|
|
|
* contain the exception. |
97
|
|
|
* |
98
|
|
|
* @param Exception $reference |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
3 |
|
public function setReference($reference) |
102
|
|
|
{ |
103
|
3 |
|
$this->referenceName = $reference; |
104
|
3 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add nested <try> element |
108
|
|
|
* |
109
|
|
|
*/ |
110
|
4 |
|
public function addTry(SequentialTask $container) |
111
|
|
|
{ |
112
|
4 |
|
$this->tryContainer = $container; |
113
|
4 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add nested <catch> element |
117
|
|
|
* |
118
|
|
|
*/ |
119
|
3 |
|
public function addCatch(SequentialTask $container) |
120
|
|
|
{ |
121
|
3 |
|
$this->catchContainer = $container; |
122
|
3 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add nested <finally> element |
126
|
|
|
* |
127
|
|
|
*/ |
128
|
4 |
|
public function addFinally(SequentialTask $container) |
129
|
|
|
{ |
130
|
4 |
|
$this->finallyContainer = $container; |
131
|
4 |
|
} |
132
|
|
|
} |
133
|
|
|
|