Passed
Push — main ( 4e114c...753ff6 )
by Siad
06:16
created

ReflexiveTask   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 17.78%

Importance

Changes 0
Metric Value
wmc 16
eloc 49
dl 0
loc 98
ccs 8
cts 45
cp 0.1778
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D main() 0 75 15
A setFile() 0 3 1
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;
21
22
use Exception;
23
use Phing\Exception\BuildException;
24
use Phing\Io\File;
25
use Phing\Io\FileReader;
26
use Phing\Io\FileUtils;
27
use Phing\Io\FileWriter;
28
use Phing\Project;
29
use Phing\Task;
30
use Phing\Type\Element\FileSetAware;
31
use Phing\Type\Element\FilterChainAware;
32
33
/**
34
 * This task is for using filter chains to make changes to files and overwrite the original files.
35
 *
36
 * This task was created to serve the need for "cleanup" tasks -- e.g. a ReplaceRegexp task or strip task
37
 * being used to modify files and then overwrite the modified files.  In many (most?) cases you probably
38
 * should just use a copy task  to preserve the original source files, but this task supports situations
39
 * where there is no src vs. build directory, and modifying source files is actually desired.
40
 *
41
 * <code>
42
 *    <reflexive>
43
 *        <fileset dir=".">
44
 *            <include pattern="*.html">
45
 *        </fileset>
46
 *        <filterchain>
47
 *            <replaceregexp>
48
 *                <regexp pattern="\n\r" replace="\n"/>
49
 *            </replaceregexp>
50
 *        </filterchain>
51
 *    </reflexive>
52
 * </code>
53
 *
54
 * @author Hans Lellelid <[email protected]>
55
 *
56
 */
57
class ReflexiveTask extends Task
58
{
59
    use FileSetAware;
60
    use FilterChainAware;
61
62
    /**
63
     * Single file to process.
64
     * @var File
65
     */
66
    private $file;
67
68
    /**
69
     * Alias for setFrom()
70
     *
71
     */
72 1
    public function setFile(File $f)
73
    {
74 1
        $this->file = $f;
75 1
    }
76
77
    /**
78
     * Append the file(s).
79
     */
80 2
    public function main()
81
    {
82 2
        if ($this->file === null && empty($this->filesets)) {
83 1
            throw new BuildException("You must specify a file or fileset(s) for the <reflexive> task.");
84
        }
85
86 1
        if ($this->file->isDirectory()) {
87 1
            throw new BuildException('File cannot be a directory.');
88
        }
89
90
        // compile a list of all files to modify, both file attrib and fileset elements
91
        // can be used.
92
93
        $files = [];
94
95
        if ($this->file !== null) {
96
            $files[] = $this->file;
97
        }
98
99
        if (!empty($this->filesets)) {
100
            foreach ($this->filesets as $fs) {
101
                try {
102
                    $ds = $fs->getDirectoryScanner($this->project);
103
                    $filenames = $ds->getIncludedFiles(); // get included filenames
104
                    $dir = $fs->getDir($this->project);
105
                    foreach ($filenames as $fname) {
106
                        $files[] = new File($dir, $fname);
107
                    }
108
                } catch (BuildException $be) {
109
                    $this->log($be->getMessage(), Project::MSG_WARN);
110
                }
111
            }
112
        }
113
114
        $this->log("Applying reflexive processing to " . count($files) . " files.");
115
116
        // These "slots" allow filters to retrieve information about the currently-being-process files
117
        $slot = $this->getRegisterSlot("currentFile");
118
        $basenameSlot = $this->getRegisterSlot("currentFile.basename");
119
120
        foreach ($files as $file) {
121
            // set the register slots
122
123
            $slot->setValue($file->getPath());
124
            $basenameSlot->setValue($file->getName());
125
126
            // 1) read contents of file, pulling through any filters
127
            $in = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $in is dead and can be removed.
Loading history...
128
            $out = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $out is dead and can be removed.
Loading history...
129
            $contents = "";
130
131
            try {
132
                $in = FileUtils::getChainedReader(new FileReader($file), $this->filterChains, $this->project);
133
                while (-1 !== ($buffer = $in->read())) {
134
                    $contents .= $buffer;
135
                }
136
                $in->close();
137
            } catch (Exception $e) {
138
                if ($in) {
139
                    $in->close();
140
                }
141
                $this->log("Error reading file: " . $e->getMessage(), Project::MSG_WARN);
142
            }
143
144
            try {
145
                // now create a FileWriter w/ the same file, and write to the file
146
                $out = new FileWriter($file);
147
                $out->write($contents);
148
                $out->close();
149
                $this->log("Applying reflexive processing to " . $file->getPath(), Project::MSG_VERBOSE);
150
            } catch (Exception $e) {
151
                if ($out) {
152
                    $out->close();
153
                }
154
                $this->log("Error writing file back: " . $e->getMessage(), Project::MSG_WARN);
155
            }
156
        }
157
    }
158
}
159