AdhocTaskdefTask.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * $Id: AdhocTaskdefTask.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/tasks/system/AdhocTask.php';
  22. /**
  23. * A class for creating adhoc tasks in build file.
  24. *
  25. * <target name="test-adhoc">
  26. * <adhoc-task name="foo"><![CDATA[
  27. *
  28. * class FooTest extends Task {
  29. * private $bar;
  30. *
  31. * function setBar($bar) {
  32. * $this->bar = $bar;
  33. * }
  34. *
  35. * function main() {
  36. * $this->log("In FooTest: " . $this->bar);
  37. * }
  38. * }
  39. *
  40. * ]]></adhoc-task>
  41. *
  42. * <foo bar="B.L.I.N.G"/>
  43. * </target>
  44. *
  45. * @author Hans Lellelid <hans@xmpl.org>
  46. * @version $Revision: 905 $
  47. * @package phing.tasks.system
  48. */
  49. class AdhocTaskdefTask extends AdhocTask
  50. {
  51. /**
  52. * The tag that refers to this task.
  53. */
  54. private $name;
  55. /**
  56. * Set the tag that will represent this adhoc task/type.
  57. * @param string $name
  58. */
  59. public function setName($name)
  60. {
  61. $this->name = $name;
  62. }
  63. /** Main entry point */
  64. public function main()
  65. {
  66. if ($this->name === null)
  67. {
  68. throw new BuildException("The name attribute is required for adhoc task definition.",$this->location);
  69. }
  70. $taskdefs = $this->getProject()->getTaskDefinitions();
  71. if (!isset($taskdefs[$this->name]))
  72. {
  73. $this->execute();
  74. $classes = $this->getNewClasses();
  75. if (count($classes) !== 1)
  76. {
  77. throw new BuildException("You must define one (and only one) class for AdhocTaskdefTask.");
  78. }
  79. $classname = array_shift($classes);
  80. // instantiate it to make sure it is an instance of Task
  81. $t = new $classname();
  82. if (!($t instanceof Task))
  83. {
  84. throw new BuildException("The adhoc class you defined must be an instance of phing.Task", $this->location);
  85. }
  86. $this->log("Task " . $this->name . " will be handled by class " . $classname, Project::MSG_VERBOSE);
  87. $this->project->addTaskDefinition($this->name, $classname);
  88. }
  89. }
  90. }