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
 * Coverts a path to a fileset.
22
 * This is useful if you have a path but need to use a fileset as input in a phing task.
23
 *
24
 * Example
25
 * =======
26
 *
27
 * ```
28
 *   <path id="modified.sources.path" dir="C:\Path\to\phing\classes\phing\" />
29
 *   <pathtofileset name="modified.sources.fileset"
30
 *                  pathrefid="modified.sources.path"
31
 *                  dir="." />
32
 *
33
 *   <copy todir="C:\Path\to\phing\docs\api">
34
 *     <mapper type="glob" from="*.php" to="*.php.bak" />
35
 *     <fileset refid="modified.sources.fileset" />
36
 *   </copy>
37
 * ```
38
 *
39
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
40
 * @package phing.tasks.ext.property
41
 */
42
class PathToFileSet extends Task
43
{
44
    /**
45
     * @var PhingFile $dir
46
     */
47
    private $dir;
48

49
    /**
50
     * @var string $name
51
     */
52
    private $name;
53

54
    /**
55
     * @var string $pathRefId
56
     */
57
    private $pathRefId;
58

59
    /**
60
     * @var bool $ignoreNonRelative
61
     */
62
    private $ignoreNonRelative = false;
63

64
    /**
65
     * @param PhingFile $dir
66
     */
67 0
    public function setDir(PhingFile $dir)
68
    {
69 0
        $this->dir = $dir;
70
    }
71

72
    /**
73
     * @param $name
74
     */
75 0
    public function setName($name)
76
    {
77 0
        $this->name = $name;
78
    }
79

80
    /**
81
     * @param $pathRefId
82
     */
83 0
    public function setPathRefId($pathRefId)
84
    {
85 0
        $this->pathRefId = $pathRefId;
86
    }
87

88
    /**
89
     * @param $ignoreNonRelative
90
     */
91 0
    public function setIgnoreNonRelative($ignoreNonRelative)
92
    {
93 0
        $this->ignoreNonRelative = $ignoreNonRelative;
94
    }
95

96
    /**
97
     * {@inheritdoc}
98
     *
99
     * @throws BuildException
100
     * @throws IOException
101
     */
102 0
    public function main()
103
    {
104 0
        if ($this->dir == null) {
105 0
            throw new BuildException("missing dir");
106
        }
107 0
        if ($this->name == null) {
108 0
            throw new BuildException("missing name");
109
        }
110 0
        if ($this->pathRefId == null) {
111 0
            throw new BuildException("missing pathrefid");
112
        }
113 0
        if (!$this->dir->isDirectory()) {
114 0
            throw new BuildException(
115 0
                (string) $this->dir . " is not a directory"
116
            );
117
        }
118 0
        $path = $this->getProject()->getReference($this->pathRefId);
119 0
        if ($path == null) {
120 0
            throw new BuildException("Unknown reference " . $this->pathRefId);
121
        }
122 0
        if (!($path instanceof Path)) {
123 0
            throw new BuildException($this->pathRefId . " is not a path");
124
        }
125 0
        $sources = $path->listPaths();
126 0
        $fileSet = new FileSet();
127 0
        $fileSet->setProject($this->getProject());
128 0
        $fileSet->setDir($this->dir);
129 0
        $fileUtils = new FileUtils();
130 0
        $dirNormal = $fileUtils->normalize($this->dir->getAbsolutePath());
131 0
        $dirNormal = rtrim($dirNormal, FileUtils::getSeparator()) . FileUtils::getSeparator();
132

133 0
        $atLeastOne = false;
134 0
        for ($i = 0, $resourcesCount = count($sources); $i < $resourcesCount; ++$i) {
135 0
            $sourceFile = new PhingFile($sources[$i]);
136 0
            if (!$sourceFile->exists()) {
137 0
                continue;
138
            }
139 0
            $includePattern = $this->getIncludePattern($dirNormal, $sourceFile);
140 0
            if ($includePattern === false && !$this->ignoreNonRelative) {
141 0
                throw new BuildException(
142 0
                    $sources[$i] . " is not relative to " . $this->dir->getAbsolutePath()
143
                );
144
            }
145 0
            if ($includePattern === false) {
146 0
                continue;
147
            }
148 0
            $fileSet->createInclude()->setName($includePattern);
149 0
            $atLeastOne = true;
150
        }
151 0
        if (!$atLeastOne) {
152 0
            $fileSet->createInclude()->setName("a:b:c:d//THis si &&& not a file !!! ");
153
        }
154 0
        $this->getProject()->addReference($this->name, $fileSet);
155
    }
156

157
    /**
158
     * @param string $dirNormal
159
     * @param PhingFile $file
160
     * @return string|false
161
     * @throws IOException
162
     */
163 0
    private function getIncludePattern($dirNormal, PhingFile $file)
164
    {
165 0
        $fileUtils = new FileUtils();
166 0
        $fileNormal = $fileUtils->normalize($file->getAbsolutePath());
167

168 0
        return rtrim(str_replace('\\', '/', substr($fileNormal, strlen($dirNormal))), '/') . '/';
169
    }
170
}

Read our documentation on viewing source code .

Loading