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