PhpEvalTask.php 5.6 KB

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