1<?php
2/*
3 *  $Id: 5d02fa25dc18b56093884ae756b1720aafb42937 $
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';
23
24/**
25 * Call another target in the same project.
26 *
27 * <samp>
28 *    <target name="foo">
29 *      <phingcall target="bar">
30 *        <property name="property1" value="aaaaa" />
31 *        <property name="foo" value="baz" />
32 *       </phingcall>
33 *    </target>
34 *
35 *    <target name="bar" depends="init">
36 *      <echo message="prop is ${property1} ${foo}" />
37 *    </target>
38 * </samp>
39 *
40 * This only works as expected if neither property1 nor foo are defined in the project itself.
41 *
42 * @author    Andreas Aderhold <andi@binarycloud.com>
43 * @copyright 2001,2002 THYRELL. All rights reserved
44 * @version   $Id: 5d02fa25dc18b56093884ae756b1720aafb42937 $
45 * @access    public
46 * @package   phing.tasks.system
47 */
48class PhingCallTask extends Task {
49
50    /**
51     * The called Phing task.
52     *
53     * @var PhingTask
54     */
55    private $callee;
56
57    /**
58     * The target to call.
59     *
60     * @var string
61     */
62    private $subTarget;
63
64    /**
65     * Whether to inherit all properties from current project.
66     *
67     * @var boolean
68     */
69    private $inheritAll = true;
70
71    /**
72     * Whether to inherit refs from current project.
73     *
74     * @var boolean
75     */
76    private $inheritRefs = false;
77
78    /**
79     *  If true, pass all properties to the new Phing project.
80     *  Defaults to true. Future use.
81     *  @param boolean new value
82     */
83    function setInheritAll($inherit) {
84        $this->inheritAll = (boolean) $inherit;
85    }
86
87    /**
88     *  If true, pass all references to the new Phing project.
89     *  Defaults to false. Future use.
90     *
91     *  @param boolean new value
92     */
93    function setInheritRefs($inheritRefs) {
94        $this->inheritRefs = (boolean) $inheritRefs;
95    }
96
97    /**
98     * Alias for createProperty
99     * @see createProperty()
100     */
101    function createParam() {
102        if ($this->callee === null) {
103            $this->init();
104        }
105        return $this->callee->createProperty();
106    }
107
108    /**
109     * Property to pass to the invoked target.
110     */
111    function createProperty() {
112        if ($this->callee === null) {
113            $this->init();
114        }
115        return $this->callee->createProperty();
116    }
117
118    /**
119     * Target to execute, required.
120     */
121    function setTarget($target) {
122        $this->subTarget = (string) $target;
123    }
124
125    /**
126     *  init this task by creating new instance of the phing task and
127     *  configuring it's by calling its own init method.
128     */
129    function init() {
130        $this->callee = $this->project->createTask("phing");
131        $this->callee->setOwningTarget($this->getOwningTarget());
132        $this->callee->setTaskName($this->getTaskName());
133        $this->callee->setHaltOnFailure(true);
134        $this->callee->setLocation($this->getLocation());
135        $this->callee->init();
136    }
137
138    /**
139     *  hand off the work to the phing task of ours, after setting it up
140     *  @throws BuildException on validation failure or if the target didn't
141     *  execute
142     */
143    function main() {
144
145        $this->log("Running PhingCallTask for target '" . $this->subTarget . "'", Project::MSG_DEBUG);
146        if ($this->callee === null) {
147            $this->init();
148        }
149
150        if ($this->subTarget === null) {
151            throw new BuildException("Attribute target is required.", $this->getLocation());
152        }
153
154        $this->callee->setPhingfile($this->project->getProperty("phing.file"));
155        $this->callee->setTarget($this->subTarget);
156        $this->callee->setInheritAll($this->inheritAll);
157        $this->callee->setInheritRefs($this->inheritRefs);
158        $this->callee->main();
159    }
160
161}
162