Passed
Push — master ( 383118...127e3c )
by Siad
06:48
created

ResolvePathTask::setLevel()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 6
nop 1
dl 0
loc 21
ccs 0
cts 19
cp 0
crap 42
rs 9.0111
c 0
b 0
f 0
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
 * Task for resolving relative paths and setting absolute path in property value.
22
 *
23
 * This task was created to address a need for resolving absolute paths of files / directories.
24
 * In many cases a relative directory (e.g. "./build") is specified, but it needs to be treated
25
 * as an absolute path since other build files (e.g. in subdirs) should all be using the same
26
 * path -- and not treating it as a relative path to their own directory.
27
 *
28
 * <code>
29
 * <property name="relative_path" value="./dirname"/>
30
 * <resolvepath propertyName="absolute_path" file="${relative_path}"/>
31
 * <echo>Resolved [absolute] path: ${absolute_path}</echo>
32
 * </code>
33
 *
34
 * TODO:
35
 *      - Possibly integrate this with PackageAsPath, for handling/resolving dot-path paths.
36
 *
37
 * @author  Hans Lellelid <[email protected]>
38
 * @package phing.tasks.system
39
 */
40
class ResolvePathTask extends Task
41
{
42
    use LogLevelAware;
43
44
    /**
45
     * Name of property to set.
46
     */
47
    private $propertyName;
48
49
    /**
50
     * The [possibly] relative file/path that needs to be resolved.
51
     */
52
    private $file;
53
54
    /**
55
     * Base directory used for resolution.
56
     *
57
     * @var PhingFile
58
     */
59
    private $dir;
60
61
    /**
62
     * Set the name of the property to set.
63
     *
64
     * @param  string $v Property name
65
     * @return void
66
     */
67 137
    public function setPropertyName($v)
68
    {
69 137
        $this->propertyName = $v;
70 137
    }
71
72
    /**
73
     * Sets a base dir to use for resolution.
74
     *
75
     * @param PhingFile $d
76
     */
77 4
    public function setDir(PhingFile $d)
78
    {
79 4
        $this->dir = $d;
80 4
    }
81
82
    /**
83
     * Sets a path (file or directory) that we want to resolve.
84
     * This is the same as setFile() -- just more generic name so that it's
85
     * clear that you can also use it to set directory.
86
     *
87
     * @param string $f
88
     * @see   setFile()
89
     */
90 6
    public function setPath($f)
91
    {
92 6
        $this->file = $f;
93 6
    }
94
95
    /**
96
     * Sets a file that we want to resolve.
97
     *
98
     * @param string $f
99
     */
100 131
    public function setFile($f)
101
    {
102 131
        $this->file = $f;
103 131
    }
104
105
    /**
106
     * Perform the resolution & set property.
107
     */
108 137
    public function main()
109
    {
110 137
        if (!$this->propertyName) {
111
            throw new BuildException("You must specify the propertyName attribute", $this->getLocation());
112
        }
113
114
        // Currently only files are supported
115 137
        if ($this->file === null) {
116
            throw new BuildException("You must specify a path to resolve", $this->getLocation());
117
        }
118
119 137
        $fs = FileSystem::getFileSystem();
120
121
        // if dir attribute was specified then we should
122
        // use that as basedir to which file was relative.
123
        // -- unless the file specified is an absolute path
124 137
        if ($this->dir !== null && !$fs->isAbsolute(new PhingFile($this->file))) {
125 4
            $this->file = new PhingFile($this->dir->getPath(), $this->file);
126
        }
127
128 137
        $resolved = $this->project->resolveFile($this->file);
129
130 137
        $this->log("Resolved " . $this->file . " to " . $resolved->getAbsolutePath(), $this->logLevel);
131 137
        $this->project->setProperty($this->propertyName, $resolved->getAbsolutePath());
132 137
    }
133
}
134