Passed
Push — master ( 7a9799...2aee06 )
by Michiel
06:03
created

PathToFileSet::setPathRefId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 1
crap 2
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
namespace Phing\Task\System\Property;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\FileUtils;
24
use Phing\Io\IOException;
25
use Phing\Io\File;
26
use Phing\Task;
27
use Phing\Type\FileSet;
28
use Phing\Type\Path;
29
30
/**
31
 * Coverts a path to a fileset.
32
 * This is useful if you have a path but need to use a fileset as input in a phing task.
33
 *
34
 * Example
35
 * =======
36
 *
37
 * ```
38
 *   <path id="modified.sources.path" dir="C:\Path\to\phing\classes\phing\" />
39
 *   <pathtofileset name="modified.sources.fileset"
40
 *                  pathrefid="modified.sources.path"
41
 *                  dir="." />
42
 *
43
 *   <copy todir="C:\Path\to\phing\docs\api">
44
 *     <mapper type="glob" from="*.php" to="*.php.bak" />
45
 *     <fileset refid="modified.sources.fileset" />
46
 *   </copy>
47
 * ```
48
 *
49
 * @author  Siad Ardroumli <[email protected]>
50
 * @package phing.tasks.ext.property
51
 */
52
class PathToFileSet extends Task
53
{
54
    /**
55
     * @var File $dir
56
     */
57
    private $dir;
58
59
    /**
60
     * @var string $name
61
     */
62
    private $name;
63
64
    /**
65
     * @var string $pathRefId
66
     */
67
    private $pathRefId;
68
69
    /**
70
     * @var bool $ignoreNonRelative
71
     */
72
    private $ignoreNonRelative = false;
73
74
    /**
75
     * @param File $dir
76
     */
77
    public function setDir(File $dir)
78
    {
79
        $this->dir = $dir;
80
    }
81
82
    /**
83
     * @param $name
84
     */
85
    public function setName($name)
86
    {
87
        $this->name = $name;
88
    }
89
90
    /**
91
     * @param $pathRefId
92
     */
93
    public function setPathRefId($pathRefId)
94
    {
95
        $this->pathRefId = $pathRefId;
96
    }
97
98
    /**
99
     * @param $ignoreNonRelative
100
     */
101
    public function setIgnoreNonRelative($ignoreNonRelative)
102
    {
103
        $this->ignoreNonRelative = $ignoreNonRelative;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     *
109
     * @throws BuildException
110
     * @throws IOException
111
     */
112
    public function main()
113
    {
114
        if ($this->dir == null) {
115
            throw new BuildException("missing dir");
116
        }
117
        if ($this->name == null) {
118
            throw new BuildException("missing name");
119
        }
120
        if ($this->pathRefId == null) {
121
            throw new BuildException("missing pathrefid");
122
        }
123
        if (!$this->dir->isDirectory()) {
124
            throw new BuildException(
125
                (string) $this->dir . " is not a directory"
126
            );
127
        }
128
        $path = $this->getProject()->getReference($this->pathRefId);
129
        if ($path == null) {
130
            throw new BuildException("Unknown reference " . $this->pathRefId);
131
        }
132
        if (!($path instanceof Path)) {
133
            throw new BuildException($this->pathRefId . " is not a path");
134
        }
135
        $sources = $path->listPaths();
136
        $fileSet = new FileSet();
137
        $fileSet->setProject($this->getProject());
138
        $fileSet->setDir($this->dir);
139
        $fileUtils = new FileUtils();
140
        $dirNormal = $fileUtils->normalize($this->dir->getAbsolutePath());
141
        $dirNormal = rtrim($dirNormal, FileUtils::getSeparator()) . FileUtils::getSeparator();
142
143
        $atLeastOne = false;
144
        for ($i = 0, $resourcesCount = count($sources); $i < $resourcesCount; ++$i) {
145
            $sourceFile = new File($sources[$i]);
146
            if (!$sourceFile->exists()) {
147
                continue;
148
            }
149
            $includePattern = $this->getIncludePattern($dirNormal, $sourceFile);
150
            if ($includePattern === false && !$this->ignoreNonRelative) {
151
                throw new BuildException(
152
                    $sources[$i] . " is not relative to " . $this->dir->getAbsolutePath()
153
                );
154
            }
155
            if ($includePattern === false) {
156
                continue;
157
            }
158
            $fileSet->createInclude()->setName($includePattern);
159
            $atLeastOne = true;
160
        }
161
        if (!$atLeastOne) {
0 ignored issues
show
introduced by
The condition $atLeastOne is always false.
Loading history...
162
            $fileSet->createInclude()->setName("a:b:c:d//THis si &&& not a file !!! ");
163
        }
164
        $this->getProject()->addReference($this->name, $fileSet);
165
    }
166
167
    /**
168
     * @param string $dirNormal
169
     * @param File $file
170
     * @return string|false
171
     * @throws IOException
172
     */
173
    private function getIncludePattern($dirNormal, File $file)
174
    {
175
        $fileUtils = new FileUtils();
176
        $fileNormal = $fileUtils->normalize($file->getAbsolutePath());
177
178
        return rtrim(str_replace('\\', '/', substr($fileNormal, strlen($dirNormal))), '/') . '/';
179
    }
180
}
181