Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: PhpEvalTask.php 144 2007-02-05 15:19:00Z 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
 * Executes PHP function or evaluates expression and sets return value to a property.
26
 *
27
 *    WARNING:
28
 *        This task can, of course, be abused with devastating effects.  E.g. do not
29
 *        modify internal Phing classes unless you know what you are doing.
30
 *
31
 * @author   Hans Lellelid <hans@xmpl.org>
32
 * @version  $Revision: 1.7 $
33
 * @package  phing.tasks.system
34
 *
35
 * @todo Add support for evaluating expressions
36
 */
37
class PhpEvalTask extends Task {
38
 
39
    protected $expression; // Expression to evaluate
40
    protected $function; // Function to execute
41
    protected $class; // Class containing function to execute
42
    protected $returnProperty; // name of property to set to return value
43
    protected $params = array(); // parameters for function calls
44
 
45
    /** Main entry point. */
46
    function main() {
47
 
48
        if ($this->function === null && $this->expression === null) {
49
            throw new BuildException("You must specify a function to execute or PHP expression to evalute.", $this->location);
50
        }
51
 
52
        if ($this->function !== null && $this->expression !== null) {
53
            throw new BuildException("You can specify function or expression, but not both.", $this->location);
54
        }
55
 
56
        if ($this->expression !== null && !empty($this->params)) {
57
            throw new BuildException("You cannot use nested <param> tags when evaluationg a PHP expression.", $this->location);
58
        }
59
 
60
        $retval = null;
61
        if ($this->function !== null) {
62
            $retval = $this->callFunction();
63
        } elseif ($this->expression !== null) {
64
            $retval = $this->evalExpression();
65
        }
66
 
67
        if ($this->returnProperty !== null) {
68
            $this->project->setProperty($this->returnProperty, $retval);
69
        }
70
    }
71
 
72
    /**
73
     * Calls function and returns results.
74
     * @return mixed
75
     */
76
    protected function callFunction() {
77
 
78
        if ($this->class !== null) {
79
            // import the classname & unqualify it, if necessary
80
            $this->class = Phing::import($this->class);
81
 
82
            $user_func = array($this->class, $this->function);
83
            $h_func = $this->class . '::' . $this->function; // human-readable (for log)
84
        } else {
85
            $user_func = $this->function;
86
            $h_func = $user_func; // human-readable (for log)
87
        }
88
 
89
        // put parameters into simple array
90
        $params = array();
91
        foreach($this->params as $p) {
92
            $params[] = $p->getValue();
93
        }
94
 
95
        $this->log("Calling PHP function: " . $h_func . "()");
96
        foreach($params as $p) {
97
            $this->log("  param: " . $p, Project::MSG_VERBOSE);
98
        }
99
 
100
        $return = call_user_func_array($user_func, $params);
101
        return $return;
102
    }
103
 
104
    /**
105
     * Evaluates expression and returns resulting value.
106
     * @return mixed
107
     */
108
    protected function evalExpression() {
109
        $this->log("Evaluating PHP expression: " . $this->expression);
110
        if (!StringHelper::endsWith(';', trim($this->expression))) {
111
            $this->expression .= ';';
112
        }
113
        $retval = null;
114
        eval('$retval = ' . $this->expression);
115
        return $retval;
116
    }
117
 
118
    /** Set function to execute */
119
    public function setFunction($f) {
120
       $this->function = $f;
121
    }
122
 
123
    /** Set [static] class which contains function to execute */
124
    public function setClass($c) {
125
       $this->class = $c;
126
    }
127
 
128
    /** Sets property name to set with return value of function or expression.*/
129
    public function setReturnProperty($r) {
130
       $this->returnProperty = $r;
131
    }
132
 
133
    /** Set PHP expression to evaluate. */
134
    public function addText($expression) {
135
        $this->expression = $expression;
136
    }
137
 
138
    /** Set PHP expression to evaluate. */
139
    public function setExpression($expression) {
140
        $this->expression = $expression;
141
    }
142
 
143
    /** Add a nested <param> tag. */
144
    public function createParam() {
145
        $p = new FunctionParam();
146
        $this->params[] = $p;
147
        return $p;
148
    }
149
}
150
 
151
/**
152
 * Supports the <param> nested tag for PhpTask.
153
 */
154
class FunctionParam {
155
 
156
    private $val;
157
 
158
    public function setValue($v) {
159
        $this->val = $v;
160
    }
161
 
162
    public function addText($v) {
163
        $this->val = $v;
164
    }
165
 
166
    public function getValue() {
167
        return $this->val;
168
    }
169
}