TaskdefTask.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /*
  3. * $Id: TaskdefTask.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. include_once 'phing/system/io/PhingFile.php';
  23. /**
  24. * Register a task for use within a buildfile.
  25. *
  26. * This is for registering your own tasks -- or any non-core Task -- for use within a buildfile.
  27. * If you find that you are using a particular class frequently, you may want to edit the
  28. * phing/tasks/defaults.properties file so that it is included by default. You may also
  29. * want to submit it (if LGPL or compatible license) to be included in Phing distribution.
  30. *
  31. * <pre>
  32. * <taskdef name="mytag" classname="path.to.MyHandlingClass"/>
  33. * .
  34. * .
  35. * <mytag param1="val1" param2="val2"/>
  36. * </pre>
  37. *
  38. * TODO:
  39. * -- possibly refactor since this is almost the same as TypeDefTask
  40. * (right now these are just too simple to really justify creating an abstract class)
  41. *
  42. * @author Hans Lellelid <hans@xmpl.org>
  43. * @version $Revision: 905 $
  44. * @package phing.tasks.system
  45. */
  46. class TaskdefTask extends Task {
  47. /** Tag name for task that will be used in XML */
  48. private $name;
  49. /**
  50. * Classname of task to register.
  51. * This can be a dot-path -- relative to a location on PHP include_path.
  52. * E.g. path.to.MyClass -> path/to/MyClass.php
  53. * @var string
  54. */
  55. private $classname;
  56. /**
  57. * Path to add to PHP include_path to aid in finding specified class.
  58. * @var Path
  59. */
  60. private $classpath;
  61. /**
  62. * Refid to already defined classpath
  63. */
  64. private $classpathId;
  65. /**
  66. * Name of file to load multiple definitions from.
  67. * @var string
  68. */
  69. private $typeFile;
  70. /**
  71. * Set the classpath to be used when searching for component being defined
  72. *
  73. * @param Path $classpath An Path object containing the classpath.
  74. */
  75. public function setClasspath(Path $classpath) {
  76. if ($this->classpath === null) {
  77. $this->classpath = $classpath;
  78. } else {
  79. $this->classpath->append($classpath);
  80. }
  81. }
  82. /**
  83. * Create the classpath to be used when searching for component being defined
  84. */
  85. public function createClasspath() {
  86. if ($this->classpath === null) {
  87. $this->classpath = new Path($this->project);
  88. }
  89. return $this->classpath->createPath();
  90. }
  91. /**
  92. * Reference to a classpath to use when loading the files.
  93. */
  94. public function setClasspathRef(Reference $r) {
  95. $this->classpathId = $r->getRefId();
  96. $this->createClasspath()->setRefid($r);
  97. }
  98. /**
  99. * Sets the name that will be used in XML buildfile.
  100. * @param string $name
  101. */
  102. public function setName($name) {
  103. $this->name = $name;
  104. }
  105. /**
  106. * Sets the class name / dotpath to use.
  107. * @param string $class
  108. */
  109. public function setClassname($class) {
  110. $this->classname = $class;
  111. }
  112. /**
  113. * Sets the file of definitionas to use to use.
  114. * @param string $file
  115. */
  116. public function setFile($file) {
  117. $this->typeFile = $file;
  118. }
  119. /** Main entry point */
  120. public function main() {
  121. if ($this->typeFile === null &&
  122. ($this->name === null || $this->classname === null)) {
  123. throw new BuildException("You must specify name and class attributes for <taskdef>.");
  124. }
  125. if ($this->typeFile == null) {
  126. $this->log("Task " . $this->name . " will be handled by class " . $this->classname, Project::MSG_VERBOSE);
  127. $this->project->addTaskDefinition($this->name, $this->classname, $this->classpath);
  128. } else {
  129. try { // try to load taskdefs given in file
  130. $props = new Properties();
  131. $in = new PhingFile((string) $this->typeFile);
  132. if ($in === null) {
  133. throw new BuildException("Can't load task list {$this->typeFile}");
  134. }
  135. $props->load($in);
  136. $enum = $props->propertyNames();
  137. foreach($enum as $key) {
  138. $value = $props->getProperty($key);
  139. $this->project->addTaskDefinition($key, $value, $this->classpath);
  140. }
  141. } catch (IOException $ioe) {
  142. throw new BuildException("Can't load task list {$this->typeFile}");
  143. }
  144. }
  145. }
  146. }