ProjectHandler.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /*
  3. * $Id: ProjectHandler.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. require_once 'phing/system/io/PhingFile.php';
  23. /**
  24. * Handler class for the <project> XML element This class handles all elements
  25. * under the <project> element.
  26. *
  27. * @author Andreas Aderhold <andi@binarycloud.com>
  28. * @copyright (c) 2001,2002 THYRELL. All rights reserved
  29. * @version $Revision: 905 $ $Date: 2010-10-05 18:28:03 +0200 (Tue, 05 Oct 2010) $
  30. * @access public
  31. * @package phing.parser
  32. */
  33. class ProjectHandler extends AbstractHandler {
  34. /**
  35. * The phing project configurator object.
  36. * @var ProjectConfigurator
  37. */
  38. private $configurator;
  39. /**
  40. * Constructs a new ProjectHandler
  41. *
  42. * @param object the ExpatParser object
  43. * @param object the parent handler that invoked this handler
  44. * @param object the ProjectConfigurator object
  45. * @access public
  46. */
  47. function __construct($parser, $parentHandler, $configurator) {
  48. $this->configurator = $configurator;
  49. parent::__construct($parser, $parentHandler);
  50. }
  51. /**
  52. * Executes initialization actions required to setup the project. Usually
  53. * this method handles the attributes of a tag.
  54. *
  55. * @param string the tag that comes in
  56. * @param array attributes the tag carries
  57. * @param object the ProjectConfigurator object
  58. * @throws ExpatParseException if attributes are incomplete or invalid
  59. * @access public
  60. */
  61. function init($tag, $attrs) {
  62. $def = null;
  63. $name = null;
  64. $id = null;
  65. $desc = null;
  66. $baseDir = null;
  67. $ver = null;
  68. // some shorthands
  69. $project = $this->configurator->project;
  70. $buildFileParent = $this->configurator->buildFileParent;
  71. foreach ($attrs as $key => $value) {
  72. if ($key === "default") {
  73. $def = $value;
  74. } elseif ($key === "name") {
  75. $name = $value;
  76. } elseif ($key === "id") {
  77. $id = $value;
  78. } elseif ($key === "basedir") {
  79. $baseDir = $value;
  80. } elseif ($key === "description") {
  81. $desc = $value;
  82. } elseif ($key === "phingVersion") {
  83. $ver = $value;
  84. } else {
  85. throw new ExpatParseException("Unexpected attribute '$key'");
  86. }
  87. }
  88. // these things get done no matter what
  89. if (null != $name) {
  90. $canonicalName = self::canonicalName($name);
  91. $this->configurator->setCurrentProjectName($canonicalName);
  92. $project->setUserProperty("phing.file.{$canonicalName}",
  93. (string) $this->configurator->getBuildFile());
  94. }
  95. if (!$this->configurator->isIgnoringProjectTag()) {
  96. if ($def === null) {
  97. throw new ExpatParseException(
  98. "The default attribute of project is required");
  99. }
  100. $project->setDefaultTarget($def);
  101. if ($name !== null) {
  102. $project->setName($name);
  103. $project->addReference($name, $project);
  104. }
  105. if ($id !== null) {
  106. $project->addReference($id, $project);
  107. }
  108. if ($desc !== null) {
  109. $project->setDescription($desc);
  110. }
  111. if($ver !== null) {
  112. $project->setPhingVersion($ver);
  113. }
  114. if ($project->getProperty("project.basedir") !== null) {
  115. $project->setBasedir($project->getProperty("project.basedir"));
  116. } else {
  117. if ($baseDir === null) {
  118. $project->setBasedir($buildFileParent->getAbsolutePath());
  119. } else {
  120. // check whether the user has specified an absolute path
  121. $f = new PhingFile($baseDir);
  122. if ($f->isAbsolute()) {
  123. $project->setBasedir($baseDir);
  124. } else {
  125. $project->setBaseDir($project->resolveFile($baseDir, new PhingFile(getcwd())));
  126. }
  127. }
  128. }
  129. }
  130. }
  131. /**
  132. * Handles start elements within the <project> tag by creating and
  133. * calling the required handlers for the detected element.
  134. *
  135. * @param string the tag that comes in
  136. * @param array attributes the tag carries
  137. * @throws ExpatParseException if a unxepected element occurs
  138. * @access public
  139. */
  140. function startElement($name, $attrs) {
  141. $project = $this->configurator->project;
  142. $types = $project->getDataTypeDefinitions();
  143. if ($name == "target") {
  144. $tf = new TargetHandler($this->parser, $this, $this->configurator);
  145. $tf->init($name, $attrs);
  146. } elseif (isset($types[$name])) {
  147. $tyf = new DataTypeHandler($this->parser, $this, $this->configurator);
  148. $tyf->init($name, $attrs);
  149. } else {
  150. $tf = new TaskHandler($this->parser, $this, $this->configurator);
  151. $tf->init($name, $attrs);
  152. }
  153. }
  154. static function canonicalName ($name) {
  155. return preg_replace('/\W/', '_', strtolower($name));
  156. }
  157. }