1<?php
2
3/*
4 * $Id: 565d694798b9660ff80ee9af6afbdcc621e9d02b $
5 *
6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 *
18 * This software consists of voluntary contributions made by many individuals
19 * and is licensed under the LGPL. For more information please see
20 * <http://phing.info>.
21 */
22
23require_once 'phing/Task.php';
24include_once 'phing/types/Path.php';
25
26/**
27 * Adds a normalized path to the PHP include_path.
28 *
29 * This provides a way to alter the include_path without editing any global php.ini settings
30 * or PHP_CLASSPATH environment variable.
31 *
32 * <code>
33 *   <includepath classpath="new/path/here"/>
34 * </code>
35 *
36 * @author    Hans Lellelid <hans@xmpl.org>
37 * @version   $Id: 565d694798b9660ff80ee9af6afbdcc621e9d02b $
38 * @package   phing.tasks.system
39 */
40class IncludePathTask extends Task {
41
42    /**
43     * Classname of task to register.
44     * This can be a dot-path -- relative to a location on PHP include_path.
45     * E.g. path.to.MyClass ->  path/to/MyClass.php
46     * @var string
47     */
48    private $classname;
49
50    /**
51     * Path to add to PHP include_path to aid in finding specified class.
52     * @var Path
53     */
54    private $classpath;
55
56    /**
57     * Refid to already defined classpath
58     */
59    private $classpathId;
60
61    /**
62     * Set the classpath to be used when searching for component being defined
63     *
64     * @param Path $classpath An Path object containing the classpath.
65     */
66    public function setClasspath(Path $classpath) {
67        if ($this->classpath === null) {
68            $this->classpath = $classpath;
69        } else {
70            $this->classpath->append($classpath);
71        }
72    }
73
74    /**
75     * Create the classpath to be used when searching for component being defined
76     */
77    public function createClasspath() {
78        if ($this->classpath === null) {
79            $this->classpath = new Path($this->project);
80        }
81        return $this->classpath->createPath();
82    }
83
84    /**
85     * Reference to a classpath to use when loading the files.
86     */
87    public function setClasspathRef(Reference $r) {
88        $this->classpathId = $r->getRefId();
89        $this->createClasspath()->setRefid($r);
90    }
91
92
93    /** Main entry point */
94    public function main() {
95
96        // Apparently casting to (string) no longer invokes __toString() automatically.
97        if (is_object($this->classpath)) {
98            $classpath = $this->classpath->__toString();
99        }
100
101        if (empty($classpath)) {
102            throw new BuildException("Provided classpath was empty.");
103        }
104
105        $curr_parts = Phing::explodeIncludePath();
106        $add_parts = Phing::explodeIncludePath($classpath);
107        $new_parts = array_diff($add_parts, $curr_parts);
108
109        if ($new_parts) {
110            $this->log("Prepending new include_path components: " . implode(PATH_SEPARATOR, $new_parts), Project::MSG_VERBOSE);
111            set_include_path(implode(PATH_SEPARATOR, array_merge($new_parts, $curr_parts)));
112        }
113
114    }
115}
116