/build/ext/phing/classes/phing/tasks/system/PhpEvalTask.php

http://github.com/alexgorbatchev/SyntaxHighlighter · PHP · 169 lines · 90 code · 27 blank · 52 comment · 11 complexity · ab85229c63132de70a72711c18f64053 MD5 · raw file

  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. 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: 1.7 $
  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; // 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. $retval = null;
  53. if ($this->function !== null) {
  54. $retval = $this->callFunction();
  55. } elseif ($this->expression !== null) {
  56. $retval = $this->evalExpression();
  57. }
  58. if ($this->returnProperty !== null) {
  59. $this->project->setProperty($this->returnProperty, $retval);
  60. }
  61. }
  62. /**
  63. * Calls function and returns results.
  64. * @return mixed
  65. */
  66. protected function callFunction() {
  67. if ($this->class !== null) {
  68. // import the classname & unqualify it, if necessary
  69. $this->class = Phing::import($this->class);
  70. $user_func = array($this->class, $this->function);
  71. $h_func = $this->class . '::' . $this->function; // human-readable (for log)
  72. } else {
  73. $user_func = $this->function;
  74. $h_func = $user_func; // human-readable (for log)
  75. }
  76. // put parameters into simple array
  77. $params = array();
  78. foreach($this->params as $p) {
  79. $params[] = $p->getValue();
  80. }
  81. $this->log("Calling PHP function: " . $h_func . "()");
  82. foreach($params as $p) {
  83. $this->log(" param: " . $p, Project::MSG_VERBOSE);
  84. }
  85. $return = call_user_func_array($user_func, $params);
  86. return $return;
  87. }
  88. /**
  89. * Evaluates expression and returns resulting value.
  90. * @return mixed
  91. */
  92. protected function evalExpression() {
  93. $this->log("Evaluating PHP expression: " . $this->expression);
  94. if (!StringHelper::endsWith(';', trim($this->expression))) {
  95. $this->expression .= ';';
  96. }
  97. $retval = null;
  98. eval('$retval = ' . $this->expression);
  99. return $retval;
  100. }
  101. /** Set function to execute */
  102. public function setFunction($f) {
  103. $this->function = $f;
  104. }
  105. /** Set [static] class which contains function to execute */
  106. public function setClass($c) {
  107. $this->class = $c;
  108. }
  109. /** Sets property name to set with return value of function or expression.*/
  110. public function setReturnProperty($r) {
  111. $this->returnProperty = $r;
  112. }
  113. /** Set PHP expression to evaluate. */
  114. public function addText($expression) {
  115. $this->expression = $expression;
  116. }
  117. /** Set PHP expression to evaluate. */
  118. public function setExpression($expression) {
  119. $this->expression = $expression;
  120. }
  121. /** Add a nested <param> tag. */
  122. public function createParam() {
  123. $p = new FunctionParam();
  124. $this->params[] = $p;
  125. return $p;
  126. }
  127. }
  128. /**
  129. * Supports the <param> nested tag for PhpTask.
  130. */
  131. class FunctionParam {
  132. private $val;
  133. public function setValue($v) {
  134. $this->val = $v;
  135. }
  136. public function addText($v) {
  137. $this->val = $v;
  138. }
  139. public function getValue() {
  140. return $this->val;
  141. }
  142. }