ImportTask.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * $Id: ImportTask.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. require_once 'phing/system/io/FileSystem.php';
  23. require_once 'phing/system/io/PhingFile.php';
  24. require_once 'phing/parser/ProjectConfigurator.php';
  25. /**
  26. * Imports another build file into the current project.
  27. *
  28. * Targets and properties of the imported file can be overrridden
  29. * by targets and properties of the same name declared in the importing file.
  30. *
  31. * The imported file will have a new synthetic property of
  32. * "phing.file.<projectname>" declared which gives the full path to the
  33. * imported file. Additionally each target in the imported file will be
  34. * declared twice: once with the normal name and once with "<projectname>."
  35. * prepended. The "<projectname>.<targetname>" synthetic targets allow the
  36. * importing file a mechanism to call the imported files targets as
  37. * dependencies or via the <phing> or <phingcall> task mechanisms.
  38. *
  39. * @author Bryan Davis <bpd@keynetics.com>
  40. * @version $Revision: 905 $
  41. * @package phing.tasks.system
  42. */
  43. class ImportTask extends Task {
  44. /**
  45. * @var FileSystem
  46. */
  47. protected $fs;
  48. /**
  49. * @var PhingFile
  50. */
  51. protected $file = null;
  52. /**
  53. * @var bool
  54. */
  55. protected $optional = false;
  56. /**
  57. * Initialize task.
  58. * @return void
  59. */
  60. public function init () {
  61. $this->fs = FileSystem::getFileSystem();
  62. } //end init
  63. /**
  64. * Set the file to import.
  65. * @param string $f Path to file
  66. * @return void
  67. */
  68. public function setFile ($f) {
  69. $this->file = $f;
  70. }
  71. /**
  72. * Is this include optional?
  73. * @param bool $opt If true, do not stop the build if the file does not
  74. * exist
  75. * @return void
  76. */
  77. public function setOptional ($opt) {
  78. $this->optional = $opt;
  79. }
  80. /**
  81. * Parse a Phing build file and copy the properties, tasks, data types and
  82. * targets it defines into the current project.
  83. *
  84. * @return void
  85. */
  86. public function main () {
  87. if (!isset($this->file)) {
  88. throw new BuildException("Missing attribute 'file'");
  89. }
  90. $file = new PhingFile($this->file);
  91. if (!$file->isAbsolute()) {
  92. $file = new PhingFile($this->project->getBasedir(), $this->file);
  93. }
  94. if (!$file->exists()) {
  95. $msg = "Unable to find build file: {$file->getPath()}";
  96. if ($this->optional) {
  97. $this->log($msg . '... skipped');
  98. } else {
  99. throw new BuildException($msg);
  100. }
  101. }
  102. $ctx = $this->project->getReference("phing.parsing.context");
  103. $cfg = $ctx->getConfigurator();
  104. if (null !== $cfg && $cfg->isParsing()) {
  105. // because there isn't a top level implicit target in phing like there is
  106. // in Ant 1.6, we will be called as soon as our xml is parsed. This isn't
  107. // really what we want to have happen. Instead we will register ourself
  108. // with the parse context to be called at the end of the current file's
  109. // parse phase.
  110. $cfg->delayTaskUntilParseEnd($this);
  111. } else {
  112. // Import xml file into current project scope
  113. // Since this is delayed until after the importing file has been
  114. // processed, the properties and targets of this new file may not take
  115. // effect if they have alreday been defined in the outer scope.
  116. $this->log("Importing configuration from {$file->getName()}", Project::MSG_VERBOSE);
  117. ProjectConfigurator::configureProject($this->project, $file);
  118. $this->log("Configuration imported.", Project::MSG_VERBOSE);
  119. }
  120. } //end main
  121. } //end ImportTask