1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Adds a normalized path to the PHP include_path.
22
 *
23
 * This provides a way to alter the include_path without editing any global php.ini settings
24
 * or PHP_CLASSPATH environment variable.
25
 *
26
 * <code>
27
 *   <includepath classpath="new/path/here"/>
28
 * </code>
29
 *
30
 * @author  Hans Lellelid <hans@xmpl.org>
31
 * @package phing.tasks.system
32
 */
33
class IncludePathTask extends Task
34
{
35
    use ClasspathAware;
36

37
    /**
38
     * Classname of task to register.
39
     * This can be a dot-path -- relative to a location on PHP include_path.
40
     * E.g. path.to.MyClass ->  path/to/MyClass.php
41
     *
42
     * @var string
43
     */
44
    private $classname;
45

46
    /**
47
     * Whether to prepend, append or replace the include path
48
     *
49
     * @var string
50
     */
51
    private $mode = "prepend";
52

53
    /**
54
     * @param $mode
55
     * @throws BuildException
56
     */
57 0
    public function setMode($mode)
58
    {
59 0
        if (!in_array($mode, ['append', 'prepend', 'replace'])) {
60 0
            throw new BuildException("Illegal mode: needs to be either append, prepend or replace");
61
        }
62

63 0
        $this->mode = $mode;
64
    }
65

66
    /**
67
     * Main entry point
68
     */
69 1
    public function main()
70
    {
71

72
        // Apparently casting to (string) no longer invokes __toString() automatically.
73 1
        if (is_object($this->classpath)) {
74 1
            $classpath = $this->classpath->__toString();
75
        }
76

77 1
        if (empty($classpath)) {
78 0
            throw new BuildException("Provided classpath was empty.");
79
        }
80

81 1
        $curr_parts = Phing::explodeIncludePath();
82 1
        $add_parts = Phing::explodeIncludePath($classpath);
83 1
        $new_parts = array_diff($add_parts, $curr_parts);
84

85 1
        if ($new_parts) {
86 1
            $this->updateIncludePath($new_parts, $curr_parts);
87
        }
88
    }
89

90
    /**
91
     * @param $new_parts
92
     * @param $curr_parts
93
     */
94 1
    private function updateIncludePath($new_parts, $curr_parts)
95
    {
96 1
        $includePath = [];
97 1
        $verb = "";
98

99 1
        switch ($this->mode) {
100 1
            case "append":
101 0
                $includePath = array_merge($curr_parts, $new_parts);
102 0
                $verb = "Appending";
103 0
                break;
104

105 1
            case "replace":
106 0
                $includePath = $new_parts;
107 0
                $verb = "Replacing";
108 0
                break;
109

110 1
            case "prepend":
111 1
                $includePath = array_merge($new_parts, $curr_parts);
112 1
                $verb = "Prepending";
113 1
                break;
114
        }
115

116 1
        $this->log(
117 1
            $verb . " new include_path components: " . implode(PATH_SEPARATOR, $new_parts),
118 1
            Project::MSG_VERBOSE
119
        );
120

121 1
        set_include_path(implode(PATH_SEPARATOR, $includePath));
122
    }
123
}

Read our documentation on viewing source code .

Loading