TargetHandler.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * $Id: TargetHandler.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/parser/AbstractHandler.php';
  22. /**
  23. * The target handler class.
  24. *
  25. * This class handles the occurance of a <target> tag and it's possible
  26. * nested tags (datatypes and tasks).
  27. *
  28. * @author Andreas Aderhold <andi@binarycloud.com>
  29. * @copyright 2001,2002 THYRELL. All rights reserved
  30. * @version $Revision: 905 $
  31. * @package phing.parser
  32. */
  33. class TargetHandler extends AbstractHandler {
  34. /**
  35. * Reference to the target object that represents the currently parsed
  36. * target.
  37. * @var object the target instance
  38. */
  39. private $target;
  40. /**
  41. * The phing project configurator object
  42. * @var ProjectConfigurator
  43. */
  44. private $configurator;
  45. /**
  46. * Constructs a new TargetHandler
  47. *
  48. * @param object the ExpatParser object
  49. * @param object the parent handler that invoked this handler
  50. * @param object the ProjectConfigurator object
  51. */
  52. function __construct(AbstractSAXParser $parser, AbstractHandler $parentHandler, ProjectConfigurator $configurator) {
  53. parent::__construct($parser, $parentHandler);
  54. $this->configurator = $configurator;
  55. }
  56. /**
  57. * Executes initialization actions required to setup the data structures
  58. * related to the tag.
  59. * <p>
  60. * This includes:
  61. * <ul>
  62. * <li>creation of the target object</li>
  63. * <li>calling the setters for attributes</li>
  64. * <li>adding the target to the project</li>
  65. * <li>adding a reference to the target (if id attribute is given)</li>
  66. * </ul>
  67. *
  68. * @param string the tag that comes in
  69. * @param array attributes the tag carries
  70. * @throws ExpatParseException if attributes are incomplete or invalid
  71. */
  72. function init($tag, $attrs) {
  73. $name = null;
  74. $depends = "";
  75. $ifCond = null;
  76. $unlessCond = null;
  77. $id = null;
  78. $description = null;
  79. foreach($attrs as $key => $value) {
  80. if ($key==="name") {
  81. $name = (string) $value;
  82. } else if ($key==="depends") {
  83. $depends = (string) $value;
  84. } else if ($key==="if") {
  85. $ifCond = (string) $value;
  86. } else if ($key==="unless") {
  87. $unlessCond = (string) $value;
  88. } else if ($key==="id") {
  89. $id = (string) $value;
  90. } else if ($key==="description") {
  91. $description = (string)$value;
  92. } else {
  93. throw new ExpatParseException("Unexpected attribute '$key'", $this->parser->getLocation());
  94. }
  95. }
  96. if ($name === null) {
  97. throw new ExpatParseException("target element appears without a name attribute", $this->parser->getLocation());
  98. }
  99. // shorthand
  100. $project = $this->configurator->project;
  101. // check to see if this target is a dup within the same file
  102. if (isset($this->configurator->getCurrentTargets[$name])) {
  103. throw new BuildException("Duplicate target: $targetName",
  104. $this->parser->getLocation());
  105. }
  106. $this->target = new Target();
  107. $this->target->setName($name);
  108. $this->target->setIf($ifCond);
  109. $this->target->setUnless($unlessCond);
  110. $this->target->setDescription($description);
  111. // take care of dependencies
  112. if (strlen($depends) > 0) {
  113. $this->target->setDepends($depends);
  114. }
  115. $usedTarget = false;
  116. // check to see if target with same name is already defined
  117. $projectTargets = $project->getTargets();
  118. if (isset($projectTargets[$name])) {
  119. $project->log("Already defined in main or a previous import, " .
  120. "ignore {$name}", Project::MSG_VERBOSE);
  121. } else {
  122. $project->addTarget($name, $this->target);
  123. if ($id !== null && $id !== "") {
  124. $project->addReference($id, $this->target);
  125. }
  126. $usedTarget = true;
  127. }
  128. if ($this->configurator->isIgnoringProjectTag() &&
  129. $this->configurator->getCurrentProjectName() != null &&
  130. strlen($this->configurator->getCurrentProjectName()) != 0) {
  131. // In an impored file (and not completely
  132. // ignoring the project tag)
  133. $newName = $this->configurator->getCurrentProjectName() . "." . $name;
  134. if ($usedTarget) {
  135. // clone needs to make target->children a shared reference
  136. $newTarget = clone $this->target;
  137. } else {
  138. $newTarget = $this->target;
  139. }
  140. $newTarget->setName($newName);
  141. $ct = $this->configurator->getCurrentTargets();
  142. $ct[$newName] = $newTarget;
  143. $project->addTarget($newName, $newTarget);
  144. }
  145. }
  146. /**
  147. * Checks for nested tags within the current one. Creates and calls
  148. * handlers respectively.
  149. *
  150. * @param string the tag that comes in
  151. * @param array attributes the tag carries
  152. */
  153. function startElement($name, $attrs) {
  154. // shorthands
  155. $project = $this->configurator->project;
  156. $types = $project->getDataTypeDefinitions();
  157. if (isset($types[$name])) {
  158. $th = new DataTypeHandler($this->parser, $this, $this->configurator, $this->target);
  159. $th->init($name, $attrs);
  160. } else {
  161. $tmp = new TaskHandler($this->parser, $this, $this->configurator, $this->target, null, $this->target);
  162. $tmp->init($name, $attrs);
  163. }
  164. }
  165. }