Task.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /*
  3. * $Id: Task.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/ProjectComponent.php';
  22. include_once 'phing/RuntimeConfigurable.php';
  23. /**
  24. * The base class for all Tasks.
  25. *
  26. * Use {@link Project#createTask} to register a new Task.
  27. *
  28. * @author Andreas Aderhold <andi@binarycloud.com>
  29. * @copyright � 2001,2002 THYRELL. All rights reserved
  30. * @version $Revision: 905 $
  31. * @see Project#createTask()
  32. * @package phing
  33. */
  34. abstract class Task extends ProjectComponent {
  35. /**
  36. * @var Target owning Target object
  37. */
  38. protected $target;
  39. /**
  40. * @var string description of the task
  41. */
  42. protected $description;
  43. /**
  44. * @var string internal taskname (req)
  45. */
  46. protected $taskType;
  47. /**
  48. * @var string Taskname for logger
  49. */
  50. protected $taskName;
  51. /**
  52. * @var Location stored buildfile location
  53. */
  54. protected $location;
  55. /**
  56. * @var RuntimeConfigurable wrapper of the task
  57. */
  58. protected $wrapper;
  59. /**
  60. * Sets the owning target this task belongs to.
  61. *
  62. * @param Target Reference to owning target
  63. */
  64. public function setOwningTarget(Target $target) {
  65. $this->target = $target;
  66. }
  67. /**
  68. * Returns the owning target of this task.
  69. *
  70. * @return Target The target object that owns this task
  71. */
  72. public function getOwningTarget() {
  73. return $this->target;
  74. }
  75. /**
  76. * Returns the name of task, used only for log messages
  77. *
  78. * @return string Name of this task
  79. */
  80. public function getTaskName() {
  81. if ($this->taskName === null) {
  82. // if no task name is set, then it's possible
  83. // this task was created from within another task. We don't
  84. // therefore know the XML tag name for this task, so we'll just
  85. // use the class name stripped of "task" suffix. This is only
  86. // for log messages, so we don't have to worry much about accuracy.
  87. return preg_replace('/task$/i', '', get_class($this));
  88. }
  89. return $this->taskName;
  90. }
  91. /**
  92. * Sets the name of this task for log messages
  93. *
  94. * @return string A string representing the name of this task for log
  95. */
  96. public function setTaskName($name) {
  97. $this->taskName = (string) $name;
  98. }
  99. /**
  100. * Returns the name of the task under which it was invoked,
  101. * usually the XML tagname
  102. *
  103. * @return string The type of this task (XML Tag)
  104. */
  105. public function getTaskType() {
  106. return $this->taskType;
  107. }
  108. /**
  109. * Sets the type of the task. Usually this is the name of the XML tag
  110. *
  111. * @param string The type of this task (XML Tag)
  112. */
  113. public function setTaskType($name) {
  114. $this->taskType = (string) $name;
  115. }
  116. /**
  117. * Returns a name
  118. *
  119. */
  120. protected function getRegisterSlot($slotName) {
  121. return Register::getSlot('task.' . $this->getTaskName() . '.' . $slotName);
  122. }
  123. /**
  124. * Provides a project level log event to the task.
  125. *
  126. * @param string The message to log
  127. * @param integer The priority of the message
  128. * @see BuildEvent
  129. * @see BuildListener
  130. */
  131. function log($msg, $level = Project::MSG_INFO) {
  132. $this->project->logObject($this, $msg, $level);
  133. }
  134. /**
  135. * Sets a textual description of the task
  136. *
  137. * @param string $desc The text describing the task
  138. */
  139. public function setDescription($desc) {
  140. $this->description = $desc;
  141. }
  142. /**
  143. * Returns the textual description of the task
  144. *
  145. * @return string The text description of the task
  146. */
  147. public function getDescription() {
  148. return $this->description;
  149. }
  150. /**
  151. * Called by the parser to let the task initialize properly.
  152. * Should throw a BuildException if something goes wrong with the build
  153. *
  154. * This is abstract here, but may not be overloaded by subclasses.
  155. *
  156. * @throws BuildException
  157. */
  158. public function init() {
  159. }
  160. /**
  161. * Called by the project to let the task do it's work. This method may be
  162. * called more than once, if the task is invoked more than once. For
  163. * example, if target1 and target2 both depend on target3, then running
  164. * <em>phing target1 target2</em> will run all tasks in target3 twice.
  165. *
  166. * Should throw a BuildException if someting goes wrong with the build
  167. *
  168. * This is abstract here. Must be overloaded by real tasks.
  169. */
  170. abstract public function main();
  171. /**
  172. * Returns the location within the buildfile this task occurs. Used
  173. * by {@link BuildException} to give detailed error messages.
  174. *
  175. * @return Location The location object describing the position of this
  176. * task within the buildfile.
  177. */
  178. function getLocation() {
  179. return $this->location;
  180. }
  181. /**
  182. * Sets the location within the buildfile this task occurs. Called by
  183. * the parser to set location information.
  184. *
  185. * @param Location $location The location object describing the position of this
  186. * task within the buildfile.
  187. */
  188. function setLocation(Location $location) {
  189. $this->location = $location;
  190. }
  191. /**
  192. * Returns the wrapper object for runtime configuration
  193. *
  194. * @return RuntimeConfigurable The wrapper object used by this task
  195. */
  196. function getRuntimeConfigurableWrapper() {
  197. if ($this->wrapper === null) {
  198. $this->wrapper = new RuntimeConfigurable($this, $this->getTaskName());
  199. }
  200. return $this->wrapper;
  201. }
  202. /**
  203. * Sets the wrapper object this task should use for runtime
  204. * configurable elements.
  205. *
  206. * @param RuntimeConfigurable $wrapper The wrapper object this task should use
  207. */
  208. function setRuntimeConfigurableWrapper(RuntimeConfigurable $wrapper) {
  209. $this->wrapper = $wrapper;
  210. }
  211. /**
  212. * Configure this task if it hasn't been done already.
  213. */
  214. public function maybeConfigure() {
  215. if ($this->wrapper !== null) {
  216. $this->wrapper->maybeConfigure($this->project);
  217. }
  218. }
  219. /**
  220. * Perfrom this task
  221. */
  222. public function perform() {
  223. try { // try executing task
  224. $this->project->fireTaskStarted($this);
  225. $this->maybeConfigure();
  226. $this->main();
  227. $this->project->fireTaskFinished($this, $null=null);
  228. } catch (Exception $exc) {
  229. if ($exc instanceof BuildException) {
  230. if ($exc->getLocation() === null) {
  231. $exc->setLocation($this->getLocation());
  232. }
  233. }
  234. $this->project->fireTaskFinished($this, $exc);
  235. throw $exc;
  236. }
  237. }
  238. }